Topic
5 replies Latest Post - ‏2013-03-12T06:56:05Z by SystemAdmin
SystemAdmin
SystemAdmin
7944 Posts
ACCEPTED ANSWER

Pinned topic C++ concert - adding IloRangeArray to model

‏2013-03-08T08:15:41Z |
Hi,

I am new to CPLEX concert and dunno how to add multiple constraints to a model. Below is the attached code with compiling errors:


IloEnv env; IloModel model(env, 
"resalloc");   IloIntVarArray n[10]; IloIntVarArray fl[10]; 

for(

int i = 0; i < APP_NUM; i++)
{ n[i] = IloIntVarArray(env); fl[i] = IloIntVarArray(env); n[i].setSize(20); fl[i].setSize(20); 

for(

int j = 0; j < 20; j++)
{ n[i].add(IloIntVar(env, 1, 3, 
"number")); fl[i].add(IloIntVar(env, 1, 6, 
"freq_level")); 
} 
}   IloRangeArray range[10]; 

for(

int i = 0; i < 10; i++)
{ 

for(

int j = 0; j < 20; j++)
{ IloInt lower_bound =  1; IloInt upper_bound = 5; range[i].add(IloRange(env, lower_bound, (n[i][j] - 1)*6 + fl[i][j], upper_bound, 
"range")); 
} 
} model.add(range); env.end();


simulation.cpp(68): warning C4244: 'argument' : conversion from 'IloInt' to 'IloNum', possible loss of data
1>simulation.cpp(68): warning C4244: 'argument' : conversion from 'IloInt' to 'IloNum', possible loss of data
1>simulation.cpp(72): error C2664: 'IloExtractable IloModel::add(const IloExtractable) const' : cannot convert parameter 1 from 'IloRangeArray 10' to 'const IloExtractable'
1> No constructor could take the source type, or constructor overload resolution was ambiguous

Can't I directly use "model.add(range);" to accomplish what I've wished?
Another question is "can I directly assign an int type variable in C++ to a IloInt variable?".
Thanks in advance;)
Updated on 2013-03-12T06:56:05Z at 2013-03-12T06:56:05Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    7944 Posts
    ACCEPTED ANSWER

    Re: C++ concert - adding IloRangeArray to model

    ‏2013-03-08T08:27:29Z  in response to SystemAdmin
    No, you can't do that. The add() function takes as argument an IloExtractable. IloExtractables are (among others) IloRange and IloRangeArray, but not plain arrays of IloRange or IloRangeArray.
    You can change your code to the following to get it working
    IloRangeArray range[10];
    for(int i = 0; i < 10; i++){
      for(int j = 0; j < 20; j++){
        IloInt lower_bound =  1;
        IloInt upper_bound = 5;
        range[i].add(IloRange(env, lower_bound, (n[i][j] - 1)*6 + fl[i][j], upper_bound, "range"));
      }
      model.add(range[i]);
    }
    


    The question about int vars I did not understand. Can you please elaborate on what you want to do, maybe with a code example?

    The CPLEX distribution comes with a lot of examples, some of which are explained in detail in the user manual's tutorial section. You may want to take a look at them as well.
    Updated on 2014-03-24T22:36:13Z at 2014-03-24T22:36:13Z by iron-man
    • SystemAdmin
      SystemAdmin
      7944 Posts
      ACCEPTED ANSWER

      Re: C++ concert - adding IloRangeArray to model

      ‏2013-03-08T09:23:43Z  in response to SystemAdmin
      Thank you very much, DanielJunglas! I figured that out earlier myself;) The second question i asked can be illustrated as follows:
      int i;
      IloInt j;
      j = i;
      

      I was asking if the above value assignment is correct. I encountered another problem though. There prompts a run-time access violation error at this statement:
      IloIntExprArg range_expr = FREQ_LVL_PER_CPU*(n[i][j] - 1)+ fl[i][j];
      

      where FREQ_LVL_PER_CPU is a global constant i defined and both n[i] and fl[i] are IloIntVarArrays as shown in my first post. I was trying to pass range_expr to the following statement.
      range[i].add(IloRange(env, lower_bound, range_expr, upper_bound, "range"));
      

      How can I get the value of a IloIntVar? Guess I have been used to array operations in C++. I tried to get the value of the IloIntVal through [] operation.
      thanks for your help!
      Updated on 2014-03-24T22:36:09Z at 2014-03-24T22:36:09Z by iron-man
      • SystemAdmin
        SystemAdmin
        7944 Posts
        ACCEPTED ANSWER

        Re: C++ concert - adding IloRangeArray to model

        ‏2013-03-11T08:24:16Z  in response to SystemAdmin
        An assignment j = i (j of type IloInt, i of type int) is ok. You can think of IloInt being of type long.

        My guess for the access violation problem is that you did not initialize at least one of the elements in the arrays. Maybe use a debugger to figure out where exactly the access violation occurs. That should tell you which elements are not initialized.
        • SystemAdmin
          SystemAdmin
          7944 Posts
          ACCEPTED ANSWER

          Re: C++ concert - adding IloRangeArray to model

          ‏2013-03-12T01:42:34Z  in response to SystemAdmin
          Thanks daniel, i added an IloIntVar to an IloIntVarArray n[0] like this:

          n[0].add(IloIntVar(env, 1, CPU_CAP_PER_APP, "number"));
          


          I thought the added variable has already been initialized after this statement?

          I tried to assign a integer value to the newly added IloIntVar using

          n[0][0] = 5;
          

          but with a compiling error "error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)".

          How can I set an initial value to a newly created variable? Sorry I am pretty new to concert tech and there's been a lot to ask. The users' online manual does not provide everything.
          Updated on 2014-03-24T22:35:51Z at 2014-03-24T22:35:51Z by iron-man