Topic
  • 15 replies
  • Latest Post - ‏2013-03-19T07:15:31Z by SystemAdmin
SystemAdmin
SystemAdmin
754 Posts

Pinned topic Having problem with IloExpr type variable and operand "+="

‏2013-03-08T22:03:10Z |
I write a function to add constraints to my model.

void addConstarint(IloEnv env, IloModel &model, IloBoolVarArray y, BoolVarMatrix x)
/****************************************************/
/* This function add constarints to the formulation */
/****************************************************/

{
IloExpr constraint(env);
for (int i = 0; i < nCandidates; i++){
constraint += y[i];
}
cout << constraint << endl;
model.add(constraint == k);
for (int j = 0; j < nVoters; j++){
IloExpr constraint(env);
for(int i = 0; i < nCandidates; i++){
constraint+=x[i][j];
}
model.add(constraint == 1);
}
for(int i = 0; i < nCandidates; i++){
for (int j = 0; j < nVoters; j++){
model.add(x[i][j]-y[i] <= 0);
}
}
}

This is for IP solution and it works fine, but I want to check my LP solution and when I change the type of variables y and x and make them IloNumVarArray and NumVarMatrix in order to get the fractional solution, I got the following error when it reached to line "constraint += y[i];":
Error :"No source available for "operator+() "

This is my function for adding constraint to LP model :
void addConstarintLP(IloEnv env, IloModel &model, IloNumVarArray y, NumVarMatrix x)
/***********************************************************/
/* This function add constarints to the formulation for LP */
/***********************************************************/

