Esempio n. 1
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();
            //------------------------------------------------------------------------------------------
        }
Esempio n. 2
0
 /// <summary> Constructor.
 /// (for invocation by subclass constructors, typically implicit)
 /// </summary>
 protected internal Constraint(Network net)
 {
     Network = net;
     Index = -1;
     Network.ADD(this);
     CType = ConstraintTypes.Hard;
 }
Esempio n. 3
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();
        }
Esempio n. 4
0
 public Relation(Network net, Variable v0, bool[][] rel, Variable v1, ConstraintTypes cType, int weight)
     : base(net, cType, weight)
 {
     _rel = rel;
     _v0 = v0;
     _v1 = v1;
 }
Esempio n. 5
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();
 }
Esempio n. 6
0
 public Element(Network net, Variable v0, Variable v1, Variable[] v, ConstraintTypes cType, int weight)
     : base(net, cType, weight)
 {
     _v0 = v0;
     _v1 = v1;
     _v = (Variable[])v.Clone();
 }
Esempio n. 7
0
 /// <summary> Constructor.
 /// (for invocation by subclass constructors, typically implicit)
 /// </summary>
 protected internal Constraint(Network net, ConstraintTypes cType)
 {
     Network = net;
     Index = -1;
     Network.ADD(this);
     CType = cType;
 }
Esempio n. 8
0
 /// <summary> Constructor.
 /// (for invocation by subclass constructors, typically implicit)
 /// </summary>
 protected internal Constraint(Network net, ConstraintTypes cType, int weight)
 {
     Network = net;
     Index = -1;
     Weight = weight;
     Network.ADD(this);
     CType = cType;
 }
Esempio n. 9
0
 public Semantics(Cell baseCell, Dictionary<string, Cell> cells,
     Network network, SpreadSheet spreadSheet)
 {
     _baseCell = baseCell;
       _cells = cells;
       _network = network;
       _spreadSheet = spreadSheet;
 }
Esempio n. 10
0
 public Sequential(Network net, Variable[] v, int[] l, ConstraintTypes cType, int weight)
     : base(net, cType, weight)
 {
     _v = new Variable[v.Length];
     v.CopyTo(_v, 0);
     _l = new int[l.Length];
     l.CopyTo(_l, 0);
 }
Esempio 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);
 }
Esempio n. 12
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);
 }
Esempio n. 13
0
 /// <summary> Constructs a variable of the network
 /// with an initial domain <tt>d</tt>
 /// and a name specified by the parameter <tt>name</tt>.
 /// When the parameter <tt>name</tt> is <tt>null</tt>,
 /// default names (<tt>v1</tt>, <tt>v2</tt>, and so on) are used.
 /// </summary>
 /// <param name="net">the network
 /// </param>
 /// <param name="d">the initial domain
 /// </param>
 /// <param name="name">the name of the variable, or <tt>null</tt> for a default name
 /// </param>
 public Variable(Network net, Domain d, String name)
 {
     Network = net;
     Domain = d;
     _modified = true;
     _watch = false;
     Name = name ?? "v" + (_count++);
     Network.ADD(this);
 }
Esempio n. 14
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();
        }
Esempio n. 15
0
 public Code(Network network)
 {
     System.Collections.IList constraints = network.Constraints;
     conditions = new Condition[constraints.Count];
     for (var i = 0; i < conditions.Length; i++)
     {
         var c = network.GetConstraint(i);
         conditions[i] = c.ExtractCondition();
     }
 }
Esempio n. 16
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);
 }
Esempio n. 17
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);
 }
Esempio n. 18
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultSolver"/> class.  Constructs a branch-and-bound solver for the given network, options, and name.
        /// </summary>
        /// <param name="network">
        /// the constraint network
        /// </param>
        /// <param name="options">
        /// the options for search strategy, or Default for default search strategy
        /// </param>
        /// <param name="name">
        /// the name of the solver, or <tt>null</tt> for a default name
        /// </param>
        public DefaultSolver(Network network, int options, string name)
            : base(network, options, name)
        {
            // get all soft constraints
            _softConstraints = network.Constraints.Cast<Constraint>()
                .Where(soft => soft.CType == ConstraintTypes.Soft).ToArray();

            // get all values that have equal preferences
            _valuesWhichHavePreferences = _softConstraints
                .Where(soft => soft is Equals).Cast<Equals>()
                .Select(s => ((IntDomain)s.Vars[1].Domain).Minimum())
                .Distinct().
                Union(_softConstraints
                        .Where(soft => soft is NotEquals).Cast<NotEquals>()
                        .Select(s => ((IntDomain)s.Vars[1].Domain).Minimum())
                        .Distinct()).ToArray();

            GenerateOnlySameOrBetterWeightedSolutions = false;
            Random = new Random();
        }
