Question

I got a problem in my program. I know where but I don't know why.

Here is my code:

#include <ilcplex/ilocplex.h>
ILOSTLBEGIN
using namespace std;
typedef IloArray<IloNumArray>    NumMatrix;
typedef IloArray<IloNumVarArray> NumVarMatrix;

int main() {
IloEnv env; 
IloInt i, j, k;
IloModel model(env); 
IloInt pro = 4; 
IloInt empl = 5;

IloNumArray e(env, project, 2, 2, 2, 3);
IloNumArray pr(env, project, 1000, 2000, 500, 1500);

IloNumVarArray p(env, project, 0, 1);
NumVarMatrix x(env, project);

for(k = 0; k < pro; k++) {
    x[k] = IloNumVarArray(env, empl+1, 0, 1);
}

for(k = 0; k < pro; k++) {     
    IloExpr sum_over_i(env);
    for(i = 0; i < empl; i++)
        sum_over_i += x[i][k];
    model.add(sum_over_i >= e[k] * p[k]);
    sum_over_i.end();
}

}

When pro and empl is same value or empl less than pro everything work. but if empl is more than pro it doesn't work anymore.

Does anyone have an idea why empl can't be > than pro?

Thanks

Was it helpful?

Solution

The indices of x in the inner loop are reversed. You should be referencing x[k][i] not x[i][k]. It's not working either way, it's just that it's crashing only if you have empl > pro because you are creating arrays with pro+1 elements instead of pro elements. You can avoid writing the inner loop altogether by using IloSum.

for(k = 0; k < pro; k++) {
    x[k] = IloNumVarArray(env, empl, 0, 1); // don't add additional elements
}

for(k = 0; k < pro; k++) {     
    model.add(IloSum(x[k]) >= e[k] * p[k]);
}

OTHER TIPS

I don't know CPLEX, but I'll take a wild guess:

NumVarMatrix x(env, project);

for(k = 0; k < pro; k++) {
    x[k] = IloNumVarArray(env, empl+1, 0, 1);
}

...

for(i = 0; i < empl; i++)
    sum_over_i += x[i][k];

So if empl > pro, it looks very much as if you're reading past the bottom row of the matrix.

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