Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

Solve CP Model in C++ (using cp - scheduling prob)

  • 1.  Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/07/12 10:12 PM

    Originally posted by: UMQX_Olfat_ELMahi


    Hellp i am trying to solve a scheduling problem using OPL i need to recall the procblem more then one time with different data input or calling different models.

    i was trying to use C++
    i tried to follow info in
    IBM ILOG OPL V6.3 > Language > Language User’s Manual > IBM ILOG Script for OPL > Tutorial: Flow control and multiple searches

    also i tried to use the examples like mulprod.cpp but they all depend on using cplex but i am using cp

    can any one send me some toutorial or example ??

    Thanks
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 2.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/07/12 10:22 PM

    Originally posted by: UMQX_Olfat_ELMahi


    I Found two examples steelmile.cpp and carseq.cpp but they just print the whole solution :
    opl.getCP().getObjValue()
    opl.printSolution(cout);

    but i can not find a command like :
    cp.getValue() ???
    Thanks
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 3.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/09/12 09:54 AM

    Originally posted by: ChrisBr


    Hello,

    You might use something like:
    
    opl.getCP().getValue(opl.getElement(
    "your_variable").asIntVar());
    

    I hope this helps,

    Chris.
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 4.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/09/12 11:19 AM

    Originally posted by: UMQX_Olfat_ELMahi


    yes it does this is the exact command i was looking for

    Thank you Thank you Thank you
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 5.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/09/12 11:59 AM

    Originally posted by: UMQX_Olfat_ELMahi


    another question if possible

    to add integer item you use : handler.addIntItem("variable name");
    to add a string you use : handler.addStringItem("variable name");
    but what to do if you wand to add float number ?

    thanks
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 6.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/09/12 12:16 PM

    Originally posted by: ChrisBr


    Hi,

    Try
    
    handler.startElement(
    "variable name"); handler.addNumItem(value); handler.endElement();
    

    
    
    
    public 
    
    void addNumItem(IloNum value)
    


    Chris.
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 7.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/09/12 03:34 PM

    Originally posted by: UMQX_Olfat_ELMahi


    thank you but i have another one "Sorry"

    after i finished writing the problem and run it it gives me no solution"


    ! Satisfiability problem - 0 variables, 0 constraints
    ! Initial process time : 0.00s (0.00s extraction + 0.00s propagation)
    ! . Log search space : 0.0 (before), 0.0 (after)
    ! . Memory usage : 530.6 KB (before), 530.6 KB (after)
    ! Using parallel search with 8 workers.
    !
    ! Branches Non-fixed W Branch decision
    * 0 0.01s 8 -
    !
    ! Search terminated normally, 1 solution found.
    ! Number of branches : 0
    ! Number of fails : 0
    ! Total memory usage : 807.0 KB (530.6 KB CP Optimizer + 276.4 KB Concert)
    ! Time spent in solve : 0.04s (0.04s engine + 0.00s extraction)
    ! Search speed (br. / s) : 0
    !
    with the following exception
    1. exception: No Objective has been extracted by IloAlgorithm 0000000002FC2200
    any idea what could be the problem ?

    know that the actual problem has:

    !
    ! Minimization problem - 8,946 variables, 37,100 constraints
    ! Preprocessing : 17,892 extractables eliminated, 17,831 constraints generated
    ! Initial process time : 1.76s (0.00s extraction + 1.76s propagation)
    ! . Log search space : 222,680.0 (before), 203,767.3 (after)
    ! . Memory usage : 35.5 MB (before), 44.5 MB (after)
    ! . Variables fixed : 252
    ! Using parallel search with 8 workers.
    !
    thanks again
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 8.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/10/12 05:30 AM

    Originally posted by: ChrisBr


    Hello,

    This exception is thrown when you try to get the objective value while you didn't add any objective to your model.

    BTW, looking at the output you got:
    
    ! Satisfiability problem - 0 variables, 0 constraints
    

    it seems that your model is empty.

    Won't you forget to generate it?
    
    opl.generate();
    


    Chris.
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 9.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/10/12 10:44 AM

    Originally posted by: UMQX_Olfat_ELMahi


    first i really thank you for your help.

    for the generation i did generate the model.

    i used the following commands:
    IloEnv env;
    int status = 127;
    try {
    IloOplErrorHandler handler(env,cout);
    std::istringstream in( getModelText() );
    IloOplModelSource modelSource(env,in,"TestArts");
    IloOplSettings settings(env,handler);
    IloOplModelDefinition def(modelSource,settings);
    IloCP cp(env);
    IloOplModel opl(def,cp);
    MyData data(env);
    IloOplDataSource dataSource(&data);
    opl.addDataSource(dataSource);
    opl.generate();

    if ( cp.solve()) {
    cout << " HHHHHHHHHHHHHHHHHHHHHHHIIIIIIIIIIIIIII";
    cout << endl
    << "OBJECTIVE: " << opl.getCP().getObjValue()
    << endl;
    opl.postProcess();
    opl.printSolution(cout);
    status = 0;
    } else {
    cout << "No solution!" << endl;
    status = 1;
    }
    } catch (IloOplException & e) {
    cout << "### OPL exception: " << e.getMessage() << endl;
    } catch( IloException & e ) {
    cout << "### exception: ";
    e.print(cout);
    status = 2;
    } catch (...) {
    cout << "### UNEXPECTED ERROR ..." << endl;
    status = 3;
    }

    env.end();

    cout << endl << "Press <Enter> to exit" << endl;
    getchar();

    return status;

    also if i change the OPL model to another simpler one it do run and get results.

    but my model have alot of execute blocks before objectives is this not allowed should i transfer all calculatons to C++ and only the variables and model in the OPL or this is not the problem ??

    Thanks
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 10.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/13/12 10:44 AM

    Originally posted by: ChrisBr


    Hello,

    Do you think that the problem of "//" inside c++ strings (see your other thread) explains your problem mentioned here? Or do you need more help?

    Chris.
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 11.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/14/12 08:44 AM

    Originally posted by: UMQX_Olfat_ELMahi


    yes it was thanks alot for your help i just waited to be sure.
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 12.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/17/12 11:21 PM

    Originally posted by: UMQX_Olfat_ELMahi


    Hello

    i know i am asking alot i hope i am not annoying:
    so regarding my previous question about how to access the decision variable of the model:
    you adviced me to use the command :
    opl.getCP().getValue(opl.getElement("your_variable").asIntVar());

    the variable i used for is :
    dvar int CapacityComponents in 0..3000;

    and i though of start with print it out :
    cout << opl.getCP().getValue(opl.getElement("Capacity").asIntVar());

    it gives me the following exapcion :

    OPL exception: Type dvar intComponents expected for element "Capacity"
    can you help me with that ?
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 13.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/19/12 09:06 PM

    Originally posted by: UMQX_Olfat_ELMahi


    i found out that i should use asIntMap() but i am not sure how ??
    Thanks
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 14.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/19/12 09:28 PM

    Originally posted by: UMQX_Olfat_ELMahi


    i got it but it still gives me exception it is something like that:

    // in mod file
    range Components = 0..nbComponents;
    ...
    dvar int CapacityComponents in 0..3000;
    //in cpp file
    IloIntMap x = opl.getElement("Capacity").asIntMap();
    IloSymbolSet s1 = opl.getElement("Components").asSymbolSet();

    for (IloSymbolSetIterator it1(s1); it1.ok(); ++it1){
    cout << x.get(*it1);
    }

    it through the exception :
    1. OPL exception: Type range expected for element "Components".

    ??
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 15.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/19/12 09:42 PM

    Originally posted by: UMQX_Olfat_ELMahi


    i got it :

    IloIntMap x = opl.getElement("Capacity").asIntMap();
    IloIntRange s1 = opl.getElement("Components").asIntRange();

    for (IloIntRangeIterator it1(s1); it1.ok(); ++it1){
    cout << x.get(*it1);
    }
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 16.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/20/12 11:23 PM

    Originally posted by: UMQX_Olfat_ELMahi


    Hello

    Sorry Again but now i am trying to get the values of one of the decision variables that defined as Var but i can not find the right get :

    //.mod
    dvar interval aplAct1[j in TAp1Pkt] ;

    //.cpp
    IloIntervalVarMap x1 = opl.getElement("aplAct1").asIntervalVarMap();
    IloIntRange s11 = opl.getElement("TAp1Pkt").asIntRange();

    for (IloIntRangeIterator it1(s11); it1.ok(); ++it1){
    Ap1NumOfSolns[0]*it1 = x1.get(*it1);
    cout << Ap1NumOfSolns[0]*it1 << " " ;
    }

    //error
    error C2440: '=' : cannot convert from 'const IloIntervalVar' to 'int'
    can you please help me

    Thanks
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 17.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/20/12 11:24 PM

    Originally posted by: UMQX_Olfat_ELMahi


    Hello

    Sorry Again but now i am trying to get the values of one of the decision variables that defined as Var but i can not find the right get :

    //.mod
    dvar interval aplAct1[j in TAp1Pkt] ;

    //.cpp
    IloIntervalVarMap x1 = opl.getElement("aplAct1").asIntervalVarMap();
    IloIntRange s11 = opl.getElement("TAp1Pkt").asIntRange();

    for (IloIntRangeIterator it1(s11); it1.ok(); ++it1){
    Ap1[NumOfSolns][0][*it1] = x1.get(*it1);
    cout << Ap1[NumOfSolns][0][*it1] << " " ;
    }

    //error

    error C2440: '=' : cannot convert from 'const IloIntervalVar' to 'int'
    can you please help me

    Thanks
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 18.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/21/12 05:34 AM

    Originally posted by: rdumeur


    Dear UMQX_Olfat_ELMahi,

    As stated in the documentation

    http://publib.boulder.ibm.com/infocenter/cosinfoc/v12r2/index.jsp?topic=%2Filog.odms.ide.help%2Fhtml%2Frefcppopl%2Fhtml%2Fclasses%2FIloIntervalVarMap.html

    The
    
    IloIntervalVarMap::get(..)
    
    method returns an IloIntervalVar handle, not an integer, which explains the compiler error.
    Note that an IloIntervalVar object is composite, then cannot be converted into an integer value.
    If you wish to access the state of an IloIntervalVar maintained by the solver, you can use several accessors available on the IloCP object:

    http://publib.boulder.ibm.com/infocenter/cosinfoc/v12r2/index.jsp?topic=%2Filog.odms.cpo.help%2Fhtml%2Frefcppcpoptimizer%2Fhtml%2Fclasses%2FIloCP.html

    namely:

    
    
    
    public IloInt    getEnd(
    
    const IloIntervalVar a) 
    
    const 
    
    public IloInt   getEndMax(
    
    const IloIntervalVar a) 
    
    const 
    
    public IloInt        getEndMin(
    
    const IloIntervalVar a) 
    
    const 
    
    public IloInt        getLength(
    
    const IloIntervalVar a) 
    
    const 
    
    public IloInt        getLengthMax(
    
    const IloIntervalVar a) 
    
    const 
    
    public IloInt     getLengthMin(
    
    const IloIntervalVar a) 
    
    const 
    
    public IloInt     getSize(
    
    const IloIntervalVar a) 
    
    const 
    
    public IloInt  getSizeMax(
    
    const IloIntervalVar a) 
    
    const 
    
    public IloInt       getSizeMin(
    
    const IloIntervalVar a) 
    
    const 
    
    public IloInt       getStart(
    
    const IloIntervalVar a) 
    
    const 
    
    public IloInt getStartMax(
    
    const IloIntervalVar a) 
    
    const 
    
    public IloInt getStartMin(
    
    const IloIntervalVar a) 
    
    const 
    
    public IloBool  isAbsent(
    
    const IloIntervalVar a) 
    
    const 
    
    public IloBool        isFixed(
    
    const IloIntervalVar a) 
    
    const 
    
    public IloBool isPresent(
    
    const IloIntervalVar a) 
    
    const
    


    which provide access to the instantiated interval var components and instantiation status.

    I hope this helps,
    Renaud
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 19.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/20/12 11:24 PM

    Originally posted by: UMQX_Olfat_ELMahi


    another Problem in retriving data
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 20.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/21/12 12:52 PM

    Originally posted by: UMQX_Olfat_ELMahi


    it did help thanks allot :
    i used the following

    IloIntervalVarMap x9 = opl.getElement("aplAct2CompNI").asIntervalVarMap();
    IloIntRange s16 = opl.getElement("NIVrange").asIntRange();
    for (IloIntRangeIterator it1(s16); it1.ok(); ++it1){
    IloIntervalVar oneTrip = x9.get(*it1);
    int start = cp.getStart(oneTrip);
    int size = cp.getSize(oneTrip);
    ap2CompNINumOfSolns[0]*it1 = start;
    ap2CompNINumOfSolns[1]*it1 = size;
    }
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 21.  Re: Solve CP Model in C++ (using cp - scheduling prob)

    Posted 02/22/12 10:51 AM

    Originally posted by: rdumeur


    Hi,

    This is indeed the right way to access the interval var state stored in the solver.
    #DecisionOptimization
    #OPLusingCPOptimizer