“如何对朱莉娅进行特定时期的总和?”

时间:2019-03-27 22:03:09

标签: julia julia-jump

我正在制定数学程序,现在约束中的求和符号有问题。我想在规划期间的特定时期内进行总结,我尝试了一些,但朱莉娅报告了错误消息。

数学公式看起来像这样: constraint1

和这个:constraint2

这是我尝试过的一些代码:

Horizon = 12
Section = 5
TBetPM = [4 6 9 8 5]
LPM = [1 4 5 4 4]
MaxPM = [9 8 7 10 6]
PrevPM = [3 3 2 5 2]

tam=zeros(Float64,1,5)
for i=1:Section
tam[i] = TBetPM[i]-LPM[i]
end

tar = zeros(Float64,1,5)
for i=1:Section
tar[i] = Pi[i]*(MaxPM[i]-PrevPM[i])-LPM[i]
end

@constraint(mod, [i=1:Section],
sum(m[i,t] for t=1:Horizon if t<=tam[i]) >= 1
)
@constraint(mod, [i=1:Section],
sum(r[i,t] for t=1:Horizon if t<=tar[i]) >= 1
)

我也尝试过这些方法,但都无法正常工作

@constraint(mod, [i=1:Section],
sum(m[i,t] for t=1:tam[i]) >= 1
)
@constraint(mod, [i=1:Section],
sum(r[i,t] for t=1:tar[i]) >= 1
)

在此先感谢您提供所有答案:)

2 个答案:

答案 0 :(得分:0)

您在mod宏中使用了模型@constraint。尚未定义。如果使用JuMP,则必须创建它。

using JuMP
mod = Model()

您预先将tamtar初始化为大小为1x5(矩阵)的两个维度数组。我认为您在将它们作为向量访问时需要一个一维数组:

tam = zeros(Float64,5)
tar = zeros(Float64,5)

您没有在模型中定义变量mr

 @variable(mod, m[i=1:Section, t=1:Horizon])
 @variable(mod, r[i=1:Section, t=1:Horizon])

最后,您可能希望JuMP解决您的模型,可以通过以下方式完成:

using GLPK # feel free here to use the solver you prefer 
optimize!(mod, with_optimizer(GLPK.Optimizer))

并打印解决方案:

 if termination_status(mod) == MOI.OPTIMAL
     optimal_solution = value.(m), value.(r)
     optimal_objective = objective_value(mod)

     @show optimal_solution
     @show optimal_objective
 else
     error("The model was not solved correctly.")
 end

整个工作代码(Julia v1.1.0,JuMP v0.19.0,GPLK v0.9.1):

Horizon = 12
Section = 5
TBetPM = [4 6 9 8 5]
LPM = [1 4 5 4 4]
MaxPM = [9 8 7 10 6]
PrevPM = [3 3 2 5 2]


using JuMP
using GLPK # feel free here to use the solver you prefer

mod = Model()

tam = zeros(Float64,5)
for i in 1:Section
    tam[i] = TBetPM[i]-LPM[i]
end

tar = zeros(Float64,5)
for i=1:Section
    tar[i] = pi*(MaxPM[i] - PrevPM[i]) - LPM[i]
end


@variable(mod, m[i=1:Section, t=1:Horizon])
@variable(mod, r[i=1:Section, t=1:Horizon])

@constraint(mod, [i=1:Section],
    sum(m[i,t] for t in 1:Horizon if t <= tam[i]) >= 1)
@constraint(mod, [i=1:Section],
    sum(r[i,t] for t in 1:Horizon if t <= tar[i]) >= 1)


# Solve model and printing solution
optimize!(mod, with_optimizer(GLPK.Optimizer))
if termination_status(mod) == MOI.OPTIMAL
    optimal_solution = value.(m), value.(r)
    optimal_objective = objective_value(mod)

    @show optimal_solution
    @show optimal_objective
else
    error("The model was not solved correctly.")
end

我不知道是否是由于复制/粘贴或其他原因,但是即使不是强制性的,也应该缩进代码:p

答案 1 :(得分:0)

J.Khamphousone,谢谢您的答复。

这是我尝试过的完整代码:

using JuMP, CPLEX, Gurobi, GLPKMathProgInterface
sex = Model(solver = GurobiSolver())
Horizon = 12
Section = 5
TBetPM = [4 6 9 8 5]
TlastPM = [0 0 0 0 0]
MaxPM = [9 8 7 10 6]
PrevPM = [3 3 2 5 2]
taf1=zeros(Float64,1,5)
for i=1:Section
taf1[i] = TBetPM[i]-TlastPM[i]
end
tafr1 = zeros(Float64,1,5)
for i=1:Section
tafr1[i] = TBetPM[i]*(MaxPM[i]-PrevPM[i])-TlastPM[i]
end
tafr = zeros(Float64,1,5)
for i=1:Section
tafr[i] = TBetPM[i]*MaxPM[i]
end
mdur = [9 6 8 10 3]
rdur = [18 16 23 16 12]
Maxdur = 24
mcost = [2 6 5.5 4 4]
rcost = [6 15 20 18 25]
scost = [0.6 1.17 0.81 0.66 1.4]
pcost = 2
ccost = 0.001
Ncus = 100
@variable(sex, m[1:Section,1:Horizon]>=0, Bin) # 1 if section s maintain in week h
@variable(sex, r[1:Section,1:Horizon]>=0, Bin) # 1 if section s renew in week h
@variable(sex, p[1:Horizon]>=0,Bin) #1 if possession in week h

@objective(sex, Min, sum(pcost*p[t] for t=1:Horizon)+
sum(mcost[i]*m[i,t] for t=1:Horizon, i=1:Section)+
sum(rcost[i]*r[i,t] for t=1:Horizon, i=1:Section)
)

#select first maintenance time (correct)
@constraint(sex, [i=1:Section],
sum(m[i,t] for t=1:Horizon if t==taf1[i]) >= 1
)
#select next maintenance time (correct)
@constraint(sex, [i=1:Section, k=1:(Horizon-TBetPM[i])],
sum(m[i,t] for t=1+k:TBetPM[i]+k) >= 1
)
#select first renewal time
@constraint(sex, [i=1:Section],
sum(r[i,t] for t in tafr1[i]) >= 1
)
#select next renewal time
@constraint(sex, [i=1:Section, k=1:(Horizon-tafr[i])],
sum(r[i,t] for t=1+k:tafr[i]+k) >= 1
)
# if there is maintenance, there is possession
@constraint(sex, [i=1:Section, h=1:Horizon],
m[i,h] <= p[h]
)
# if there is renewal, there is possession
@constraint(sex, [i=1:Section, h=1:Horizon],
r[i,h] <= p[h]
)


solve(sex)
#print(sex)
mVal = getvalue(m)
for i=1:Section, t=1:Horizon
    if (mVal[i,t] ==1)
        println("section[$i] repair in period [$t]", )
    end
end
rVal = getvalue(r)
for i=1:Section, t=1:Horizon
    if (rVal[i,t] ==1)
        println("section[$i] renew in period [$t]", )
    end
end