我正在使用if—else的条件语句测试gekko中的m.if3函数,但我得到了两个不同的输出.我从下面的代码得到的最佳网站是12.我用if—else语句插入到下一个代码中,以确保成本匹配,但它没有匹配.我是否使用了if3/if2错误?前5个月的enrollment_rate为0.1,其余45个月切换为0.3.
我得到了不同的输出,即使我在两个过程中做同样的事情.
我try 了所有的方法,从使用if—else语句到使用if2语句.
我正在使用if—else的条件语句测试gekko中的m.if3函数,但我得到了两个不同的输出.我从下面的代码得到的最佳网站是12.我用if—else语句插入到下一个代码中,以确保成本匹配,但它没有匹配.我是否使用了if3/if2错误?前5个月的enrollment_rate为0.1,其余45个月切换为0.3.
我得到了不同的输出,即使我在两个过程中做同样的事情.
我try 了所有的方法,从使用if—else语句到使用if2语句.
不需要if2
或if3
函数,因为切换参数duration-5
是一个常数值,它不是Gekko变量的函数.就像验证脚本一样,这两个部分可以分别计算,然后相加,得到总成本和患者数量.
from gekko import GEKKO
m = GEKKO(remote=False)
# parameters
cost_p = 9
cost_s = 12
var1 = 50
duration = 50
x = m.Var(integer=True, lb=1)
rate1 = 0.3
rate2 = 0.1
cost1 = m.Intermediate((rate1 * cost_p * 5 + cost_s) * x)
cost2 = m.Intermediate((rate2 * cost_p * (duration-5) + cost_s) * x)
cost = m.Intermediate(cost1+cost2)
countp1 = m.Intermediate(rate1 * 5 * x)
countp2 = m.Intermediate(rate2 * (duration-5) * x)
p_count = m.Intermediate(countp1+countp2)
m.Minimize(cost)
m.Equation(p_count >= var1)
m.options.SOLVER = 1 # for MINLP solution
m.solve(disp=False)
num_sites = x.value[0]
print(f'num_s = {num_s}')
print(f'cost: {cost.value[0]}')
print(f'p_count: {p_count.value[0]}')
最佳解决方案是:
num_s = 9.0
cost: 810.0
p_count: 54.0
解决方案验证符合以下答案:
# Solution validation
# Parameters
cost_s = 9
cost_p = 12
num_p = 50
duration = 50
if duration > 5:
rate = 0.1
else:
rate = 0.3
x = 9
cost1 = (0.1 * cost_p * 45 + cost_s) * x
cost2 = (0.3 * cost_p * 5 + cost_s) * x
cost = cost1 + cost2
countp1 = 0.3 * 5 * x
countp2 = 0.1 * 45 * x
countp = countp1 + countp2
print(f'cost (validation): {cost}')
print(f'count (validation): {countp}')