public Sudoku() : base(1, 9) { int size = 9; m_Matrix = new IntVarMatrix(m_Solver, size, size, new IntInterval(1, size)); for (int idx = 0; idx < size; ++idx) { IntVarListAllDifferent constraint = m_Matrix.Row(idx).AllDifferent(); constraint.Level = PropagateLevel.High; m_Solver.Add(constraint); } for (int idx = 0; idx < size; ++idx) { IntVarListAllDifferent constraint = m_Matrix.Col(idx).AllDifferent(); constraint.Level = PropagateLevel.High; m_Solver.Add(constraint); } for (int subRow = 0; subRow < 3; ++subRow) { for (int subCol = 0; subCol < 3; ++subCol) { IntVarListAllDifferent constraint = m_Matrix.Matrix(subRow * 3, subCol * 3, 3, 3).VarList.AllDifferent(); constraint.Level = PropagateLevel.High; m_Solver.Add(constraint); } } }
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; }
public MagicSquare(int count) : base(0, 10000) { m_Matrix = new IntVarMatrix(m_Solver, count, count, new IntInterval(1, count * count)); m_MagicConstant = (count * (count * count + 1)) / 2; for (int idx = 0; idx < count; ++idx) { m_Solver.Add(m_Matrix.Row(idx).Sum(m_MagicConstant)); m_Solver.Add(m_Matrix.Col(idx).Sum(m_MagicConstant)); } bool mm = false; if (mm) { if (count % 2 == 0) { int magicConstant2 = m_MagicConstant / 2; for (int idx = 0; idx < count; ++idx) { m_Solver.Add(m_Matrix.Row(idx, 0, count / 2).Sum(magicConstant2)); m_Solver.Add(m_Matrix.Row(idx, count / 2, count).Sum(magicConstant2)); m_Solver.Add(m_Matrix.Col(idx, 0, count / 2).Sum(magicConstant2)); m_Solver.Add(m_Matrix.Col(idx, count / 2, count).Sum(magicConstant2)); } } } m_Solver.Add(m_Matrix.DiagLeftTopToBottomRight().Sum(m_MagicConstant)); m_Solver.Add(m_Matrix.DiagRightTopToBottomLeft().Sum(m_MagicConstant)); IntVarListAllDifferent ad = m_Matrix.VarList.AllDifferent(); m_Solver.Add(ad); // remove symmetry m_Solver.Add(m_Matrix[0, 0] < m_Matrix[0, count - 1]); m_Solver.Add(m_Matrix[0, 0] < m_Matrix[count - 1, count - 1]); m_Solver.Add(m_Matrix[0, 0] < m_Matrix[count - 1, 0]); m_Solver.Add(m_Matrix[0, count - 1] < m_Matrix[count - 1, 0]); }
public void High() { Solver solver = new Solver(-10000, 10000); IntVar a = new IntVar(solver, 1, 3, "a"); IntVar b = new IntVar(solver, 1, 2, "b"); IntVar c = new IntVar(solver, 1, 2, "c"); IntVarListAllDifferent cons = new IntVarListAllDifferent( solver, new IntVar[] { a, b, c }); cons.Level = PropagateLevel.High; solver.Add(cons); solver.Propagate(); Assert.AreEqual(a.Domain, new IntDomain(3, 3)); Assert.AreEqual(b.Domain, new IntDomain(1, 2)); Assert.AreEqual(c.Domain, new IntDomain(1, 2)); }
public void High() { Solver solver = new Solver( -10000, 10000 ); IntVar a = new IntVar( solver, 1, 3, "a" ); IntVar b = new IntVar( solver, 1, 2, "b" ); IntVar c = new IntVar( solver, 1, 2, "c" ); IntVarListAllDifferent cons = new IntVarListAllDifferent( solver, new IntVar[] { a, b, c } ); cons.Level = PropagateLevel.High; solver.Add( cons ); solver.Propagate(); Assert.AreEqual( a.Domain, new IntDomain( 3, 3 ) ); Assert.AreEqual( b.Domain, new IntDomain( 1, 2 ) ); Assert.AreEqual( c.Domain, new IntDomain( 1, 2 ) ); }