knapsack optimization with dynamic variables - optimization

I am trying to solve an optimization problem, that it's very similar to the knapsack problem but it can not be solved using the dynamic programming.
The problem I want to solve is very similar to this problem:

indeed you may solve this with CPLEX.
Let me show you that in OPL.
The model (.mod)
{string} categories=...;
{string} groups[categories]=...;
{string} allGroups=union (c in categories) groups[c];
{string} products[allGroups]=...;
{string} allProducts=union (g in allGroups) products[g];
float prices[allProducts]=...;
int Uc[categories]=...;
float Ug[allGroups]=...;
float budget=...;
dvar boolean z[allProducts]; // product out or in ?
dexpr int xg[g in allGroups]=(1<=sum(p in products[g]) z[p]);
dexpr int xc[c in categories]=(1<=sum(g in groups[c]) xg[g]);
maximize
sum(c in categories) Uc[c]*xc[c]+
sum(c in categories) sum(g in groups[c]) Uc[c]*Ug[g]*xg[g];
subject to
{
ctBudget:
sum(p in allProducts) z[p]*prices[p]<=budget;
}
{string} solution={p | p in allProducts : z[p]==1};
execute
{
writeln("solution = ",solution);
}
The data .dat
categories={Carbs,Protein,Fat};
groups=[{Meat,Milk},{Pasta,Bread},{Oil,Butter}];
products=[
{Product11,Product12},{Product21,Product22,Product23},
{Product31,Product32},{Product41,Product42},
{Product51},{Product61,Product62}];
prices=[1,4,1,3,2,4,2,1,3,1,2,1];
// User 1
Uc=[1,1,0];
Ug=[0.8,0.2,0.1,1,0.01,0.6];
budget=3;
//User 2
//Uc=[1,1,0];
//Ug=[0.8,0.2,0.1,1,0.01,0.6];
//budget=2;
and this gives
solution = {"Product11" "Product21" "Product41"}

Related

CPLEX: SUM Operator not available for dexpr float

I am trying to program the following mixed program:
enter image description here
where both xo and xi are to be boolean, thou xi was declared as a float given its the initial condition of the system and CPLEX doesnt allow boolean variables not linked to decision variables.
The markers cc and p are both strings collected from the Excel file.
Cplex is consistently giving the error on the C2 declaration as we see below:
//Standard Variables Initialization
{string} C_PN = ...;
{string} C_CC = ...;
{string} D_PN = ...;
{string} W_CC = ...;
{string} Xi_PN = ...;
{string} Xi_CC = ...;
float u = 0.9; //utilization factor
float C[C_PN][C_CC] = ...;
float D[Xi_PN] = ...;
float Profit[D_PN] = ...;
float Hours[D_PN] = ...;
float W[W_CC] = ...;
float Eff[W_CC] = ...;
int Xi[Xi_PN][Xi_CC]= ...;
float mu[D_PN] = ...;
execute{
for (p in D_PN) mu[p] = Hours[p]/Demand[p] ;//Hours/piece
}
//Decision Variables Initialization
dvar float+ h[Xi_PN][Xi_CC] ; //Produced Parts
dvar boolean Xo[Xi_PN][Xi_CC] ; //flag for pn x cc opening
//Linear optimization problem (linear program)
minimize
sum(p in C_PN, cc in C_CC) 1 + //(D[p] - h[p,cc])*Profit[p] +
sum(p in C_PN, cc in C_CC) 1;//(Xo[p,cc] + Xi[p,cc])*C[p,cc];
//Linear optimization problem (linear program)
subject to{
C1:forall(p in Xi_PN){
sum(cc in Xi_CC){ (Xo[p,cc] + Xi[p,cc])*h[p,cc]} <= D[p] ;} //Demand Constraint
C2:forall(cc in C_CC){
sum(p in C_PN) (Xo[p,cc] + Xi[p,cc])*h[p,cc]*mu[p] <= W[cc]*u*Eff[cc] ;} //Capacity Constraint
C3:forall(p in C_PN, cc in C_CC) Xo[p,cc] + Xi[p,cc] <= 1 ; //Only one tool per CC for each PN
}
As the error is not helpfull to understand the root cause, would any of you guys know more about what could be related to it?
Thanks a lot,
You should change
C1:forall(p in Xi_PN){
sum(cc in Xi_CC){ (Xo[p,cc] + Xi[p,cc])*h[p,cc]} <= D[p] ;} //Demand Constraint
into
C1:forall(p in Xi_PN){
sum(cc in Xi_CC) (Xo[p,cc] + Xi[p,cc])*h[p,cc] <= D[p] ;} //Demand Constraint

Using a solution from a model as an input to another one and Outputting each Solution Separately

