1

i have some doubts related to a CPLEX code that i'm trying to write. The code itself (model) seems to be well written, but when it comes to fill the data i have an error. NOTE: there are no constraints in the model because i'm trying to make a trial of the model just to see that it works.

Here is the code:

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);
};

Here is the data:

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>]

*NOTE: in each gap of the arc before the travel time (last gap value=5) must be the data from departurenode, arrival node and startingnode but it's not shown and i don't know why *

Another doubt: regarding the constraints of the model i don't know how to write them into CPLEX.

1) starttime <= arrivaltime <= endtime //(for each arrivalnode)

2) x * (arrivaltime (node i) + traveltime) <= arrivaltime (node j)

3) Initialize the variable arrivaltime for each truck to 0. (at the beginning of the simulation)

4)The demand for each arrival node has to be equal to the sum of (the arcs chosen * capacity of the truck)

Thank you so much.

rkersh
  • 4,447
  • 2
  • 22
  • 31
user395496
  • 13
  • 2

1 Answers1

0

Let me help you with the syntax.

In the .mod

Comment:

//int capacity [truck]=...; // capacity of a truck

since that's not defined in the .dat, and write:

float cost [arcs][truck]=...;

since that one is in the .dat.

The .dat should be changed into:

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]];

and then you will be able to run and then improve.

halfer
  • 19,824
  • 17
  • 99
  • 186
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
  • thank you very much Alex, your answer has been so useful!! Regarding the constraints do you have any idea how to write them in CPLEX? – user395496 Jul 17 '18 at 22:10
  • Hi, let me help you with the syntax. For constraint1 you could write forall(n in arrivalnode,t in truck) n.starttime<=arrivaltime[n][t]<=n.endtime; You could find many examples in the documentation – Alex Fleischer Jul 18 '18 at 05:08
  • Thank you for your answer, once again has been so useful!! Regarding the initialization of a decision variable i didn't find any information on the IBM manual (only info about how to initialize a set). Any ideas about that? – user395496 Jul 18 '18 at 13:12
  • A decision variable is unknown so you do not initialize a decision variable. You set bounds. For instance dvar int x in 0..2; means x should be between 0 and 2 (0,1 or 2) – Alex Fleischer Jul 18 '18 at 15:41
  • i tried to write the constraints 2 and 4 but i got some syntax errors: 2) forall (n in arrivalnode,t in truck, a in arcs) x [a,t] * (arrivaltime [n][t] + a.traveltime) <= arrivaltime [n][t]; // 4) forall (n in arrivalnode, a in arcs, t in truck) n.demand= sum (x [a,t] * capacity [t]); // any ideas to solve them? Thanks in advance – user395496 Jul 18 '18 at 17:43
  • forall (n in arrivalnode) n.demand== sum (a in arcs,t in truck)(x [a,t] * capacity [t]); // the demand for each node has to be equal to the sum of the //arcs chosen multiplied by the capacity of the truck (CONSTRAINT 2) – Alex Fleischer Jul 18 '18 at 18:44
  • so I understand from your message that the only error was on the constraint 4 right? When i write the constraint 2 in CPLEX apparently i don't get any problems on the syntax but i don't know if the way i wrote the constraint applies for what I really want to describe. – user395496 Jul 18 '18 at 23:34