Question

I've got the following objective function:

minimize sum (trueck[k] - time[k]) for all k (1..n).

I tried to set it up like this:

IloLinearNumExpr obj = cplex.linearNumExpr();

for(int k=0; k<grossK.length; k++){
    obj.addTerm(1.0, cplex.sum(trueck[k], cplex.negative(time[k])));
}

cplex.addMinimize(obj);

The eclipse error message for the 4th line is:

"The method addTerm(double, IloNumVar) in the type IloLinearNumExpr is not applicable for the arguments (double, IloNumExpr)"

I guess the method "addTerm" is wrong but I can't find a solution. Thanks in advance.

Was it helpful?

Solution

Don't use cplex.sum inside addTerm. You just need to separate out the two terms in your Objective function, since they both are summed over k.

Minimize sum (trueck[k] - time[k]) for all k (1..n)

Is the same as Min *sum_over_k* (trueck[k]) - *sum_over_k* (time[k])

This way, addTerm can handle it. (The code below is untested, but it gives you the idea of what you should try.)

IloLinearNumExpr obj = cplex.linearNumExpr();

for(int k=0; k<grossK.length; k++){
    obj.addTerm(1.0, trueck[k]);
    obj.addTerm(-1.0, time[k]);
}

cplex.addMinimize(obj);

Hope that helps.

OTHER TIPS

The addTerm method is expecting a coefficient and a variable. It is not expecting a complex expression (e.g. A sum) or a specific numeric value as that second argument. It may be that you have a logic issue with what you are trying to pass in, so you may want to consider updating your question with more detail about what you want to accomplish if this doesn't clear things up for you. In other words, the term you are trying to enter is not compatible with the linear expression that you are using.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top