I'm solving an optimization problem in which I need the result from one model to be used as a input in another model for 180 iterations. I'm using CPLEX with OPL language without any addon.
I tried to save the values from one model into an Excel file and reading those into the next model but since I'm going to do this 180 times I am worried I will make an error and have to restart or not even know I made an error.
Is it possible to have this run for 180 iterations and input each iteration's solution separately?
You can rely on warmstart for that.
2 simple examples in easy OPL
Warm start from a file:
include "zoo.mod";
main {
var filename = "c:/temp/mipstart.mst";
thisOplModel.generate();
cplex.readMIPStarts(filename);
cplex.solve();
writeln("Objective: " + cplex.getObjValue());
}
or with API
int nbKids=300;
// a tuple is like a struct in C, a class in C++ or a record in Pascal
tuple bus
{
key int nbSeats;
float cost;
}
// This is a tuple set
{bus} pricebuses=...;
// asserts help make sure data is fine
assert forall(b in pricebuses) b.nbSeats>0;assert forall(b in pricebuses) b.cost>0;
// To compute the average cost per kid of each bus
// you may use OPL modeling language
float averageCost[b in pricebuses]=b.cost/b.nbSeats;
// Let us try first with a naïve computation, use the cheapest bus
float cheapestCostPerKid=min(b in pricebuses) averageCost[b];
int cheapestBusSize=first({b.nbSeats | b in pricebuses : averageCost[b]==cheapestCostPerKid});
int nbBusNeeded=ftoi(ceil(nbKids/cheapestBusSize));
float cost0=item(pricebuses,<cheapestBusSize>).cost*nbBusNeeded;
execute DISPLAY_Before_SOLVE
{
writeln("The naïve cost is ",cost0);
writeln(nbBusNeeded," buses ",cheapestBusSize, " seats");
writeln();
}
int naiveSolution[b in pricebuses]=
(b.nbSeats==cheapestBusSize)?nbBusNeeded:0;
// decision variable array
dvar int+ nbBus[pricebuses];
// objective
minimize
sum(b in pricebuses) b.cost*nbBus[b];
// constraints
subject to
{
sum(b in pricebuses) b.nbSeats*nbBus[b]>=nbKids;
}
float cost=sum(b in pricebuses) b.cost*nbBus[b];
execute DISPLAY_After_SOLVE
{
writeln("The minimum cost is ",cost);
for(var b in pricebuses) writeln(nbBus[b]," buses ",b.nbSeats, " seats");
}
main
{
thisOplModel.generate();
// Warm start the naïve solution
cplex.addMIPStart(thisOplModel.nbBus,thisOplModel.naiveSolution);
cplex.solve();
thisOplModel.postProcess();
}

Transition matrix not included in my solution of my scheduling problem in CPLEX CP

My distance matrix in my no overlap constraint does not seem to work in my model outcome. I have formulated the distance matrix by means of a tuple set. I have tried this in 2 different ways as can be seen in the code. Both tuple sets seem to be correct and the distance matrix is added in the noOverlap constraint for the dvar sequence.
Nevertheless I do not see the added transition distance between products in the optimal results. Jobs seem to continue at the same time when a job is finished. Instead of waiting for a transition time. I would like this transition matrix to hold both for machine 1 and machine 2.
Could someone tell me what I did wrong in my model formulation? I have looked into the examples, but they seem to be constructed in the same way. So I do not know what I am doing wrong.
mod.
using CP;
// Number of Machines (Packing + Manufacturing)
int nbMachines = ...;
range Machines = 1..nbMachines;
// Number of Jobs
int nbJobs = ...;
range Jobs = 1..nbJobs;
int duration[Jobs,Machines] = ...;
int release = ...;
int due = ...;
tuple Matrix { int job1; int job2; int value; };
//{Matrix} transitionTimes ={<1,1,0>,<1,2,6>,<1,3,2>,<2,1,2>,<2,2,0>,<2,3,1>,<3,1,2>,<3,2,3>,<3,3,0>};
{Matrix} transitionTimes ={ <i,j, ftoi(abs(i-j))> | i in Jobs, j in Jobs };
dvar interval task[j in Jobs] in release..due;
dvar interval opttask[j in Jobs][m in Machines] optional size duration[j][m];
dvar sequence tool[m in Machines] in all(j in Jobs) opttask[j][m];
execute {
cp.param.FailLimit = 5000;
}
// Minimize the max timespan
dexpr int makespan = max(j in Jobs, m in Machines)endOf(opttask[j][m]);
minimize makespan;
subject to {
// Each job needs one unary resource of the alternative set s (28)
forall(j in Jobs){
alternative(task[j], all(m in Machines) opttask[j][m]);
}
forall(m in Machines){
noOverlap(tool[m],transitionTimes);
}
};
execute {
writeln(task);
};
dat.
nbMachines = 2;
nbJobs = 3;
duration = [
[5,6],
[3,4],
[5,7]
];
release = 1;
due = 30;
``
You should specify interval types for each sequence.
In your case, the type is the job id:
int JobId[j in Jobs] = j;
dvar sequence tool[m in Machines] in all(j in Jobs) opttask[j][m] types JobId;

Array issue with endbefore start in CPLEX

