public MoreMoney() : base(0, 1000000) { IntVar d = new IntVar( m_Solver, 0, 9, "d"); IntVar e = new IntVar( m_Solver, 0, 9, "e"); IntVar m = new IntVar( m_Solver, 1, 9, "m"); IntVar n = new IntVar( m_Solver, 0, 9, "n"); IntVar o = new IntVar( m_Solver, 0, 9, "o"); IntVar r = new IntVar( m_Solver, 0, 9, "r"); IntVar s = new IntVar( m_Solver, 1, 9, "s"); IntVar y = new IntVar( m_Solver, 0, 9, "y"); IntVarList list = new IntVarList( m_Solver, new IntVar[] { d, e, m, n, o, r, s, y } ); m_Solver.Add( list.AllDifferent() ); IntVarListDotProduct send = new IntVarListDotProduct( m_Solver, new IntVar[] { s, e, n, d }, new int[] { 1000, 100, 10, 1 } ); IntVarListDotProduct more = new IntVarListDotProduct( m_Solver, new IntVar[] { m, o, r, e }, new int[] { 1000, 100, 10, 1 } ); IntVarListDotProduct money = new IntVarListDotProduct( m_Solver, new IntVar[] { m, o, n, e, y }, new int[] { 10000, 1000, 100, 10, 1 } ); m_Solver.Add( send ); m_Solver.Add( more ); m_Solver.Add( money ); IntVarExpr sendMore = send.Var0 + more.Var0; m_Solver.Add( sendMore ); IntVarCmp cmp = sendMore.Var0 == money.Var0; m_Solver.Add( cmp ); }
public Golomb( int n ) : base() { int maxLength = (int) Math.Pow( 2, (n-1) ) - 1; m_Solver.Horizon = new IntInterval( 0, maxLength ); m_MarkList = new IntVarList( m_Solver ); for( int idx = 0; idx < n; ++idx ) { m_MarkList.Add( new IntVar( m_Solver, idx, maxLength, "m" + ( idx + 1 ).ToString() ) ); } int mark = 0; int pos = 0; m_DiffList = new IntVarList( m_Solver, n * ( n -1 ) ); for( int i = 0; i < n - 1; ++i ) { for( int j = i + 1; j < n; ++j ) { if( i == n/2 && j == n-1 ) mark = pos; IntVarExpr exp = m_MarkList[ j ] - m_MarkList[ i ]; exp.Var0.Min = 1; exp.Var0.Name = "#" + m_MarkList[ j ].Name + "-" + m_MarkList[ i ].Name; m_Solver.Add( exp ); m_DiffList.Add( exp.Var0 ); ++pos; } } IntVarListAllDifferent ad = m_DiffList.AllDifferent(); //ad.Level = Constraint.PropagateLevel.High; m_Solver.Add( ad ); // start mark at 0 m_MarkList[ 0 ].Value = 0; // lower half should be less than the half difference IntVarCmp cmp = m_MarkList[ (n-1)/2 ] < m_DiffList[ mark ]; m_Solver.Add( cmp ); Console.WriteLine( cmp.ToString() +", " + m_DiffList[ mark ].ToString() ); }
static void Test2() { Solver solver = new Solver( 0, 100 ); IntVar a = new IntVar( solver, 1, 3, "a" ); IntVar b = new IntVar( solver, 1, 3, "b" ); IntVar c = new IntVar( solver, 1, 3, "c" ); IntVarList l = new IntVarList( a, b, c ); IntVarListAllDifferent diff = l.AllDifferent(); solver.Add( diff ); solver.Propagate(); a.Value = 1; b.Value = 2; }