需要审查计划模型逻辑,创建约束的建议以及语法错误的修复方法

时间:2019-05-09 14:19:48

标签: constraints mathematical-optimization modeling linear-programming minizinc

我正在为Mini Zinc中的混合设施创建调度模型。我之前曾问过类似的问题,但此后我一直在进步。我将总结一下我认为现有模型应该做的事情,如果有人可以纠正我所犯的任何逻辑或语法错误,那将非常有帮助。该模型当前错误,带有“预期文件结尾”的多个实例。与我发现的其他几个依赖序列的调度模型相比,这似乎简化了。在下面,您将找到注释的模型代码,其中包括我对每一行的理解。

除了逻辑和语法概述之外,我还在此模型中寻求“缺失约束”的帮助,该模型要求混合数组[y]所含整数不得超过每个混合的声明整数

此模型的显着未来目标包括自动生成blendcost矩阵,将给定开始日期的时间表数组输出到代表工作日的5列矩阵中,并显示与混和编号相对应的混和名称。

enum Blends = { A, B, C, D, E, F};
%Establish names for each blend and link them to their order number.

int: nb = count([Blends])
%Count the number of blends, plan to use later.

int: qA; %Error: syntax error, unexpected int, expecting end of file
int: qB;
int: qC;
int: qD;
int: qE;
int: qF;
int: sb;
%Call for inputs of the quantity of each of the blends needed, as well as the number/letter of the blend currently in the machine.

int: mc = qA + qB + qC + qD + qE + qF;
%Sum the blend quantities to determine total number of blends

[Blendcost] : [|1,2,2,2,2,2,|1,1,1,1,1,1,|1,1,1,1,1,1,|2,2,2,1,2,2,|1,1,1,1,1,1,|1,1,1,1,1,1,|]; %Error: syntax error, unexpected [|, expecting identifier
%Establishes a blend cost matrix, 6X6 depicting the transition costs from any blend A-F to any other blend A-F

array [Blends] of int: 1..6;
%Is this line needed to establish the link between A/1, B/2, C/3 etc;? Or is that taken care of when Blends is enumerated?

array [0..mc] of var 1..6: y;
%Create an array from 0 to the number of blends with potential values from 1-6, corresponding to the blend numbers.


%Missing constraint: [y] can contain no more than the quantity of each blend declared above, except for the blend declared in the starting blend, which will be allowed that blend quantity + 1


constraint y(0) = sb
%y(0) is set equal to the starting blend Letter/Number Defined earlier, used to determine the first transitionary cost.

array [1..mc] of int: x(i); %Error: syntax error, unexpected array, expecting end of file
%Create an array from 1 to number of blends, which will be filled with the transition costs in response to variations in y

constraint forall(i in x)(x(i) = Blendcost(y(i-1),y(i)))
%For each space in x, x will equal the blend cost value obtained from the previous blend in the y array vs the next blend in the y array

solve minimize sum (x); %Error: syntax error, unexpected solve, expecting end of file
%Solves this model attempting to minimize the sum of the x array, which should be filled with the transition costs. 

show(y):
%Print the final array of blend numbers that has minimized blend cost transition.
%Error: unexpected end of file, expecting identifier.

1 个答案:

答案 0 :(得分:1)

这是运行的CP模型的基本版本(假设有一些需求q):

enum BLEND = { A, B, C, D, E, F};

array[BLEND] of int: q = [1, 2, 4, 6, 8, 1];

array[BLEND, BLEND] of int: Blendcost = 
[|1,2,2,2,2,2|1,1,1,1,1,1|1,1,1,1,1,1|2,2,2,1,2,2|1,1,1,1,1,1|1,1,1,1,1,1|];

int: mc = sum(q);

array[1..mc] of var BLEND: y;

include "global_cardinality.mzn";
constraint global_cardinality(y, BLEND, q);

var int: obj = sum(p in 1..mc-1)(Blendcost[y[p],y[p+1]]) + 1;

array[int] of var opt BLEND: day = [y[p] | p in 1..mc-1, q in 1..max(Blendcost) where q <= Blendcost[y[p],y[p+1]]] ++ [y[mc]];

array[int] of var opt int: wash = [bool2int(q > 1) | p in 1..mc-1, q in 1..max(Blendcost) where q <= Blendcost[y[p],y[p+1]]] ++ [0];

solve minimize obj;

output  ["obj=\(obj)\n"] ++
["day=\n"] ++ [
  show(day[d]) ++ if fix(wash[d]) > 0 then "W" else "" endif ++ " " ++
  if d mod 5 = 0 then "\n" else "" endif | d in 1..length(day)
] ++ ["\nmc=\(mc)\n"] ++ ["y=\(y)\n"] ++ ["wash=\(wash)\n"]

查看https://www.minizinc.org/doc-2.2.3/en/lib-globals.html#counting-constraints,了解计数约束的替代版本。

对于较大的实例,MIP模型可能显示出更好的性能。