I am trying to add an Endbeforestartconstraint to my contrained programming problem. However, I receive an error saying that my end beforestart is not an array type. I do not understand this as I almost copied the constraint and data from the sched_seq example in CPLEX, I only changed it to integers.
What I try to accomplish with the constraint, is that task 3 and task 1 will be performed before task 2 will start.
How I can fix the array error for this constraint?
Please find below the relevant parts of my code
tuple Precedence {int pre;int post;};
{Precedence} Precedences = {<3,2>,<1,2>};
dvar interval task[j in Jobs] in release..due;
dvar interval opttask[j in Jobs][m in Machines] optional size duration[j][m];
dvar sequence tool[m in Machines] in all(j in Jobs) opttask[j][m]
dexpr int makespan = max(j in Jobs, m in Machines)(endOf(opttask[j][m]));
minimize makespan;
subject to {
// Each job needs one unary resource of the alternative set s (28)
forall(j in Jobs){
alternative(task[j], all(m in Machines) opttask[j][m]);
}
// No overlap on machines
forall(j in Jobs)
forall(p in Precedences)
endBeforeStart(opttask[j][p.pre],opttask[j][p.post]);
forall(m in Machines){
noOverlap(tool[m],transitionTimes);
}
};
execute {
writeln(task);
dat.
nbMachines = 2;
nbJobs = 3;
duration = [
[5,6],
[4,4],
[5,8]
];
release = 1;
due = 30;
There are several errors in your model, on ranges or on inverted indices.
Also, next time, please post a complete program showing the problem, not just a partial one, this may help you to get quicker answers.
A corrected program:
using CP;
int nbMachines = 2;
int nbJobs = 3;
range Machines = 0..nbMachines-1;
range Jobs = 0..nbJobs-1;
int duration[Jobs][Machines] = [
[5,6],
[4,4],
[5,8]
];
int release = 1;
int due = 30;
tuple Precedence {int pre;int post;};
{Precedence} Precedences = {<2,1>,<0,1>};
dvar interval task[j in Jobs] in release..due;
dvar interval opttask[j in Jobs][m in Machines] optional size duration[j][m];
dvar sequence tool[m in Machines] in all(j in Jobs) opttask[j][m];
dexpr int makespan = max(j in Jobs, m in Machines)(endOf(opttask[j][m]));
minimize makespan;
subject to {
// Each job needs one unary resource of the alternative set s (28)
forall(j in Jobs){
alternative(task[j], all(m in Machines) opttask[j][m]);
}
// No overlap on machines
forall(m in Machines)
forall(p in Precedences)
endBeforeStart(opttask[p.pre][m],opttask[p.post][m]);
};
execute {
writeln(task);
}
You must have values in p.pre or p.post that are outside of the array indexing range.

Declaration of 3D Decision Variable

I need to declarate a 3 dimensional decision variable, which is defined according to the following:
x[m][p][q] in {0,1} with m in M, p in P(m) and q in Q(m,p)
dvar boolean x[M][P][Q] does not work.
Is there a possiblity to define it similar to a constraint, like:
forall(m in M, p in P(m) and q in Q(m,p))
x[m][p][q] in {0,1}
or something?
Regards
array variable indexer size - 3 ways : union , tuple set, decision expression
in Simple OPL CPLEX
could help
/*
Variable indexer size:
we'd like to be able to write
dvar int+ nbBus[s in scenarii][sizes[s]];
but we get an error "Variable indexer size not allowed"
So what can we do ?
We saw 2 options:
1) Rely on a tuple set that contains all options
instead of a multi dimension array
2) For the variable dimension use the union of all options
1) Is good but the drawback is that relying on a tuple set makes the
constraint harder to read for a human being
2) Is good but a bit suboptimal since we use options that are useless and
consume memory for no gain.
We can mix 1) and 2) with decision expressions
*/
int nbKids=300;
tuple busscenario
{
key int nbSeats;
key int scenario;
float cost;
}
{busscenario} busscenarii={<40,1,500>,<30,1,400>,<30,2,410>,<35,2,440>,<40,2,520>};
{int} scenarii={i.scenario | i in busscenarii};
{int} sizeBusesPerScenario[scen in scenarii]={i.nbSeats | i in busscenarii : i.scenario==scen};
{int} busSizes=union(scen in scenarii) sizeBusesPerScenario[scen];
// decision variable array with variable size
dvar int+ nbBus2[busscenarii];
// decision expression array with variable size
dexpr int nbBus[sc in scenarii][b in busSizes]=nbBus2[<b,sc>];
dexpr float cost[sc in scenarii][b in busSizes]=item(busscenarii,<b,sc>).cost;
// objective
minimize
1/card(scenarii)*sum(sc in scenarii,b in sizeBusesPerScenario[sc])
cost[sc][b]*nbBus[sc][b];
// constraints
subject to
{
forall(sc in scenarii) sum(b in sizeBusesPerScenario[sc]) b*nbBus[sc][b]>=nbKids;
}
execute
{
for(sc in scenarii)
{
writeln("scenario ",sc);
for(var b in sizeBusesPerScenario[sc]) writeln(nbBus[sc][b]," buses ",b," seats" );
writeln();
}
}
/*
which gives
// solution (optimal) with objective 3820
scenario 1
6 buses 40 seats
2 buses 30 seats
scenario 2
0 buses 30 seats
4 buses 35 seats
4 buses 40 seats
*/