Esempio n. 19
0
        /// <summary> Constructs a solution from the given network.</summary>
        /// <param name="network">the constraint network
        /// </param>
        public Solution(Network network)
        {
            _network = network;
            _objectiveDomain = null;
            if (network.Objective != null)
            {
                _objectiveDomain = network.Objective.Domain;
            }
            System.Collections.IList variables = network.Variables;
            _bindings = new Domain[variables.Count];
            System.Collections.IEnumerator vs = variables.GetEnumerator();
            while (vs.MoveNext())
            {
                var v = (Variable) vs.Current;
                _bindings[v.Index] = v.Domain;
            }
            // Do th efollowing for Soft constraints only
            //SwapValuesToPreferences();

            _code = new Code(network);
        }
Esempio n. 20
0
 /// <summary> Constructs a random-walk solver for the given network, option, and name.</summary>
 /// <param name="network">the constraint network
 /// </param>
 /// <param name="option">the option for search strategy, or Default for default search strategy
 /// </param>
 /// <param name="name">the name of the solver, or <tt>null</tt> for a default name
 /// </param>
 public LocalSearch(Network network, int option, String name)
     : base(network, option, name)
 {
 }
Esempio n. 21
0
 /// <summary> Constructs a random-walk solver for the given network and name.
 /// This constructor is equivalent to <tt>LocalSearch(network, Default, name)</tt>.
 /// </summary>
 /// <param name="network">the constraint network
 /// </param>
 /// <param name="name">the name of the solver
 /// </param>
 public LocalSearch(Network network, String name)
     : this(network, Default, name)
 {
 }
Esempio n. 22
0
 /// <summary> Constructs a random-walk solver for the given network and option.
 /// This constructor is equivalent to <tt>LocalSearch(network, option, null)</tt>.
 /// </summary>
 /// <param name="network">the constraint network
 /// </param>
 /// <param name="option">the option for search strategy
 /// </param>
 public LocalSearch(Network network, int option)
     : this(network, option, null)
 {
 }
Esempio n. 23
0
 /// <summary> Constructs a random-walk solver for the given network.
 /// This constructor is equivalent to <tt>LocalSearch(network, Default, null)</tt>.
 /// </summary>
 /// <param name="network">the constraint network
 /// </param>
 public LocalSearch(Network network)
     : this(network, Default, null)
 {
 }
Esempio n. 24
0
 private IntArith(Network net, int a, Variable[] v, ConstraintTypes cType, int weight)
     : base(net, cType, weight)
 {
     _arith = a;
     _v = v;
 }
Esempio n. 25
0
 private IntArith(Network net, int a, Variable[] v, ConstraintTypes cType = ConstraintTypes.Hard)
     : this(net, a, v, cType, 0)
 {
 }
Esempio n. 26
0
 public IntArith(Network net, int a, int x0, Variable v1, Variable v2, ConstraintTypes cType, int weight)
     : this(net, a, new IntVariable(net, x0), v1, v2, cType, weight)
 {
 }
Esempio n. 27
0
 public IntArith(Network net, int a, Variable v0, int x1, Variable v2, ConstraintTypes cType, int weight)
     : this(net, a, v0, new IntVariable(net, x1), v2, cType, weight)
 {
 }
Esempio n. 28
0
 public IntArith(Network net, int a, Variable v0, Variable v1, int x2, ConstraintTypes cType, int weight)
     : this(net, a, v0, v1, new IntVariable(net, x2), cType, weight)
 {
 }
Esempio n. 29
0
 public IntArith(Network net, int a, Variable v0, Variable v1, Variable v2, ConstraintTypes cType, int weight)
     : this(net, a, new[] { v0, v1, v2 }, cType, weight)
 {
 }
Esempio n. 30
0
 public IntArith(Network net, int a, int x0, Variable v1, Variable v2)
     : this(net, a, new IntVariable(net, x0), v1, v2)
 {
 }