Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Multiplying Tuple Values

    Posted 04/24/17 04:02 AM

    Originally posted by: Hectoor


    Hello all,

     

    I am trying to simulate a network routing problem.

    I have defined a tuple of arcs that includes arc's weight and probability of failure of them.

    I can find total cost with sum function but I need to multiply failure values. Is it possible? Can you help me?

     

     

    tuple arc 
        {
            key int sourceNode;
            key int destinationNode;
            key int cost;
            key float reliability;
        }
        {arc} Arcs = ...;

    dvar int flowOnArc[a in Arcs] in  0 .. 1;   //Flow integer. 0-> NOT USED, 1->USED

    dexpr float totalFlow = sum (a in Arcs) a.cost * flowOnArc[a];   //<-- IT IS WORKING TO COMPUTE TOTAL COST

    dexpr float totalReliability=prod(a in Arcs) a.reliability * flowOnArc[a];   //<-- IT IS NOT WORKING TO COMPUTE TOTAL FAILURE PROBABILITY

     

    At the last line, prod is not working. How can I solve this problem? Which function should I use?


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Multiplying Tuple Values

    Posted 04/24/17 08:33 AM

    Hi

    tuple arc
        {
            key int sourceNode;
            key int destinationNode;
            key int cost;
            key float reliability;
        }
        
        
        {arc} Arcs = {<1+i,2+i,3+i,4+i> | i in 1..4};

    dvar int flowOnArc[a in Arcs] in  0..1;   //Flow integer. 0-> NOT USED, 1->USED

    dexpr float totalFlow = sum (a in Arcs) a.cost * flowOnArc[a];   //<-- IT IS WORKING TO COMPUTE TOTAL COST

    //dexpr float totalReliability=prod(a in Arcs) a.reliability * flowOnArc[a];

    dexpr float totalReliability=prod(a in Arcs) a.reliability * (card(Arcs)==sum(a in Arcs)(flowOnArc[a]==1));

    subject to
    {

    }

    float totalReliability2=prod(a in Arcs) (a.reliability * flowOnArc[a]);


    execute
    {
    writeln(totalFlow);
    writeln(totalReliability);
    writeln(totalReliability2);
    }

    could help

    regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Multiplying Tuple Values

    Posted 04/24/17 09:14 AM

    Originally posted by: Hectoor


    Hello AlexFleischer,

    It is not worked. Let me explain a bit more.

     

    Here is arcs which are defined as Tuple

    tuple arc 
        {
             int sourceNode;
             int destinationNode;
             int cost;
             float reliability;
        }

    I have given values at .dat file. Reliability values are between 0-1.

    I set a bound value for desired reliability threshold. Value will be given at .dat file.

    float reliabilityBound=...;

    Now I set a flow integer that indicates this arc is used or not. 

    dvar int flowOnArc[a in Arcs] in  0 .. 1;

    I created an array that indicates path's reliability values

    dexpr float X[a in Arcs]=a.reliability* flowOnArc[a];

    Now I got an array like this [0 0 0 0.25 0 0 0.75 1 0 0.50] 

    I need to multiply them without counting 0 values to find path's total reliability.(I mean multiply this arrays nonzero elements)

    How can I do this?

    Related .mod and .dat files are attached.


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Multiplying Tuple Values

    Posted 04/24/17 11:21 AM

    Hi,

    you could move to

    using CP;

    //DEFINITION OF NODE
    int nNodes = ...;   // Number of nodes
    range Nodes = 1..nNodes;


    //DEFINITION OF ARC. HAS 4 PROPERTIES
    tuple arc
        {
             int sourceNode;
             int destinationNode;
             int cost;
             float reliability;
        }
        
    {arc} Arcs = ...;


    //DEFINITION OF SUPPLY
    int supply[Nodes] = ...;

    //DEFINITION OF WEIGHTS
    float reliabilityBound=...;

    //MODEL
    dvar int flowOnArc[a in Arcs] in  0 .. 1;   //Flow integer. 0-> NOT USED, 1->USED

    dexpr float totalFlow = sum (a in Arcs) a.cost * flowOnArc[a];

     


    dexpr float X[a in Arcs]=a.reliability* flowOnArc[a];

    dexpr float X2[a in Arcs]=(X[a]==0)+X[a];

    dexpr float totalReliability=prod(a in Arcs) X2[a];

    execute
    {
             writeln(X.size);
        
    }

    //OBJECTIVE
    //minimize totalFlow;
    minimize totalReliability;

    //CONSTRAINTS
    subject to
        {
            //FLOW BALANCE CONSTRAINT
            forall (i in Nodes)
              sum (<i,j,c,r> in Arcs) flowOnArc[<i,j,c,r>] - sum (<j,i,c,r> in Arcs) flowOnArc[<j,i,c,r>] == supply[i];
            
            //MAX NUMBER OF ITERATION CONSTRAINT
            iteration: totalFlow <=15;
            
            //RELIABLITY CONSTRAINT        
            //totalReliability>=reliabilityBound;
              

         }
     
     //PRINT RESULTS
     execute PRINT {
        writeln("\n<SOURCE,DESTINATION,FLOW,PROB OF FAILURE%>\n");
        writeln("<Total Flow:",totalFlow,">\n");
        for(var a in Arcs)
           if(flowOnArc[a] > 0)
           {
              writeln("<",a.sourceNode,",",a.destinationNode,",",a.cost,",",a.reliability,">");
              writeln(totalReliability);
             }          
     }

    regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Multiplying Tuple Values

    Posted 04/25/17 01:21 AM

    Originally posted by: Hectoor


    Hello AlexFleischer,

     

    It is worked. Thank you very much for your assist. 


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Multiplying Tuple Values

    Posted 04/25/17 01:34 AM

    Originally posted by: Hectoor


    Hello AlexFleischer,

     

    Your advise is worked well. However I am facing with a new problem. I got total reliability as well, there is no problem. but I want to compare it with a given bound.

    It is noted as "RELIABILITY CONSTRAINT" in my code. When I add reliability constraint, I am facing with error. How can I solve this?

     

    Actually I am trying to find in the following

    1. Find totalrealibility with multiplying path's probabilities.

    2. Multiply result with number of arcs used. (Denoted as totalFlow)

    3. Use this result with predefined value. Use it as a constraint in minimization problem. (Denoted as reliabilityBound)

     

    dvar int flowOnArc[a in Arcs] in  0 .. 1;   //Flow integer. 0-> NOT USED, 1->USED

    dexpr float totalFlow = sum (a in Arcs) a.cost * flowOnArc[a]; //Find Total Cost

    dexpr float X[a in Arcs]=a.reliability* flowOnArc[a]; //Find reliability of selected arcs

    dexpr float X2[a in Arcs]=(X[a]==0)+X[a]; //To find total reliability, make 0's to 1.

    dexpr float totalReliability=prod(a in Arcs) X2[a]; //To find total reliability multiply them.

     

    Now the following constraint is not working.

    minimize totalFlow; //Objective

    subject to

    {

    totalReliability>= reliabilityBound; //Constraint is NOT working

    }

    To sum up: How can I compare with dexpr float type with float type?

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Multiplying Tuple Values

    Posted 04/25/17 03:03 AM

    Hi,

    what you wrote is fine.

    you get TotalFlow 0 with all X being 0

    and then totalReliability would be 1.

    Maybe what you need is to add that not all X are 0

    You could write

    sum(a in Arcs) X[a]!=0;

    for that and then you get totalReliability 0.92

    regards

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: Multiplying Tuple Values

    Posted 04/25/17 03:49 AM

    Originally posted by: Hectoor


    Dear AlexFleischer

     

    sum(a in Arcs) X[a]!=0;

    This statement makes error. Where should I integrate in code? in "Subject to" part? or model part?

    I got "Syntax error, unexpected sum" error.

    My .mod file is attached. .dat file is same with previous update. Can you help me?

     

    I just want to make valid the following statement: totalReliability>= reliabilityBound; //Constraint generates ERROR 


    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: Multiplying Tuple Values

    Posted 04/25/17 03:57 AM

    sum(a in Arcs) X[a]!=0;

    should be in the "subject to" block

    regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  Re: Multiplying Tuple Values

    Posted 04/25/17 04:01 AM

    Originally posted by: Hectoor


    I have tried but it generates error message on script still.

    Error is in the following.


    Function operator!=(dexpr float,int) not available in context CPLEX. 

    Impossible to load model.   

     

    Could you try the attached .mod file?


    #CPLEXOptimizers
    #DecisionOptimization


  • 11.  Re: Multiplying Tuple Values

    Posted 04/25/17 04:18 AM

    you forgot

    using CP;

    at the top of the .mod

    regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 12.  Re: Multiplying Tuple Values

    Posted 04/25/17 04:20 AM

    Originally posted by: Hectoor


    How can I add CP to the mod? Could you advise a link that describes it?


    #CPLEXOptimizers
    #DecisionOptimization


  • 13.  Re: Multiplying Tuple Values

    Posted 04/25/17 04:23 AM

    see

    using CP;

    //DEFINITION OF NODE
    int nNodes = ...;   // Number of nodes
    range Nodes = 1..nNodes;


    //DEFINITION OF ARC. HAS 4 PROPERTIES
    tuple arc
        {
             int sourceNode;
             int destinationNode;
             int cost;
             float reliability;
        }
        
    {arc} Arcs = ...;


    //DEFINITION OF SUPPLY
    int supply[Nodes] = ...;

    //DEFINITION OF WEIGHTS
    float reliabilityBound=...;


     
     
     
     
     
      dvar int flowOnArc[a in Arcs] in  0 .. 1;   //Flow integer. 0-> NOT USED, 1->USED

    dexpr float totalFlow = sum (a in Arcs) a.cost * flowOnArc[a]; //Find Total Cost

    dexpr float X[a in Arcs]=a.reliability* flowOnArc[a]; //Find reliability of selected arcs

    dexpr float X2[a in Arcs]=(X[a]==0)+X[a]; //To find total reliability, make 0's to 1.

    dexpr float totalReliability=prod(a in Arcs) X2[a]; //To find total reliability multiply them.

     

     

    minimize totalFlow; //Objective

    subject to

    {

    sum(a in Arcs) X[a]!=0;


    totalReliability>= reliabilityBound; //Constraint is NOT working

    }


    #CPLEXOptimizers
    #DecisionOptimization


  • 14.  Re: Multiplying Tuple Values

    Posted 04/25/17 04:25 AM

    Originally posted by: Hectoor


    oh thank you very much. It is worked now


    #CPLEXOptimizers
    #DecisionOptimization