Я пытаюсь выполнить упражнение, связанное с оптимизацией, используя бинарное ограничение. Ниже приводится описание проблемы.
![Supply chain - basic BIP](https://i.stack.imgur.com/SDFs1.png)
Для этой проблемы я использую R
и lpSolveAPI
- пока мне удалось перевестизадача в список ограничений и построить правильную целевую функцию для задачи, однако моя программа не выдает правильный вывод, потому что я помещаю три Y
переменные (yE
, yT
и yN
) в мою целевую функцию,Моя целевая функция не должна содержать три конечных 0
(см. Определение целевой функции на картинке выше).
Мой вопрос как мне определить переменную y
так, чтобы ониявляются двоичными и используются только как часть ограничения (поэтому они не отображаются в целевой функции)?
# SELECT FROM ....
require(lpSolveAPI)
# Set the decision variables
obj <- c(21, 22.5, 22.5, 24.5, 23, 25.5, 0, 0, 0)
# Set the constrains parameters
# EG,EK,TG,TK,NG,NK,yE,yT,yN
LHS <- matrix(c(1, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0, 0,
1, 0, 1, 0, 1, 0, 0, 0, 0,
0, 1, 0, 1, 0, 1, 0, 0, 0,
1, 1, 0, 0, 0, 0, -425, 0, 0,
0, 0, 1, 1, 0, 0, 0, -400, 0,
0, 0, 0, 0, 1, 1, 0, 0, -750,
0, 0, 0, 0, 0, 0, 1, 1, 1), nrow=9, byrow = TRUE)
RHS <- c(425, 400, 750, 550, 450, 0, 0, 0, 2)
constranints_direction <- c("<=", "<=", "<=", ">=", ">=", "<=", "<=", "<=", "<=")
# Set 9 constraints and 9 decision variables ==> THERE SHOULD BE ONLY 6 !!!
lprec <- make.lp(nrow = 9, ncol = 9)
# Set the type of problem we are trying to solve
lp.control(lprec, sense="min")
set.type(lprec, 7:9, c("binary"))
set.objfn(lprec, obj)
add.constraint(lprec, LHS[1, ], constranints_direction[[1]], RHS[1])
add.constraint(lprec, LHS[2, ], constranints_direction[[2]], RHS[2])
add.constraint(lprec, LHS[3, ], constranints_direction[[3]], RHS[3])
add.constraint(lprec, LHS[4, ], constranints_direction[[4]], RHS[4])
add.constraint(lprec, LHS[5, ], constranints_direction[[5]], RHS[5])
add.constraint(lprec, LHS[6, ], constranints_direction[[6]], RHS[6])
add.constraint(lprec, LHS[7, ], constranints_direction[[7]], RHS[7])
add.constraint(lprec, LHS[8, ], constranints_direction[[8]], RHS[8])
add.constraint(lprec, LHS[9, ], constranints_direction[[9]], RHS[9])
# Display the LPsolve matrix
lprec
get.type(lprec)
# Solve problem
solve(lprec)
# Get the decision variables values
get.variables(lprec)
# Get the value of the objective function
get.objective(lprec)
Этот код выдает целевой результат 22850
> # Get the decision variables values
> get.variables(lprec)
[1] 0 425 0 0 550 25 1 0 1
> # Get the value of the objective function
> get.objective(lprec)
[1] 22850
Однако для того же распределения переменных он должен выдать 22850,50.