如何用 nxn 矩阵解决线性规划问题,使得行和列之和为给定值

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

我正在尝试在 R 上签订一项计划,解决 243 个国家之间的最佳贸易组织问题,以最大限度地减少碳排放。我正在调查 4 种交通方式:航空、水路、公路和铁路。因此,我想解决以下问题:

选择 4 个贸易矩阵,在按元素乘以其排放系数时最小化元素总和,从而使总贸易保持不变,这意味着国家之间的行和列的总和与样本中的相同。

我为 3x3 示例编写了以下代码,但它不起作用,我不知道还能去哪里。我最终需要将其扩展到大小为 243 x 243 的矩阵。

# Test Emission by MOT 1 Matrix
E1 <- matrix(c(0,1,1,1,0,1,1,1,0), nrow = 3)

print(E1)

# Test Emission by MOT 2 Matrix
E2 <- matrix(c(0,2,2,2,0,2,2,2,0), nrow = 3)

print(E2)

# Test Distance between three countries matrix
D <- matrix(c(0,1,2,1,0,1,2,1,0), nrow = 3)

print(D)


objective.in <- function(values, A, B) {
  X <- matrix(values[1:9], nrow = 3, ncol = 3)
  Y <- matrix(values[10:18], nrow = 3, ncol = 3)
  return(sum(E1 * D * X + E2 * D * Y))
}

Trade_MOT1_Hypothetical <- matrix(c(0,4,6,4,0,5,2,1,0), nrow = 3)
Trade_MOT2_Hypothetical <- matrix(c(0,8,9,1,0,17,3,1,0), nrow = 3)

print(Trade_MOT1_Hypothetical)
print(Trade_MOT2_Hypothetical)

test_input <- c(Trade_MOT1_Hypothetical, Trade_MOT2_Hypothetical)

print(test_input)

print(objective.in(test_input))

# Summation matrix for the first column sum constraint

c1 <- matrix(c(1, 1, 1, 0, 0, 0, 0, 0, 0), nrow = 9)

# Summation matrix for the second column sum constraint
c2 <- matrix(c(0, 0, 0, 1, 1, 1, 0, 0, 0), nrow = 9)

# Summation matrix for the third column sum constraint
c3 <- matrix(c(0, 0, 0, 0, 0, 0, 1, 1, 1), nrow = 9)

# Summation matrix for the first row sum constraint
r1 <- matrix(c(1, 0, 0, 1, 0, 0, 1, 0, 0), nrow = 9)

# Summation matrix for the second row sum constraint
r2 <- matrix(c(0, 1, 0, 0, 1, 0, 0, 1, 0), nrow = 9)

# Summation matrix for the third row sum constraint
r3 <- matrix(c(0, 0, 1, 0, 0, 1, 0, 0, 1), nrow = 9)

const.mat <- matrix(c(r1, r2, r3, c1, c2, c3, r1, r2, r3, c1, c2, c3), nrow = 6)

print(const.mat)

const.dir <- c("=", "=", "=", "=", "=", "=")

const.rhs <- c(1000, 1000, 1000, 2000, 2000, 2000)

optimum <-  lp(direction = "min", objective.in, const.mat, const.dir, const.rhs)

optimum$solution

print(optimum$solution)

我尝试在约束矩阵中使用求和矩阵来应用于行和列总和约束,但这并没有真正起作用。

r mathematical-optimization linear-programming lpsolve
1个回答
0
投票

在不深入研究你的LP问题的情况下,你似乎有18个决策变量,6个约束,并且你想解决一个最小化问题,只需将你的目标函数更改为以下:

library(lpSolve)

objective.in <- c(E1*D, E2*D)
optimum <-  lp(direction = "min", objective.in, const.mat, const.dir, const.rhs)

optimum$solution
# [1] 1000    0 1000 1000    0    0    0    0 1000    0    0    0    0    0    0    0    0    0

optimum
# Success: the objective function is 3000 

这是您要解决的问题吗?如果没有,您能否以简单的数学表达式形式清楚地说明您想要最小化的决策变量、约束和目标函数?

© www.soinside.com 2019 - 2024. All rights reserved.