Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
  • 1.  C++ concert - adding IloRangeArray to model

    Posted Fri March 08, 2013 03:16 AM

    Originally posted by: SystemAdmin


    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;)
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: C++ concert - adding IloRangeArray to model

    Posted Fri March 08, 2013 03:27 AM

    Originally posted by: 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.
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: C++ concert - adding IloRangeArray to model

    Posted Fri March 08, 2013 04:24 AM

    Originally posted by: 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!
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: C++ concert - adding IloRangeArray to model

    Posted Mon March 11, 2013 04:24 AM

    Originally posted by: 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.
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: C++ concert - adding IloRangeArray to model

    Posted Mon March 11, 2013 09:43 PM

    Originally posted by: 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.
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: C++ concert - adding IloRangeArray to model

    Posted Tue March 12, 2013 02:56 AM

    Originally posted by: SystemAdmin


    I think most of your questions have already been answered here and here.

    In order to provide initial values for variables (a warm start solution) you need to use MIP starts. Please refer to the user manual and the overview of related functions.
    #CPLEXOptimizers
    #DecisionOptimization