Decision Optimization

Decision Optimization

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


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

Lexicographic permutations

  • 1.  Lexicographic permutations

    Posted 01/13/15 06:35 PM

    Originally posted by: nzinour


    Hello, 

    I'm trying to code a rather simple problem in OPL. Is there anyone who can help?

    A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:

    012   021   102   120   201   210

    Given n, the number of digits of interest (i.e. 3 for the values 0, 1, 2), print out j lexicographic permutations starting with the ith lexicographic permutation.

    Clearly, you need to ask the user for the values of n, i, and j.  There should be error checking on the value of i relative to the value of n


    #DecisionOptimization
    #MathematicalProgramming-General


  • 2.  Re: Lexicographic permutations

    Posted 01/15/15 03:19 AM

    Hi

    using CP;

    int n=4;

    int i=2;
    int j=3;

    int fact=prod(i in 1..n) i;

    dvar int x[1..fact][1..n] in 1..n;

    subject to
    {
    forall(i in 1..fact) allDifferent(all(j in 1..n)x[i][j]);
    forall(i in 2..fact) lex(all(j in 1..n)x[i-1][j],all(j in 1..n)x[i][j]);

    forall(i in 2..fact) or(j in 1..n) x[i-1][j]!=x[i][j];
    }

    execute
    {
    writeln("display");
    for(var k=i;k<i+j;k++) writeln(x[k]);
    }

    which gives

     

    display
     [1 2 4 3]
     [1 3 2 4]
     [1 3 4 2]

    regards


    #DecisionOptimization
    #MathematicalProgramming-General