Originally posted by: AMM1
hello Mr. Alex ,
i have a problem in my cp model
i need to add a fixed cost to reduce the number of used vehicles
how can i add the fixed cost of the vehicle?
my CP model:
using CP;
tuple Position {
key int id;
int x;
int y;
};
{Position} Positions = ...;
tuple Demand {
key int id;
int q;
int s;
};
{Demand} Demands = ...;
int n = ...;
int m =...; // Number of trucks
int K = ...;
//tuple distance { int c1; int c2; int dista; };
tuple timewin {
key int l1;
key int l2;
key int l3;
}
timewin lowerbound[d in Demands]=...;
tuple timewin2 {
key int u1;
key int u2;
key int u3;
}
timewin2 upperbound[d in Demands]=...;
int ld=...;
int ud=...;
{int} Customers = { p.id | p in Positions };
dvar interval visit [d in Demands] size d.s;
dvar interval tvisit[d in Demands][t in 1..m] optional (101>d.id>=1) size d.s;
dvar sequence truck[t in 1..m] in all(d in Demands) tvisit[d][t] types all(d in Demands) d.id;
tuple triplet { int c1; int c2; int d; };
{triplet} Dist = {
<p1.id,p2.id,ftoi(round(sqrt(pow(p2.x-p1.x,2)+pow(p2.y-p1.y,2))))> | p1, p2 in Positions };
int distMatrix[p1 in Positions][p2 in Positions] = ftoi(round(sqrt(pow(p2.x-p1.x,2)+pow(p2.y-p1.y,2))));
int distMatrixById[p1 in Customers][p2 in Customers] = distMatrix[<p1>][<p2>];
dexpr float truckDistance[t in 1..m] = sum(d in Demands) distMatrixById[d.id][typeOfNext(truck[t], tvisit[d][t], d.id, d.id)];
dexpr float totalDistance = sum(t in 1..m) truckDistance[t];
execute {
writeln(Dist);
};
execute {
cp.param.TimeLimit =3600;
cp.param.TimeMode = "ElapsedTime";
cp.param.Workers = 1;
cp.param.NoOverlapInferenceLevel = "Medium";
cp.param.ElementInferenceLevel = "Low";
}
minimize totalDistance;
constraints {
forall ( t in 1..m) {
forall (d in Demands: 101>d.id>=1) {
upperbound[d].u1 >=startOf(visit[d])>= lowerbound[d].l1 || upperbound[d].u2>=startOf(visit[d])>= lowerbound[d].l2||upperbound[d].u3>=startOf(visit[d])>= lowerbound[d].l3;
}
forall(t in 1..m) {
noOverlap(truck[t],Dist);
first(truck[t],tvisit[<0>][t]); // Truck t starts at depot
last (truck[t],tvisit[<101>][t]); // Truck t ends at depot
//startOf(tvisit[<101>][t])<=ud;
sum(d in Demands) presenceOf(tvisit[d][t])*d.q <= K;
}
forall(d in Demands: 101>d.id>=1) {
// ud>=startOf(visit[d])>=ld;
alternative(visit[d], all(t in 1..m) tvisit[d][t]); // Truck selection
}
}
#DecisionOptimization#OPLusingCPLEXOptimizer