簡體   English   中英

解決 R 中的線性規划 (LP) 問題

[英]Solve a linear programming (LP) problem in R

function 接受目標 function 的系數、約束矩陣、約束的右側值、約束的方向和 LP 問題的類型(最小化或最大化)。 然后它使用 lpSolveAPI package 創建一個 LP 問題,設置問題類型、決策變量類型和約束,然后求解 LP 問題。 function 返回包含最優解和目標 function 值的列表,用戶可以訪問該列表。 然后使用特定輸入調用 function 並打印最佳解決方案和目標 function 值似乎都是正確的,但我遇到了 sm 問題

這是我的 function:

solve_lp <- function(objective_coefs, constraints_matrix, constraints_rhs, constraints_dir, problem_type) {
  
  # Load the lpSolveAPI package
  library(lpSolveAPI)
  
  # Set the number of rows (constraints) and columns (decision variables)
  nrow <- nrow(constraints_matrix)
  ncol <- ncol(constraints_matrix)
  
  # Create an LP problem with nrow constraints and ncol decision variables
  lprec <- make.lp(nrow = nrow, ncol = ncol)
  
  # Set the type of problem to minimize or maximize the objective function based on the problem_type argument
  lp.control(lprec, sense=problem_type)
  
  # Set the type of decision variables to integer
  set.type(lprec, 1:ncol, type=c("integer"))
  
  # Set the objective function coefficients
  set.objfn(lprec, objective_coefs)
  
  # Add the constraints to the LP problem
  for (i in 1:nrow) {
    add.constraint(lprec, constraints_matrix[i, ], constraints_dir[i], constraints_rhs[i])
  }
  
  # Solve the LP problem
  solve(lprec)
  
  # If the problem has a feasible solution, get the decision variables values and the value of the objective function
  solution <- get.variables(lprec)
  obj_value <- get.objective(lprec)
  
  # Return the optimal solution and objective function value
  return(list(solution = solution, obj_value = obj_value))
}


  objective_coefs <- c(15, 3, -6)

constraints_matrix <- matrix(c(1, 1, 1,
                               2, -1, -2,
                               2, 3, -5), nrow=3, byrow=TRUE)

constraints_rhs <- c(36, 8, 10)

constraints_dir <- c("<=", ">=", "=")

problem_type <- "min"


# Solve the LP problem using the solve_lp function
result <- solve_lp(objective_coefs = objective_coefs, constraints_matrix = constraints_matrix, constraints_rhs = constraints_rhs, constraints_dir = constraints_dir,  problem_type= problem_type)
                   # Extract the optimal solution and objective function value
                   optimal_solution <- result$solution
                   obj_value <- result$obj_value
                   
                   # Print the results
                   print(paste("Optimal solution:", optimal_solution))
                   print(paste("Objective function value:", obj_value))
 
               min z = 15x1 + 3x2 − 6x3
               S.C
               x1 + x2 + x3 ≤ 36
               2x1 − x2 − 2x3 ≥ 8
               2x1 + 3x2 − 5x3 = 10
               x1, x2, x3 ≥ 0

結果 output 變成了這個程序的線性"Optimal solution: 5" "Optimal solution: 0" "Optimal solution: 0"我從我們在 class 中手動執行的程序測試了這個程序,但我們有不同的 output 7/14 0.5 0我的問題是哪種解決方案是正確的

使用 lpSolve 會更容易。 res$solution 給出了解決方案。 res$status 為 0 表示成功。

library(lpSolve)

res <- lp("min", objective_coefs, constraints_matrix, 
  constraints_dir, constraints_rhs)
str(res)

給予:

List of 28
 $ direction       : int 0
 $ x.count         : int 3
 $ objective       : num [1:3] 15 3 -6
 $ const.count     : int 3
 $ constraints     : num [1:5, 1:3] 1 1 1 1 36 2 -1 -2 2 8 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:5] "" "" "" "const.dir.num" ...
  .. ..$ : NULL
 $ int.count       : int 0
 $ int.vec         : int 0
 $ bin.count       : int 0
 $ binary.vec      : int 0
 $ num.bin.solns   : int 1
 $ objval          : num 65.2
 $ solution        : num [1:3] 4.25 0.5 0
 $ presolve        : int 0
 $ compute.sens    : int 0
 $ sens.coef.from  : num 0
 $ sens.coef.to    : num 0
 $ duals           : num 0
 $ duals.from      : num 0
 $ duals.to        : num 0
 $ scale           : int 196
 $ use.dense       : int 0
 $ dense.col       : int 0
 $ dense.val       : num 0
 $ dense.const.nrow: int 0
 $ dense.ctr       : num 0
 $ use.rw          : int 0
 $ tmp             : chr "Nobody will ever look at this"
 $ status          : int 0
 - attr(*, "class")= chr "lp"

筆記

我們使用了這些輸入:

objective_coefs <- c(15, 3, -6)
constraints_matrix <- matrix(c(1, 1, 1,
                               2, -1, -2,
                               2, 3, -5), nrow=3, byrow=TRUE)
constraints_rhs <- c(36, 8, 10)
constraints_dir <- c("<=", ">=", "=")
problem_type <- "min"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM