Originally posted by: Torben-Ger-1988
Hey Guys,
my Knapsackproblem is looking right now as showed down there...
I'd like to change it a little bit. At the moment there is a limited capacity. I'd like to change it that there is an unlimited capacity and instead of V[] I'd like to get a nw decision var called m
Which means i'd like to put each think into my Knapsack for exactly 1 time and I'd like to know the value of all of my parts and the needed more capacity.
But i am not sure. Changed the structure and never received a proper result. Thanks for your help
import java.util.HashMap;
import ilog.concert.IloException;
import ilog.concert.IloLinearNumExpr;
import ilog.concert.IloNumVar;
import ilog.concert.IloNumVarType;
import ilog.cplex.IloCplex;
public class Fillit{
public static void main(String[] args) throws IloException {
// TODO Auto-generated method stub
double valuesObj[] = {4.0, 2.0, 3.0, 1.0};
double values2[][] = {{2,3},{2,6}};
IloCplex cplex = new IloCplex();
IloLinearNumExpr obj = cplex.linearNumExpr();
IloNumVar V[] = new IloNumVar[4];
/*
IloNumVar P[][] = new IloNumVar[4][4];
P[0][0] = cplex.numVar(0, Double.POSITIVE_INFINITY, IloNumVarType.Bool, "");
obj.addTerm(P[0][0], values2[0][0]);
*/
V[0] = cplex.numVar(0, Double.POSITIVE_INFINITY, IloNumVarType.Bool, "V( 0 )");
V[1] = cplex.numVar(0, Double.POSITIVE_INFINITY, IloNumVarType.Bool, "V( 1 )");
V[2] = cplex.numVar(0, Double.POSITIVE_INFINITY, IloNumVarType.Bool, "V( 2 )");
V[3] = cplex.numVar(0, Double.POSITIVE_INFINITY, IloNumVarType.Bool, "V( 3 )");
for(int index = 0; index < valuesObj.length; index++){
obj.addTerm(V[index], valuesObj[index]);
}
/*
obj.addTerm(V[0], 4.0);
obj.addTerm(V[1], 2.0);
obj.addTerm(V[2], 3.0);
obj.addTerm(V[3], 1.0);
*/
cplex.addMaximize(obj);
IloLinearNumExpr lin = cplex.linearNumExpr();
lin.addTerm(V[0], 3.0 );
lin.addTerm(V[1], 4.0 );
lin.addTerm(V[2], 1.0 );
lin.addTerm(V[3], 2.0 );
cplex.addLe(lin, 7.0, "constraint");
if( cplex.solve() ){
cplex.output().println("Solution status = " + cplex.getStatus());
cplex.output().println("Solution value = " + cplex.getObjValue());
System.out.println("V[0] " + cplex.getValue(V[0]));
System.out.println("V[1] " + cplex.getValue(V[1]));
System.out.println("V[2] " + cplex.getValue(V[2]));
System.out.println("V[3] " + cplex.getValue(V[3]));
}
else{
cplex.output().println("Solution status = " + cplex.getStatus());
}
}
}
#DecisionOptimization#OPLusingCPLEXOptimizer