Decision Optimization

Decision Optimization

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


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

[JAVA] IloCustomConstraints usage

  • 1.  [JAVA] IloCustomConstraints usage

    Posted 05/16/14 03:36 AM

    Originally posted by: cvannier


    Hi !

    First of all, my apologies if i'm not in the good forum.

    I'm a trainee and i have to use ilog cp optimizer, and especially express constraints.

    I've found the IloCustomConstraint, and tried to use it without any success. Here is my last attempt, to express the constraint X + Y == 17

    public class IlogCpOptimizerTests {
        public static void main( String[] args ) {
                    try {
                            IloCP cp = new IloCP();
                            IloIntVar X = cp.intVar( 5, 12, "X" );
                            IloIntVar Y = cp.intVar( 2, 17, "Y" );
    
                            //cp.add( cp.eq( 17, cp.sum( X, Y ))); // Working with this constraint
                            cp.add( new MyCustomConstraint( cp, X, Y )); // not working with my custom one
                            cp.add( cp.eq( 5, cp.diff( X, Y )));
                            
                            cp.propagate();
                            
                            if ( cp.solve()) {    
                                    System.out.println();
                                    System.out.println( "X:     " + cp.getValue( X ));
                                    System.out.println( "Y:     " + cp.getValue( Y ));
                            }
                    } catch ( IloException e ) {
                            System.err.println( "Error " + e );
                    }
            }
    }
    
    public class MyCustomConstraint extends IloCustomConstraint {
        private IloIntVar _x;
        private IloIntVar _y;
    
        protected MyCustomConstraint(IloCP cp, IloIntVar x, IloIntVar y) throws IloException {
            super( cp );
            _x = x;
            addVar( _x );
            _y = y;
            addVar( _y );
        }
    
            @Override
            public void execute() {
                    if( isFixed( _x ) ) {
                            if( isFixed( _y ) ) {
                                    double xValue = getValue( _x );
                                    double yValue = getValue( _y );
                                    
                                    if( xValue + yValue == 17 ) {
                                            // OK
                                            return;
                                    }
                            }
                    }
                    violate();
            }
    
    }
    

    I guess i'm not using it right, but i've not been able to find any examples of it.

    Can someone help me ?

     

    Regards,
    cvannier


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 2.  Re: [JAVA] IloCustomConstraints usage

    Posted 05/19/14 08:42 AM

    Originally posted by: GGR


    Hi

    It seems to me that you propagate x + y != 17

     

     

    Cheers


    #DecisionOptimization
    #OPLusingCPOptimizer