Decision Optimization

Decision Optimization

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


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

using assert to test a constraint

  • 1.  using assert to test a constraint

    Posted 05/10/17 11:13 PM

    Originally posted by: MchelLeo


    Hi Alex,

    I would like to test a constraint to know how is correct or incorrect?. So, I give a example as follows:

    tuple myTuple {
    key int foo;
    key int bar;
    int ram;
    }
    {myTuple} myTuples = {
            <1,1,1>,<1,2,1>,<1,3,2>,<2,1,3>,<2,2,1>,<2,3,2>
     };;
    range r1to5=1..5;
    range r1to4=1..4;

    int ramArray[r1to5][r1to4];

    int ramArray2[foo in r1to5][bar in r1to4]=
    (<foo,bar> in myTuples)?item(myTuples,<foo,bar>).ram:0;

    execute
    {
    for(foo in r1to5){
        for(bar in r1to4){
            if(myTuples.find(foo,bar)) ramArray[foo][bar] = myTuples.find(foo,bar).ram;
            else ramArray[foo][bar] = 0;
        }
    }
    }

    assert forall(foo in r1to5,bar in r1to4)
        ramArray[foo][bar]==ramArray2[foo][bar];

    Then, I get results:

    But I don't see any other results from assert that created to check calculation steps.

    Thanks,

    Leo


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: using assert to test a constraint

    Posted 05/11/17 07:15 AM

    Hi,

    assert sends an eror only if the assert is violated.

    If you need the value you could try

     tuple myTuple {
    key int foo;
    key int bar;
    int ram;
    }
    {myTuple} myTuples = {
            <1,1,1>,<1,2,1>,<1,3,2>,<2,1,3>,<2,2,1>,<2,3,2>
     };;
    range r1to5=1..5;
    range r1to4=1..4;

    int ramArray[r1to5][r1to4];

     

    int ramArray2[foo in r1to5][bar in r1to4]=
    (<foo,bar> in myTuples)?item(myTuples,<foo,bar>).ram:0;

    execute
    {
    for(foo in r1to5){
        for(bar in r1to4){
            if(myTuples.find(foo,bar)) ramArray[foo][bar] = myTuples.find(foo,bar).ram;
            else ramArray[foo][bar] = 0;
        }
    }
    }

     

    int check[foo in r1to5][bar in r1to4]=
        (ramArray[foo][bar]==ramArray2[foo][bar]);
        
        execute
        {
        writeln(check);    
        }

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer