Integer variables.
Inheritance: Variable
Ejemplo n.º 1
0
 internal void queens(int n)
 {
     var c = 0;
     var net = new Network();
     var q = new IntVariable[n];
     var u = new IntVariable[n];
     var d = new IntVariable[n];
     for (var i = 0; i < n; ++i)
     {
         q[i] = new IntVariable(net, 1, n);
         u[i] = q[i].Add(i);
         d[i] = q[i].Subtract(i);
     }
     new NotEquals(net, q);
     new NotEquals(net, u);
     new NotEquals(net, d);
     Solver solver = new DefaultSolver(net);
     for (solver.Start(); solver.WaitNext(); solver.Resume())
     {
         Solution solution = solver.Solution;
         sol[c] = new int[8];
         for (int i = 0; i < n; i++)
         {
             var s = solution.GetIntValue(q[i]);
             sol[c][i] = solution.GetIntValue(q[i]);
         }
         c++;
     }
     solver.Stop();
 }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            var net = new Network();

            var x = new IntVariable(net, 0, 708);
            var y = new IntVariable(net, 0, 708);
            var z = new IntVariable(net, 0, 708);
            var t = new IntVariable(net, 0, 708);
            x.Add(y).Add(z).Add(t).Equals(711);
            x.Ge(y);
            y.Ge(z);
            z.Ge(t);
            x.Multiply(y).Multiply(z).Multiply(t).Equals(711000000);
            Solver solver = new DefaultSolver(net);
            for (solver.Start(); solver.WaitNext(); solver.Resume())
            {
                var solution = solver.Solution;
                Console.Out.WriteLine();
                Console.Out.WriteLine(" {0:F} + {1:F} + {2:F} + {3:F} = {4:F} ",
                                      solution.GetIntValue(x)/100.0, solution.GetIntValue(y)/100.0,
                                      solution.GetIntValue(z)/100.0, solution.GetIntValue(t)/100.0,7.11 );
            }
            solver.Stop();
            Console.ReadLine();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Network net = new Network();

            IntVariable[] A = new IntVariable[6];
            for (int j=0; j< 6; j++)
            {
                IntDomain d = new IntDomain(1, 9);
                A[j] = new IntVariable(net);
                A[j].Domain = d;
                Trail trail = new Trail();
            }
            ((A[4].Multiply(10).Add(A[5])).Multiply(A[0])).Add(
            ((A[1].Multiply(10).Add(A[2])).Multiply(A[3]))).Equals(
                (A[1].Multiply(10).Add(A[2])).Multiply(A[4].Multiply(10).Add(A[5])));
            new NotEquals(net, A);
            Solver solver = new DefaultSolver(net);
            int i = 0;
            for (solver.Start(); solver.WaitNext(); solver.Resume())
            {
                Solution solution = solver.Solution;
                Console.Out.WriteLine();
                Console.Out.WriteLine(solution.GetIntValue(A[0]) + "    " + solution.GetIntValue(A[3]));
                Console.Out.WriteLine("-- + -- = 1");
                Console.Out.WriteLine(solution.GetIntValue(A[1])+""+solution.GetIntValue(A[2])+"   "+
                                      solution.GetIntValue(A[4]) + solution.GetIntValue(A[5]));
                Console.Out.WriteLine("=========");
                i++;
            }
            Console.Out.WriteLine("There are {0} solutions",i);

            solver.Stop();
            Console.In.ReadLine();
            //------------------------------------------------------------------------------------------
        }
Ejemplo n.º 4
0
        public virtual IntVariable Negate()
        {
            Network net = Network;
            var     x   = new IntVariable(net);

            // x = - this
            new IntFunc(net, IntFunc.Negate, x, this);
            return(x);
        }
Ejemplo n.º 5
0
        public virtual IntVariable Min(int value)
        {
            Network net = Network;
            var     x   = new IntVariable(net);

            // x = Min(this, value)
            new IntArith(net, IntArith.MIN, x, this, new IntVariable(net, value));
            return(x);
        }
Ejemplo n.º 6
0
        public virtual IntVariable Min(IntVariable v)
        {
            Network net = Network;
            var     x   = new IntVariable(net);

            // x = Min(this, v)
            new IntArith(net, IntArith.MIN, x, this, v);
            return(x);
        }
