Topic
1 reply Latest Post - ‏2014-05-19T12:41:40Z by GGR
cvannier
cvannier
2 Posts
ACCEPTED ANSWER

Pinned topic [JAVA] IloCustomConstraints usage

‏2014-05-16T07:36:18Z |

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

Updated on 2014-05-16T07:39:30Z at 2014-05-16T07:39:30Z by cvannier
  • GGR
    GGR
    65 Posts
    ACCEPTED ANSWER

    Re: [JAVA] IloCustomConstraints usage

    ‏2014-05-19T12:41:40Z  in response to cvannier

    Hi

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

     

     

    Cheers