如何识别不正确的编码以从我的 R 线性规划优化模型中获得正确的结果?

问题描述 投票:0回答:0

# 
嘿蜂巢

我有一个线性规划任务,我正在努力解决。我已经制定了 LP 问题并生成了一些似乎可以运行的代码;但是,我对输出结果持怀疑态度,不认为它是问题的正确答案。

**问题是这样的:** 一家工厂用三种材料生产春、秋、冬三种产品 含有棉花、羊毛和丝绸。

        Sale Price    Production Cost
Spring  60            5
Autumn  55            3
Winter  60            5

        Purchase Price 
Cotton  30
Wool    45
Silk    50

        Demand  Min Cotton Proportion   Min Wool Proportion
Spring  3300    .55                    .30
Autumn  3600    .45                    .4
Winter  4000    .3                     .5
My Formulation of the problem is this:
Decision variables:
xij ≥ 0 denotes # of tons of
product_j ϵ {1 = Spring, 2 = Autumn, 3 = Winter}
produced from
material_i ϵ {c = Cotton, w = Wool, s = Silk}

So let:
1c, 1w, 1s, = cotton, wool and silk demand for spring
2c, 2w, 2s = cotton, wool and silk demand for autumn
3c, 3w, 2s = cotton, wool and silk demand for winter

Objective Function:
Maximize profit
Profit = Sales Price - Production Cost - Purchase Price

MAX ((55 * 1c) + (55 * 1w) + (55 * 1s) + (52 * 2c) + (52 * 2w) + (52 * 2s) + (55 * 3c) + (55 * 3w) + (55 * 3W)) - ((30 * 1c) - (30 * 2c) - (30 * 3c) - (45 * 1w) - (45 * 2w) - (45 * 3w) - (50 * 1s) - (50 * 2s) - (50 * 3s))

Constraints:
Spring Cotton Proportion + Spring Wool Proportion + Spring silk Proportion <= 3300 or 1c + 1w + 1s <= 3300
Autumn Cotton Proportion + Autumn Wool Proportion + Autumn silk Proportion <= 3600 or 2c + 2w + 2s <= 3600
Winter Cotton Proportion + Winter Wool Proportion + Winter silk Proportion <= 400 or 3c + 3w + 3s <= 4000

Spring Cotton Proportion >= 55% or 1c >= .55
Autumn Cotton Proportion >= 45% or 2c >= .45
Winter Cotton Proportion >= 30% or 3c >= .30
Spring Wool Proportion >= 30% or 1w >= .30
Autumn Wool Proportion >= 40% or 2w >= .40
Winter Wool Proportion >= 50% or 3w >= .50

All variables >= 0


> # Define the LP model
> model2 <- make.lp(0, 9)  # 3 rows for demand constraints, 9 columns for decision variables
> 
> # Set the objective function coefficients
> obj.coefs <- c(55, 55, 55, 52, 52, 52, 55, 55, 55) - c(30, 30, 30, 45, 45, 45, 50, 50, 50)
> lp.control(model2, sense = "max")  # Set the optimization sense to maximize
$anti.degen
[1] "fixedvars" "stalling" 

$basis.crash
[1] "none"

$bb.depthlimit
[1] -50

$bb.floorfirst
[1] "automatic"

$bb.rule
[1] "pseudononint" "greedy"       "dynamic"      "rcostfixing" 

$break.at.first
[1] FALSE

$break.at.value
[1] 1e+30

$epsilon
      epsb       epsd      epsel     epsint epsperturb   epspivot 
     1e-10      1e-09      1e-12      1e-07      1e-05      2e-07 

$improve
[1] "dualfeas" "thetagap"

$infinite
[1] 1e+30

$maxpivot
[1] 250

$mip.gap
absolute relative 
   1e-11    1e-11 

$negrange
[1] -1e+06

$obj.in.basis
[1] TRUE

$pivoting
[1] "devex"    "adaptive"

$presolve
[1] "none"

$scalelimit
[1] 5

$scaling
[1] "geometric"   "equilibrate" "integers"   

$sense
[1] "maximize"

$simplextype
[1] "dual"   "primal"

$timeout
[1] 0

$verbose
[1] "neutral"

> set.objfn(model2, obj.coefs)
> 
> # Add the constraints
> add.constraint(model2, c(1, 1, 1, 0, 0, 0, 0, 0, 0), "<=", 3300)  # Demand constraint for Spring (Cotton, Wool, Silk)
> add.constraint(model2, c(0, 0, 0, 1,1,1, 0, 0, 0), "<=", 3600)  # Demand constraint for Autumn (Cotton, Wool, Silk)
> add.constraint(model2, c(0, 0, 0, 0, 0, 0, 1,1,1), "<=", 4000)  # Demand constraint for Winter (Cotton, Wool, Silk)
> # Set the cotton proportion constraints
> add.constraint(model2, c(0.55, 0, 0, 0, 0, 0, 0, 0, 0), ">=", 0)  # Spring cotton proportion
> add.constraint(model2, c(0, 0, 0, 0.45, 0, 0, 0, 0, 0), ">=", 0)  # Autumn cotton proportion
> add.constraint(model2, c(0, 0, 0, 0, 0, 0, 0.30, 0, 0), ">=", 0)  # Winter cotton proportion
> 
> # Set the wool proportion constraints
> add.constraint(model2, c(0, 0.30, 0, 0, 0, 0, 0, 0, 0), ">=", 0)  # Spring wool proportion
> add.constraint(model2, c(0, 0, 0, 0, 0.40, 0, 0, 0, 0), ">=", 0)  # Autumn wool proportion
> add.constraint(model2, c(0, 0, 0, 0, 0, 0, 0, 0.50, 0), ">=", 0)  # Winter wool proportion
> add.constraint(model2, rep(0, 9), ">", 0)  # Non-negativity constraint for all variables
> 
> # Solve the LP model
> solve(model2)
[1] 0
> 
> # Get the optimal profit
> optimal_profit <- get.objective(model2)
> 
> # Get the optimal values of the decision variables
> optimal_values <- get.variables(model2)
> 
> # Display the results
> cat("Optimal Profit: $", optimal_profit, "\n")
Optimal Profit: $ 127700 
> cat("Optimal Values of Decision Variables:\n")
Optimal Values of Decision Variables:
> cat("Spring (Cotton):", optimal_values[1], "tons\n")
Spring (Cotton): 3300 tons
> cat("Spring (Wool):", optimal_values[2], "tons\n")
Spring (Wool): 0 tons
> cat("Spring (Silk):", optimal_values[3], "tons\n")
Spring (Silk): 0 tons
> cat("Autumn (Cotton):", optimal_values[4], "tons\n")
Autumn (Cotton): 3600 tons
> cat("Autumn (Wool):", optimal_values[5], "tons\n")
Autumn (Wool): 0 tons
> cat("Autumn (Silk):", optimal_values[6], "tons\n")
Autumn (Silk): 0 tons
> cat("Winter (Cotton):", optimal_values[7], "tons\n")
Winter (Cotton): 4000 tons
> cat("Winter (Wool):", optimal_values[8], "tons\n")
Winter (Wool): 0 tons
> cat("Winter (Silk):", optimal_values[9], "tons\n")
Winter (Silk): 0 tons



r mathematical-optimization linear-programming lpsolve
© www.soinside.com 2019 - 2024. All rights reserved.