Ejemplo n.º 7
0
        public virtual IntVariable Multiply(int value)
        {
            Network net = Network;
            var     x   = new IntVariable(net);

            // x = this * value
            new IntArith(net, IntArith.MULTIPLY, x, this, new IntVariable(net, value));
            return(x);
        }
Ejemplo n.º 8
0
        public virtual IntVariable Multiply(IntVariable v)
        {
            Network net = Network;
            var     x   = new IntVariable(net);

            // x = this * v
            new IntArith(net, IntArith.MULTIPLY, x, this, v);
            return(x);
        }
Ejemplo n.º 9
0
        public virtual IntVariable Subtract(int value)
        {
            Network net = Network;
            var     x   = new IntVariable(net);

            // x = this - value
            new IntArith(net, IntArith.Subtract, x, this, new IntVariable(net, value));
            return(x);
        }
Ejemplo n.º 10
0
        public virtual IntVariable Subtract(IntVariable v)
        {
            Network net = Network;
            var     x   = new IntVariable(net);

            // x = this - v
            new IntArith(net, IntArith.Subtract, x, this, v);
            return(x);
        }
Ejemplo n.º 11
0
 internal static void maxExample()
 {
     var net = new Network();
     var x = new IntVariable(net, -1, 1, "x");
     var y = new IntVariable(net, -1, 1, "y");
     var z = x.Max(y);
     z.Name ="z";
     runExample(net, Solver.Default);
 }
Ejemplo n.º 12
0
        public virtual IntVariable Add(IntVariable v)
        {
            Network net = Network;
            var     x   = new IntVariable(net);

            // x = this + v
            new IntArith(net, IntArith.Add, x, this, v);
            return(x);
        }
Ejemplo n.º 13
0
 internal static void absExample()
 {
     var net = new Network();
     var x = new IntVariable(net, -3, 2, "x");
     var y = x.Abs();
     y.NotEquals(2);
     y.Name ="y";
     runExample(net, Solver.Default);
 }
Ejemplo n.º 14
0
        public virtual IntVariable Add(int value)
        {
            Network net = Network;
            var     x   = new IntVariable(net);

            // x = this + value
            new IntArith(net, IntArith.Add, x, this, new IntVariable(net, value));
            return(x);
        }
Ejemplo n.º 15
0
        public virtual IntVariable Abs()
        {
            Network net = Network;
            var     x   = new IntVariable(net);

            // x = Abs(this)
            new IntFunc(net, IntFunc.Abs, x, this);
            return(x);
        }
Ejemplo n.º 16
0
        public virtual IntVariable Sign()
        {
            Network net = Network;
            var     x   = new IntVariable(net);

            // x = Sign(this)
            new IntFunc(net, IntFunc.Sign, x, this);
            return(x);
        }
Ejemplo n.º 17
0
        internal static void pp()
        {
            Network net = new Network();
            // number of materials
            int m = 3;
            // limit of each material
            int[] limit = new int[] { 1650, 1400, 1800 };
            // number of products
            int n = 2;
            // profit of each product
            int[] p = new int[] { 5, 4 };
            // amount of materials required to make each product
            int[][] a = new int[][] { new int[] { 15, 10, 9 }, new int[] { 11, 14, 20 } };

            // initialize variables for products
            IntVariable[] x = new IntVariable[n];
            for (int j = 0; j < n; j++)
            {
                x[j] = new IntVariable(net);
                x[j].Ge(0);
            }
            // generate constraits of limiting materials
            for (int i = 0; i < m; i++)
            {
                IntVariable sum = new IntVariable(net, 0);
                for (int j = 0; j < n; j++)
                {
                    sum = sum.Add(x[j].Multiply(a[j][i]));
                }
                sum.Le(limit[i]);
            }
            // total profit
            IntVariable profit = new IntVariable(net, 0);
            for (int j = 0; j < n; j++)
            {
                profit = profit.Add(x[j].Multiply(p[j]));
            }
            // maximize the total profit
            net.Objective = profit;
            // iteratively find a better solution until the optimal solution is found
            Solver solver = new DefaultSolver(net, Solver.Maximize | Solver.Better);
            for (solver.Start(); solver.WaitNext(); solver.Resume())
            {
                Solution solution = solver.Solution;
                Console.WriteLine(solver.GetCount());
                Console.Out.WriteLine("Profit = " + solution.GetIntValue(profit));
                for (int j = 0; j < n; j++)
                {
                    Console.Out.WriteLine("x[" + j + "]=" + solution.GetIntValue(x[j]));
                }
                Console.Out.WriteLine();
            }

            solver.Stop();
            Console.ReadLine();
        }