{
IloExpr constraint(env);
IloBoolVar v;
constraint = constraint + v;
for (int i = 0; i < nCandidates; i++){
constraint += y[i];
}
cout << constraint << endl;
model.add(constraint == k);
for (int j = 0; j < nVoters; j++){
IloExpr constraint(env);
for(int i = 0; i < nCandidates; i++){
constraint+=x[i][j];
}
model.add(constraint == 1);
}
for(int i = 0; i < nCandidates; i++){
for (int j = 0; j < nVoters; j++){
model.add(x[i][j]-y[i] <= 0);
}
}
}
Can someone help me? what is my mistake or what did I forget?
Thank you.
Updated on 2013-03-19T07:15:31Z at 2013-03-19T07:15:31Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    754 Posts

    Re: Having problem with IloExpr type variable and operand "+="

    ‏2013-03-11T08:21:17Z  
    That is a strange error message. How did you define BoolVarMatrix and NumVarMatrix?
    What version of CPLEX do you use? What is your OS?
  • SystemAdmin
    SystemAdmin
    754 Posts

    Re: Having problem with IloExpr type variable and operand "+="

    ‏2013-03-11T17:05:37Z  
    That is a strange error message. How did you define BoolVarMatrix and NumVarMatrix?
    What version of CPLEX do you use? What is your OS?
    This is how I define the BoolVarMatrix and NumVarMatrix
    \{
    typedef IloArray<IloBoolVarArray> BoolVarMatrix;
    typedef IloArray<IloNumVarArray> NumVarMatrix;
    \}

    My cplex version is 12.2 and I run my program under both MAC OS and Linux (CentOS).
  • SystemAdmin
    SystemAdmin
    754 Posts

    Re: Having problem with IloExpr type variable and operand "+="

    ‏2013-03-12T07:01:44Z  
    This is how I define the BoolVarMatrix and NumVarMatrix
    \{
    typedef IloArray<IloBoolVarArray> BoolVarMatrix;
    typedef IloArray<IloNumVarArray> NumVarMatrix;
    \}

    My cplex version is 12.2 and I run my program under both MAC OS and Linux (CentOS).
    With that definition your code works fine here.
    Could you try to write a minimal example and post that verbatim here?
  • SystemAdmin
    SystemAdmin
    754 Posts

    Re: Having problem with IloExpr type variable and operand "+="

    ‏2013-03-12T07:29:16Z  
    With that definition your code works fine here.
    Could you try to write a minimal example and post that verbatim here?
    Sorry, but what do you mean by minimal example?
  • SystemAdmin
    SystemAdmin
    754 Posts

    Re: Having problem with IloExpr type variable and operand "+="

    ‏2013-03-12T17:38:06Z  
    Sorry, but what do you mean by minimal example?
    I create a simple example which end up with error:
    '{

    #include <iostream>
    #include <ilcplex/ilocplex.h>

    ILOSTLBEGIN

    int main()
    {

    IloEnv env;
    try
    {
    IloModel model(env);
    IloNumVarArray y(env,3);
    IloExpr e(env);
    e = y[0];
    }
    catch (IloException& e)
    {
    cerr << "Concert exception caught: " << e << endl;
    }
    catch (...)
    {
    cerr << "Unknown exception caught" << endl;
    }
    env.end();
    return 0;
    }

    '}
    The error is :
    No source available for "IloExpr::IloExpr() "

    but if I change the code to the following, it is OK!

    '{

    #include <iostream>
    #include <ilcplex/ilocplex.h>

    ILOSTLBEGIN

    int main()
    {

    IloEnv env;
    try
    {
    IloModel model(env);
    IloNumVar y(env);
    IloExpr e(env);
    e = y;
    }
    catch (IloException& e)
    {
    cerr << "Concert exception caught: " << e << endl;
    }
    catch (...)
    {
    cerr << "Unknown exception caught" << endl;
    }
    env.end();
    return 0;
    }
    '}
  • SystemAdmin
    SystemAdmin
    754 Posts

    Re: Having problem with IloExpr type variable and operand "+="

    ‏2013-03-12T17:45:54Z  
    Sorry, but what do you mean by minimal example?
    I create a simple example which end up with error:
    '{
    #include <iostream>
    #include <ilcplex/ilocplex.h>

    ILOSTLBEGIN

    int main()
    {

    IloEnv env;
    try
    {
    IloModel model(env);
    IloNumVarArray y(env,3);
    IloExpr e(env);
    e = y[0];
    }
    catch (IloException& e)
    {
    cerr << "Concert exception caught: " << e << endl;
    }
    catch (...)
    {
    cerr << "Unknown exception caught" << endl;
    }
    env.end();
    return 0;
    }

    }'
    The error is :
    No source available for "IloExpr::IloExpr() "

    but if I change the code to the following, it is OK!

    '{
    #include <iostream>
    #include <ilcplex/ilocplex.h>

    ILOSTLBEGIN

    int main()
    {

    IloEnv env;
    try
    {
    IloModel model(env);
    IloNumVar y(env);
    IloExpr e(env);
    e = y;
    }
    catch (IloException& e)
    {
    cerr << "Concert exception caught: " << e << endl;
    }
    catch (...)
    {
    cerr << "Unknown exception caught" << endl;
    }
    env.end();
    return 0;
    }
    }'
  • SystemAdmin
    SystemAdmin
    754 Posts

    Re: Having problem with IloExpr type variable and operand "+="

    ‏2013-03-12T18:34:43Z  
    I create a simple example which end up with error:
    '{
    #include <iostream>
    #include <ilcplex/ilocplex.h>

    ILOSTLBEGIN

    int main()
    {

    IloEnv env;
    try
    {
    IloModel model(env);
    IloNumVarArray y(env,3);
    IloExpr e(env);
    e = y[0];
    }
    catch (IloException& e)
    {
    cerr << "Concert exception caught: " << e << endl;
    }
    catch (...)
    {
    cerr << "Unknown exception caught" << endl;
    }
    env.end();
    return 0;
    }

    }'
    The error is :
    No source available for "IloExpr::IloExpr() "

    but if I change the code to the following, it is OK!

    '{
    #include <iostream>
    #include <ilcplex/ilocplex.h>

    ILOSTLBEGIN

    int main()
    {

    IloEnv env;
    try
    {
    IloModel model(env);
    IloNumVar y(env);
    IloExpr e(env);
    e = y;
    }
    catch (IloException& e)
    {
    cerr << "Concert exception caught: " << e << endl;
    }
    catch (...)
    {
    cerr << "Unknown exception caught" << endl;
    }
    env.end();
    return 0;
    }
    }'
    The following code also works fine on my computer!! I only change the IloNumVarArray to IloBoolVarArray !

    '{#include <iostream>
    #include <ilcplex/ilocplex.h>

    ILOSTLBEGIN

    int main()
    {
    IloEnv env;
    try
    {
    IloModel model(env);
    IloBoolVarArray y(env,3);
    IloExpr e(env);
    e = y[0];
    }
    catch (IloException& e)
    {
    cerr << "Concert exception caught: " << e << endl;
    }
    catch (...)
    {
    cerr << "Unknown exception caught" << endl;
    }
    cout << "helllo" << endl;
    env.end();
    return 0;
    } }'
  • SystemAdmin
    SystemAdmin
    754 Posts

    Re: Having problem with IloExpr type variable and operand "+="

    ‏2013-03-13T06:37:51Z  
    The following code also works fine on my computer!! I only change the IloNumVarArray to IloBoolVarArray !

    '{#include <iostream>
    #include <ilcplex/ilocplex.h>

    ILOSTLBEGIN

    int main()
    {
    IloEnv env;
    try
    {
    IloModel model(env);
    IloBoolVarArray y(env,3);
    IloExpr e(env);
    e = y[0];
    }
    catch (IloException& e)
    {
    cerr << "Concert exception caught: " << e << endl;
    }
    catch (...)
    {
    cerr << "Unknown exception caught" << endl;
    }
    cout << "helllo" << endl;
    env.end();
    return 0;
    } }'
    All the code examples you provided compile without problem here.
    Can you please specify exactly what machine and OS you use and show us the full compiler command line you use as well as the full compiler output?
  • SystemAdmin
    SystemAdmin
    754 Posts

    Re: Having problem with IloExpr type variable and operand "+="

    ‏2013-03-13T21:26:08Z  
    All the code examples you provided compile without problem here.
    Can you please specify exactly what machine and OS you use and show us the full compiler command line you use as well as the full compiler output?
    My machine is OPTIPLEX 755 and my OS is Linux (CentOS), but I have the same problem on my MacBookpro with Lion as an OS. The compiling procedure is fine, but when I run ./mycode, I get "Segmentation fault (core dumped)". By the way How can I format the code nicely, I do '{code}', but it seems it is not working.
  • SystemAdmin
    SystemAdmin
    754 Posts

    Re: Having problem with IloExpr type variable and operand "+="

    ‏2013-03-13T21:36:48Z  
    My machine is OPTIPLEX 755 and my OS is Linux (CentOS), but I have the same problem on my MacBookpro with Lion as an OS. The compiling procedure is fine, but when I run ./mycode, I get "Segmentation fault (core dumped)". By the way How can I format the code nicely, I do '{code}', but it seems it is not working.
    Do you put the code tag on both sides of the code? (Note that the closing tag is identical to the opening tag, unlike HTML.)

    Paul

    Mathematicians are like Frenchmen: whenever you say something to them, they translate it into their own language, and at once it is something entirely different. (Goethe)
  • SystemAdmin
    SystemAdmin
    754 Posts

    Re: Having problem with IloExpr type variable and operand "+="

    ‏2013-03-14T06:24:36Z  
    My machine is OPTIPLEX 755 and my OS is Linux (CentOS), but I have the same problem on my MacBookpro with Lion as an OS. The compiling procedure is fine, but when I run ./mycode, I get "Segmentation fault (core dumped)". By the way How can I format the code nicely, I do '{code}', but it seems it is not working.
    Aha, so you get a runtime error (a segmentation fault) and the "no source available for operator+" messages comes from some debugger?

    My first guess for a segmentation fault would that you are either doing an out-of-bounds array access or that you are using an uninitialized Ilo* instance. Did you check both? Maybe the debugger can assist you in checking that. Just displaying a backtrace is often very helpful.
  • SystemAdmin
    SystemAdmin
    754 Posts

    Re: Having problem with IloExpr type variable and operand "+="

    ‏2013-03-15T01:52:55Z  
    Aha, so you get a runtime error (a segmentation fault) and the "no source available for operator+" messages comes from some debugger?

    My first guess for a segmentation fault would that you are either doing an out-of-bounds array access or that you are using an uninitialized Ilo* instance. Did you check both? Maybe the debugger can assist you in checking that. Just displaying a backtrace is often very helpful.
    I sent you the code, I do not use any uninitialized Ilo*. I also attached the debugger's screenshot.
  • SystemAdmin
    SystemAdmin
    754 Posts

    Re: Having problem with IloExpr type variable and operand "+="

    ‏2013-03-19T06:26:56Z  
    I sent you the code, I do not use any uninitialized Ilo*. I also attached the debugger's screenshot.
    I did not receive any code. Maybe you can just attach the full code here?
  • SystemAdmin
    SystemAdmin
    754 Posts

    Re: Having problem with IloExpr type variable and operand "+="

    ‏2013-03-19T06:45:01Z  
    I did not receive any code. Maybe you can just attach the full code here?
    I already posted the code in this thread, but I post it below

    '{
    #include <iostream>
    #include <ilcplex/ilocplex.h>

    ILOSTLBEGIN

    int main()
    {

    IloEnv env;
    try
    {
    IloModel model(env);
    IloNumVarArray y(env,3);
    IloExpr e(env);
    e = y[0];
    }
    catch (IloException& e)
    {
    cerr << "Concert exception caught: " << e << endl;
    }
    catch (...)
    {
    cerr << "Unknown exception caught" << endl;
    }
    env.end();
    return 0;
    }
    '{
  • SystemAdmin
    SystemAdmin
    754 Posts

    Re: Having problem with IloExpr type variable and operand "+="

    ‏2013-03-19T07:15:31Z  
    I already posted the code in this thread, but I post it below

    '{
    #include <iostream>
    #include <ilcplex/ilocplex.h>

    ILOSTLBEGIN

    int main()
    {

    IloEnv env;
    try
    {
    IloModel model(env);
    IloNumVarArray y(env,3);
    IloExpr e(env);
    e = y[0];
    }
    catch (IloException& e)
    {
    cerr << "Concert exception caught: " << e << endl;
    }
    catch (...)
    {
    cerr << "Unknown exception caught" << endl;
    }
    env.end();
    return 0;
    }
    '{
    From the reference documentation of IloNumVarArray(IloEnv env, int n):
    
    This constructor creates an extensible array of n numeric variables in env. Initially, the n elements are empty handles.
    

    Consequently, in your code the elements in y are just empty handles (not initialized) and trying to assign those uninitialized elements to an IloExpr will throw the exception. You either need to use a constructor that initializes the elements in the array or explicitly initialize them.