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:
Asked
Active
Viewed 502 times
1 Answers
1
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"}

Martijn Pieters
- 1,048,767
- 296
- 4,058
- 3,343

Alex Fleischer
- 9,276
- 2
- 12
- 15
-
Hello thank you a dozen for your valuable feedback, it's saving me a lot of time. I have tried to download IBM CPLEX Op Studio (free ed) but unfortunately I couldn't, so I couldn't directly try the code (I am trying to convert this code to python to try it). So I have two questions, I couldn't understand line 11 (dexpr) if I understood correctly this line creates a boolean array xg, but is this an embedded if statement: the sum over all the values of z[p] of each group g should be greater than 1, than xg[g] is 0? Also do you have an idea of the algorithm solving this problem? Thank you again – sel Sep 04 '18 at 19:42
-
1Hi you can get free CPLEX community edition at https://www.ibm.com/products/ilog-cplex-optimization-studio that is part of https://www.linkedin.com/pulse/low-barrier-entry-optimization-through-cplex-alex-fleischer/ – Alex Fleischer Sep 05 '18 at 06:31
-
Yes I have used the link you have provided in your linkedin post, but because I am not in the US I couldn't download it, I have posted a question in the IBM Forum in order to solve this problem. I have used instead the IBM DropSolve OAAS as a temporary solution. And your code worked perfectly. Thank you so much. – sel Sep 05 '18 at 17:39
-
Hi. I hope you are still here. I have similar problem: I have my .mod file and can execute it on my personal computer with CPLEX IDE. However, this takes way much time and I want to execute in on my university’s HPC and it only allows us to use a remote console connection. So I need .lp equivalent of the above .mod file. How can I do that? Or is there any way to execute a .mod file (with the data file) on cplex console? Thanks. – WhoCares Jan 27 '22 at 18:54
-
Yes, you can use oplrun (command line for cplex) And many links at https://www.linkedin.com/pulse/low-barrier-entry-optimization-through-cplex-alex-fleischer/ – Alex Fleischer Jan 28 '22 at 08:05