Ejemplo n.º 18
0
 protected override IToken EvaluateComplexOperation(IToken n, Tokens o)
 {
     Variable vr;
       Variable vn = ConvertTokenToVariable(n);
       switch(o) {
       case Tokens.UMINUS:
     vr = new IntVariable(_network, "-" + vn.ToString());
     new IntFunc(_network, IntFunc.Negate, vr, vn);
     return new VariableToken(vr);
       default:
     throw new ArgumentException();
       }
 }
Ejemplo n.º 19
0
 internal static void minimizeExample()
 {
     var net = new Network();
     var x = new IntVariable(net, 1, 10, "x");
     var y = new IntVariable(net, 1, 10, "y");
     // x + y >= 10
     x.Add(y).Ge(10);
     // z = max(x, y)
     var z = x.Max(y);
     z.Name ="z";
     // minimize z
     net.Objective = z;
     runExample(net, Solver.Minimize | Solver.Better);
 }
Ejemplo n.º 20
0
 internal static void elementExample()
 {
     var net = new Network();
     var x = new IntVariable(net, "x");
     var i = new IntVariable(net, "i");
     const int n = 4;
     var v = new IntVariable[n];
     //var ran = new Random(1000);
     for (var j = 0; j < n; j++)
     {
         //v[j] = new IntVariable(net, ran.Next(200));
         v[j] = new IntVariable(net, 10 * (j + 1)+2);
     }
     new Element(net, x, i, v);
     runExample(net, Solver.Default);
 }
Ejemplo n.º 21
0
 public virtual IntVariable Add(int value)
 {
     Network net = Network;
     var x = new IntVariable(net);
     // x = this + value
     new IntArith(net, IntArith.Add, x, this, new IntVariable(net, value));
     return x;
 }
Ejemplo n.º 22
0
 public virtual IntVariable Add(IntVariable v)
 {
     Network net = Network;
     var x = new IntVariable(net);
     // x = this + v
     new IntArith(net, IntArith.Add, x, this, v);
     return x;
 }
Ejemplo n.º 23
0
 public virtual IntVariable Abs()
 {
     Network net = Network;
     var x = new IntVariable(net);
     // x = Abs(this)
     new IntFunc(net, IntFunc.Abs, x, this);
     return x;
 }
Ejemplo n.º 24
0
        public virtual void Gt(IntVariable v, ConstraintTypes cType, int weight)
        {
            Network net = Network;

            new IntComparison(net, IntComparison.Gt, this, v, cType, weight);
        }
Ejemplo n.º 25
0
 public virtual void Lt(IntVariable v)
 {
     Lt(v, ConstraintTypes.Hard);
 }
Ejemplo n.º 26
0
 public virtual IntVariable Subtract(IntVariable v)
 {
     Network net = Network;
     var x = new IntVariable(net);
     // x = this - v
     new IntArith(net, IntArith.Subtract, x, this, v);
     return x;
 }
Ejemplo n.º 27
0
 public virtual void NotEquals(IntVariable v)
 {
     NotEquals(v, ConstraintTypes.Hard);
 }
Ejemplo n.º 28
0
 public virtual void NotEquals(IntVariable v, ConstraintTypes cType, int weight)
 {
     Network net = Network;
     new NotEquals(net, this, v, cType, weight);
 }
Ejemplo n.º 29
0
 public virtual IntVariable Multiply(int value)
 {
     Network net = Network;
     var x = new IntVariable(net);
     // x = this * value
     new IntArith(net, IntArith.MULTIPLY, x, this, new IntVariable(net, value));
     return x;
 }
