在 GLPK 中将负载分散到不同的等成本变量上
Spread load on different equal cost variables in GLPK
在 GLPK 中是否可以将解决方案分散到多个等成本变量上?
假设我在 myprog 中有这段代码:
from pymprog import *
begin()
loads = var('loads', 3)
load_cost = par('load_cost', [10, 10, 10])
sum (loads[i] for i in range(len(loads))) >= 200
for i in range(len(loads)) :
loads[i] <= 100
minimize (sum (load_cost[i] * loads[i] for i in range(len(loads))))
solve()
end()
是否可以要求求解器将三个负载 return 66、66、66 而不是 100、100、0?
以下代码片段使用 gekko 优化包解决了您的问题。它会产生您想要的解决方案“66、66、66”:
from gekko import GEKKO
import numpy as np
#Initialize Model
m = GEKKO()
#initialize variables
x1,x2,x3 = [m.Var() for i in range(3)]
c1,c2,c3 = [m.Param(value=10) for i in range(3)]
#initial values
x1.value = 1
x2.value = 1
x3.value = 1
# lower bounds
x1.lower = 0
x2.lower = 0
x3.lower = 0
# upper bounds
x1.upper = 100
x2.upper = 100
x3.upper = 100
#Equations
m.Equation(x1+x2+x3>=200)
#Objective
m.Obj(x1*c1+x2*c2+x3*c3)
#Set global options
m.options.IMODE = 3 #steady state optimization
#Solve simulation
m.solve()
#Results
print('')
print('Results')
print('x1: ' + str(x1.value))
print('x2: ' + str(x2.value))
print('x3: ' + str(x3.value))
但是,如果您选择不同的初始值,求解器可能会找到负载分布不均的其他解。要在负载上强制平均分配,您应该考虑通过添加惩罚项来修改 objective 函数:
m.Obj(x1*c1+x2*c2+x3*c3+x1*x1+x2*x2+x3*x3)
使用此 objective 函数,无论您使用什么初始值,结果始终是所需的结果。
在 GLPK 中是否可以将解决方案分散到多个等成本变量上?
假设我在 myprog 中有这段代码:
from pymprog import *
begin()
loads = var('loads', 3)
load_cost = par('load_cost', [10, 10, 10])
sum (loads[i] for i in range(len(loads))) >= 200
for i in range(len(loads)) :
loads[i] <= 100
minimize (sum (load_cost[i] * loads[i] for i in range(len(loads))))
solve()
end()
是否可以要求求解器将三个负载 return 66、66、66 而不是 100、100、0?
以下代码片段使用 gekko 优化包解决了您的问题。它会产生您想要的解决方案“66、66、66”:
from gekko import GEKKO
import numpy as np
#Initialize Model
m = GEKKO()
#initialize variables
x1,x2,x3 = [m.Var() for i in range(3)]
c1,c2,c3 = [m.Param(value=10) for i in range(3)]
#initial values
x1.value = 1
x2.value = 1
x3.value = 1
# lower bounds
x1.lower = 0
x2.lower = 0
x3.lower = 0
# upper bounds
x1.upper = 100
x2.upper = 100
x3.upper = 100
#Equations
m.Equation(x1+x2+x3>=200)
#Objective
m.Obj(x1*c1+x2*c2+x3*c3)
#Set global options
m.options.IMODE = 3 #steady state optimization
#Solve simulation
m.solve()
#Results
print('')
print('Results')
print('x1: ' + str(x1.value))
print('x2: ' + str(x2.value))
print('x3: ' + str(x3.value))
但是,如果您选择不同的初始值,求解器可能会找到负载分布不均的其他解。要在负载上强制平均分配,您应该考虑通过添加惩罚项来修改 objective 函数:
m.Obj(x1*c1+x2*c2+x3*c3+x1*x1+x2*x2+x3*x3)
使用此 objective 函数,无论您使用什么初始值,结果始终是所需的结果。