即使代码没有错误,我也无法模拟 CPLEX 模型
I can't simulate a CPLEX model even when the code has no errors
我对我正在尝试编写的 CPLEX 代码有一些疑问。代码本身(模型)似乎写得很好,但是在填充数据时出现错误。 注意:模型中没有任何限制,因为我正在尝试对该模型进行试验,看看它是否有效。
代码如下:
using CP;
// NETWORK+PARAMETERS
int trucks=...; // set of trucks
range truck= 1..trucks;
int capacity [truck]=...; // capacity of a truck
tuple nodeinfo {
string name; // name of a node
int starttime; // available start time
int endtime; // available end time
float demand; // demand from a node
}
{nodeinfo} departurenode=...; //size=4
{nodeinfo} arrivalnode=...; //size=4
{nodeinfo} startingnode=...; //size=4
// OPTION 2: vector of nodes
//each node has tuple structure nodeinfo
//length of the vector is length of dataset
//read data from dataset
//void* node = new {nodeinfo} [k];
tuple arc {
nodeinfo departurenode; //departure node of an arc
nodeinfo arrivalnode; // arrival node of an arc
nodeinfo startingnode; // starting node of an arc
int traveltime; // travel time of an arc
}
{arc} arcs=...; //number of arcs (24)
float cost [arcs][truck]; //cost of using an arc by a truck
// option2: int arc[i in departurenode,j in arrivalnode,k in startingnode]=...; //arcs (size=24)
// how can i create a setof arcs taking into account the info from each arc??
// VARIABLES
dvar boolean x [arcs,truck]; // 1 if truck uses the arc, 0 otherwise (array of size 24x7)
dvar int+ arrivaltime [arrivalnode,truck]; //arrival time of a truck at a node (array size of 4x7)
// OBJECTIVE FUNCTION
dexpr float totalcost =
sum (i in arcs, j in truck) x [i,j] * cost [i,j];
minimize totalcost;
// CONSTRAINTS
subject to {}
execute {
writeln(arcs);
};
这是数据:
trucks= 2;
departurenode= [[A,0,10000,0],[B,0,10000,0],[C,0,10000,10],[D,0,10000,10]];
arrivalnode= [[A,0,10000,0],[B,0,10000,0],[C,0,10000,10],[D,0,10000,10]];
startingnode=[[A,0,10000,0],[B,0,10000,0],[C,0,10000,10],[D,0,10000,10]];
arcs= [[<A,0,10000,0>,<A,0,10000,0>,<C,0,10000,10>,5], [<A,0,10000,0>,<A,0,10000,0>,<D,0,10000,10>,5],
[<B,0,10000,0>,<B,0,10000,0>,<C,0,10000,10>,5], [<B,0,10000,0>,<B,0,10000,0>,<D,0,10000,10>,5],
[<C,0,10000,0>,<C,0,10000,0>,<A,0,10000,10>,5], [<C,0,10000,0>,<C,0,10000,0>,<B,0,10000,10>,5],
[<D,0,10000,0>,<D,0,10000,0>,<A,0,10000,10>,5], [<D,0,10000,0>,<D,0,10000,0>,<B,0,10000,10>,5]]
cost= [<1,1>,<1,1>,<1,1>,<1,1>,<1,1>,<1,1>,<1,1>,<1,1>]
*注意:在行程时间之前的弧的每个间隙中(最后一个间隙值=5)必须是来自departmentnode,arrival node和startingnode的数据但它没有显示,我没有知道为什么*
另一个疑问:关于模型的约束,我不知道如何将它们写入 CPLEX。
1) 开始时间 <= 到达时间 <= 结束时间 //(对于每个到达节点)
2) x * (到达时间 (节点 i) + 旅行时间) <= 到达时间 (节点 j)
3) 将每辆卡车的变量到达时间初始化为0。(在模拟开始时)
4)每个到达节点的需求必须等于(选择的弧*卡车容量)
非常感谢。
我来帮你语法。
在.mod
评论:
//int capacity [truck]=...; // capacity of a truck
因为 .dat
中没有定义,所以写:
float cost [arcs][truck]=...;
因为那个在 .dat
.
.dat 应更改为:
trucks= 2;
departurenode= {<A,0,10000,0>,<B,0,10000,0>,<C,0,10000,10>,<D,0,10000,10>};
arrivalnode= {<A,0,10000,0>,<B,0,10000,0>,<C,0,10000,10>,<D,0,10000,10>};
startingnode={<A,0,10000,0>,<B,0,10000,0>,<C,0,10000,10>,<D,0,10000,10>};
arcs= {<<A,0,10000,0>,<A,0,10000,0>,<C,0,10000,10>,5>, <<A,0,10000,0>,<A,0,10000,0>,<D,0,10000,10>,5>,
<<B,0,10000,0>,<B,0,10000,0>,<C,0,10000,10>,5>, <<B,0,10000,0>,<B,0,10000,0>,<D,0,10000,10>,5>,
<<C,0,10000,0>,<C,0,10000,0>,<A,0,10000,10>,5>, <<C,0,10000,0>,<C,0,10000,0>,<B,0,10000,10>,5>,
<<D,0,10000,0>,<D,0,10000,0>,<A,0,10000,10>,5>, <<D,0,10000,0>,<D,0,10000,0>,<B,0,10000,10>,5>};
cost= [[1,1],[1,1],[1,1],[1,1],[1,1],[1,1],[1,1],[1,1]];
然后你就可以运行再提高。
我对我正在尝试编写的 CPLEX 代码有一些疑问。代码本身(模型)似乎写得很好,但是在填充数据时出现错误。 注意:模型中没有任何限制,因为我正在尝试对该模型进行试验,看看它是否有效。
代码如下:
using CP;
// NETWORK+PARAMETERS
int trucks=...; // set of trucks
range truck= 1..trucks;
int capacity [truck]=...; // capacity of a truck
tuple nodeinfo {
string name; // name of a node
int starttime; // available start time
int endtime; // available end time
float demand; // demand from a node
}
{nodeinfo} departurenode=...; //size=4
{nodeinfo} arrivalnode=...; //size=4
{nodeinfo} startingnode=...; //size=4
// OPTION 2: vector of nodes
//each node has tuple structure nodeinfo
//length of the vector is length of dataset
//read data from dataset
//void* node = new {nodeinfo} [k];
tuple arc {
nodeinfo departurenode; //departure node of an arc
nodeinfo arrivalnode; // arrival node of an arc
nodeinfo startingnode; // starting node of an arc
int traveltime; // travel time of an arc
}
{arc} arcs=...; //number of arcs (24)
float cost [arcs][truck]; //cost of using an arc by a truck
// option2: int arc[i in departurenode,j in arrivalnode,k in startingnode]=...; //arcs (size=24)
// how can i create a setof arcs taking into account the info from each arc??
// VARIABLES
dvar boolean x [arcs,truck]; // 1 if truck uses the arc, 0 otherwise (array of size 24x7)
dvar int+ arrivaltime [arrivalnode,truck]; //arrival time of a truck at a node (array size of 4x7)
// OBJECTIVE FUNCTION
dexpr float totalcost =
sum (i in arcs, j in truck) x [i,j] * cost [i,j];
minimize totalcost;
// CONSTRAINTS
subject to {}
execute {
writeln(arcs);
};
这是数据:
trucks= 2;
departurenode= [[A,0,10000,0],[B,0,10000,0],[C,0,10000,10],[D,0,10000,10]];
arrivalnode= [[A,0,10000,0],[B,0,10000,0],[C,0,10000,10],[D,0,10000,10]];
startingnode=[[A,0,10000,0],[B,0,10000,0],[C,0,10000,10],[D,0,10000,10]];
arcs= [[<A,0,10000,0>,<A,0,10000,0>,<C,0,10000,10>,5], [<A,0,10000,0>,<A,0,10000,0>,<D,0,10000,10>,5],
[<B,0,10000,0>,<B,0,10000,0>,<C,0,10000,10>,5], [<B,0,10000,0>,<B,0,10000,0>,<D,0,10000,10>,5],
[<C,0,10000,0>,<C,0,10000,0>,<A,0,10000,10>,5], [<C,0,10000,0>,<C,0,10000,0>,<B,0,10000,10>,5],
[<D,0,10000,0>,<D,0,10000,0>,<A,0,10000,10>,5], [<D,0,10000,0>,<D,0,10000,0>,<B,0,10000,10>,5]]
cost= [<1,1>,<1,1>,<1,1>,<1,1>,<1,1>,<1,1>,<1,1>,<1,1>]
*注意:在行程时间之前的弧的每个间隙中(最后一个间隙值=5)必须是来自departmentnode,arrival node和startingnode的数据但它没有显示,我没有知道为什么*
另一个疑问:关于模型的约束,我不知道如何将它们写入 CPLEX。
1) 开始时间 <= 到达时间 <= 结束时间 //(对于每个到达节点)
2) x * (到达时间 (节点 i) + 旅行时间) <= 到达时间 (节点 j)
3) 将每辆卡车的变量到达时间初始化为0。(在模拟开始时)
4)每个到达节点的需求必须等于(选择的弧*卡车容量)
非常感谢。
我来帮你语法。
在.mod
评论:
//int capacity [truck]=...; // capacity of a truck
因为 .dat
中没有定义,所以写:
float cost [arcs][truck]=...;
因为那个在 .dat
.
.dat 应更改为:
trucks= 2;
departurenode= {<A,0,10000,0>,<B,0,10000,0>,<C,0,10000,10>,<D,0,10000,10>};
arrivalnode= {<A,0,10000,0>,<B,0,10000,0>,<C,0,10000,10>,<D,0,10000,10>};
startingnode={<A,0,10000,0>,<B,0,10000,0>,<C,0,10000,10>,<D,0,10000,10>};
arcs= {<<A,0,10000,0>,<A,0,10000,0>,<C,0,10000,10>,5>, <<A,0,10000,0>,<A,0,10000,0>,<D,0,10000,10>,5>,
<<B,0,10000,0>,<B,0,10000,0>,<C,0,10000,10>,5>, <<B,0,10000,0>,<B,0,10000,0>,<D,0,10000,10>,5>,
<<C,0,10000,0>,<C,0,10000,0>,<A,0,10000,10>,5>, <<C,0,10000,0>,<C,0,10000,0>,<B,0,10000,10>,5>,
<<D,0,10000,0>,<D,0,10000,0>,<A,0,10000,10>,5>, <<D,0,10000,0>,<D,0,10000,0>,<B,0,10000,10>,5>};
cost= [[1,1],[1,1],[1,1],[1,1],[1,1],[1,1],[1,1],[1,1]];
然后你就可以运行再提高。