Originally posted by: seg.fault
Hello,
I setup a profit-maximization problem in C++ where the objective function is defined as the difference between revenue and cost.
From the obtained solution, I need to to retrieve both the objective value and the values of the revenue and cost from which the objective value has been computed.
I'm facing some issues with CPLEX v12.8.0 probably because I didn't understand well how to use variables/expressions and the IloCplex::getValue method.
I report my problem with an example where the objective value, the revenue and the cost should be 1.038, 1.46 and 0.422, respectively..
- Tentative#1: in my original formulation, I defined 2 expressions, one for the revenue and one for the cost, by means of which I defined the objective function as the difference of the two.
The result is: objective value is OK, value of revenue expression is OK, value of cost expression is NOT OK. Specifically:
* Code fragment:
IloObjective obj;
IloNumExpr revenue_expr(env);
IloNumExpr cost_expr(env);
//...
obj = IloMaximize(env, revenue_expr-cost_expr);
mode.add(obj);
//...
cplex.solve();
//...
std::cout << "Obj value: " << cplex.getObjValue() << " (revenue-expr: " << cplex.getValue(revenue_expr) << ", cost-expr: " << cplex.getValue(cost_expr) << ")" << std::endl;
* Output:
Obj value: 1.038 (revenue-expr: 1.46, cost-expr: 1.022)
- Tentative#2: to solve the above issue, I added two variables (one for the revenue and the other one for the cost) and constrained them to be equal to the revenue and cost expressions defined above, respectively, and then I used these variables to define the objective function in a similar way I have done in Tentative#1.
The result is: objective value is NOT OK, value of revenue expression is OK, value of cost expression is NOT OK. Specifically:
* Code fragment:
IloObjective obj;
IloNumVar revenue_var(env, "revenue");
model.add(revenue_var);
IloNumVar cost_var(env, "cost");
model.add(cost_var);
IloNumExpr revenue_expr(env);
//...
model.add(IloConstraint(env, revenue_var == revenue_expr));
IloNumExpr cost_expr(env);
//...
model.add(IloConstraint(env, cost_var == cost_expr));
obj = IloMaximize(env, revenue_var-cost_var);
mode.add(obj);
//...
cplex.solve();
//...
std::cout << "Obj value: " << cplex.getObjValue() << " (revenue-var: " << cplex.getValue(revenue_var) << ", revenue-expr: " << cplex.getValue(revenue_expr) << ", cost-var: " << cplex.getValue(cost_var) << ", cost-expr: " << cplex.getValue(cost_expr) << ")" << std::endl;
* Output:
Obj value: 1.46 (revenue-var: 1.46, revenue-expr: 1.46, cost-var: 0, cost-expr: 0.422)
- Tentative#3: starting from Tentative#2, I was finally able to get a working solution by simply defining the objective function by means of the expressions instead of the variables.
The result is: objective value, value of revenue expression, and value of cost expression are all OK. Specifically:
* Code fragment:
IloObjective obj;
IloNumVar revenue_var(env, "revenue");
model.add(revenue_var);
IloNumExpr revenue_expr(env);
//...
model.add(IloConstraint(env, revenue_var == revenue_expr));
IloNumVar cost_var(env, "cost");
model.add(cost_var);
IloNumExpr cost_expr(env);
//...
model.add(IloConstraint(env, cost_var == cost_expr));
obj = IloMaximize(env, revenue_expr-cost_expr);
mode.add(obj);
//...
cplex.solve();
//...
std::cout << "Obj value: " << cplex.getObjValue() << " (revenue-var: " << cplex.getValue(revenue_var) << ", revenue-expr: " << cplex.getValue(revenue_expr) << ", cost-var: " << cplex.getValue(cost_var) << ", cost-expr: " << cplex.getValue(cost_expr) << ")" << std::endl;
* Output:
Obj value: 1.038 (revenue-var: 1.46, revenue-expr: 1.46, cost-var: 0, cost-expr: 0.422)
Can anyone be so kind to tell what's happening here?
Thank you very much for your consideration and time.
Best,
Sergio.
#CPLEXOptimizers#DecisionOptimization