Ejemplo n.º 30
0
 public virtual IntVariable Min(int value)
 {
     Network net = Network;
     var x = new IntVariable(net);
     // x = Min(this, value)
     new IntArith(net, IntArith.MIN, x, this, new IntVariable(net, value));
     return x;
 }
Ejemplo n.º 31
0
 public virtual void Lt(IntVariable v, ConstraintTypes cType, int weight)
 {
     Network net = Network;
     new IntComparison(net, IntComparison.Lt, this, v, cType, weight);
 }
Ejemplo n.º 32
0
 public void Equals(IntVariable v)
 {
     Equals(v, ConstraintTypes.Hard);
 }
Ejemplo n.º 33
0
 public virtual void Gt(IntVariable v, ConstraintTypes cType)
 {
     Gt(v, cType, 0);
 }
Ejemplo n.º 34
0
 public void Equals(IntVariable v, ConstraintTypes cType)
 {
     Equals(v, cType, 0);
 }
Ejemplo n.º 35
0
 public virtual void Ge(IntVariable v)
 {
     Ge(v, ConstraintTypes.Hard);
 }
Ejemplo n.º 36
0
 public virtual void Ge(IntVariable v, ConstraintTypes cType)
 {
     Ge(v, cType, 0);
 }
Ejemplo n.º 37
0
 public void Equals(IntVariable v)
 {
     Equals(v, ConstraintTypes.Hard);
 }
Ejemplo n.º 38
0
 public virtual void Lt(IntVariable v, ConstraintTypes cType)
 {
     Lt(v, cType, 0);
 }
Ejemplo n.º 39
0
 public void Equals(IntVariable v, ConstraintTypes cType)
 {
     Equals(v, cType, 0);
 }
Ejemplo n.º 40
0
 public virtual IntVariable Min(IntVariable v)
 {
     Network net = Network;
     var x = new IntVariable(net);
     // x = Min(this, v)
     new IntArith(net, IntArith.MIN, x, this, v);
     return x;
 }
Ejemplo n.º 41
0
 public virtual void NotEquals(IntVariable v)
 {
     NotEquals(v, ConstraintTypes.Hard);
 }
Ejemplo n.º 42
0
 public virtual IntVariable Multiply(IntVariable v)
 {
     Network net = Network;
     var x = new IntVariable(net);
     // x = this * v
     new IntArith(net, IntArith.MULTIPLY, x, this, v);
     return x;
 }
Ejemplo n.º 43
0
 public virtual void NotEquals(IntVariable v, ConstraintTypes cType)
 {
     NotEquals(v, cType, 0);
 }
Ejemplo n.º 44
0
 public virtual IntVariable Negate()
 {
     Network net = Network;
     var x = new IntVariable(net);
     // x = - this
     new IntFunc(net, IntFunc.Negate, x, this);
     return x;
 }
Ejemplo n.º 45
0
        public virtual void NotEquals(IntVariable v, ConstraintTypes cType, int weight)
        {
            Network net = Network;

            new NotEquals(net, this, v, cType, weight);
        }
Ejemplo n.º 46
0
 public virtual void NotEquals(IntVariable v, ConstraintTypes cType)
 {
     NotEquals(v, cType, 0);
 }
Ejemplo n.º 47
0
 public virtual void Le(IntVariable v)
 {
     Le(v, ConstraintTypes.Hard);
 }
Ejemplo n.º 48
0
 public virtual IntVariable Sign()
 {
     Network net = Network;
     var x = new IntVariable(net);
     // x = Sign(this)
     new IntFunc(net, IntFunc.Sign, x, this);
     return x;
 }
Ejemplo n.º 49
0
 public virtual void Le(IntVariable v, ConstraintTypes cType)
 {
     Le(v, cType, 0);
 }
Ejemplo n.º 50
0
 public virtual IntVariable Subtract(int value)
 {
     Network net = Network;
     var x = new IntVariable(net);
     // x = this - value
     new IntArith(net, IntArith.Subtract, x, this, new IntVariable(net, value));
     return x;
 }
Ejemplo n.º 51
0
 public virtual void Gt(IntVariable v)
 {
     Gt(v, ConstraintTypes.Hard);
 }