Пример #1
0
    public static void Main(String[] args)
    {
        Network     net = new Network();
        IntVariable J   = new IntVariable(net, 0, 9);
        IntVariable A   = new IntVariable(net, 0, 9);
        IntVariable V   = new IntVariable(net, 0, 9);
        IntVariable C   = new IntVariable(net, 0, 9);
        IntVariable R   = new IntVariable(net, 0, 9);
        IntVariable E   = new IntVariable(net, 0, 9);
        IntVariable M   = new IntVariable(net, 0, 9);
        IntVariable S   = new IntVariable(net, 0, 9);
        IntVariable O   = new IntVariable(net, 0, 9);
        IntVariable L   = new IntVariable(net, 0, 9);

        new NotEquals(net, new IntVariable[] { J, A, V, C, R, E, M, S, O, L });
        J.NotEquals(0);
        C.NotEquals(0);
        S.NotEquals(0);
        IntVariable JAVA   = J.Multiply(1000).Add(A.Multiply(100)).Add(V.Multiply(10)).Add(A);
        IntVariable CREAM  = C.Multiply(10000).Add(R.Multiply(1000)).Add(E.Multiply(100)).Add(A.Multiply(10)).Add(M);
        IntVariable SOLVER = S.Multiply(100000).Add(O.Multiply(10000)).Add(L.Multiply(1000)).Add(V.Multiply(100)).Add(E.Multiply(10)).Add(R);

        JAVA.Add(CREAM).Equals(SOLVER);
        Solver solver = new DefaultSolver(net);

        for (solver.Start(); solver.WaitNext(); solver.Resume())
        {
            Solution solution = solver.Solution;
            Console.Out.WriteLine(solution.GetIntValue(JAVA) + " + " + solution.GetIntValue(CREAM) + " = " + solution.GetIntValue(SOLVER));
        }
        solver.Stop();
        Console.ReadLine();
    }
Пример #2
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++)
                {
                    sol[c][i] = solution.GetIntValue(q[i]);
                }
                c++;
            }
            solver.Stop();
        }
        public void DefaultSolverWithDefaultStrategies_CanSolvePuzzle_Test()
        {
            var defaultSolver = new DefaultSolver();
            var sudokuPuzzle  = defaultSolver.Solve(_sudoku);

            Assert.That(sudokuPuzzle.Cells[0, 0].Value, Is.EqualTo(4));
        }
Пример #4
0
        public static void Main(string[] args)
        {
            Network net = new Network();

            IntVariable X = new IntVariable(net, 0, 708);
            IntVariable Y = new IntVariable(net, 0, 708);
            IntVariable Z = new IntVariable(net, 0, 708);
            IntVariable 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())
            {
                Solution 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();
        }
Пример #5
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();
        }
        public void DefaultSolverWithOnlyEliminationStrategies_CannotSolvePuzzle_Test()
        {
            var defaultSolver = new DefaultSolver(new BasicElimination());
            var sudokuPuzzle  = defaultSolver.Solve(_sudoku);

            Assert.That(sudokuPuzzle.Cells[0, 0].Value, Is.EqualTo(0));
            Assert.That(sudokuPuzzle.Cells[0, 0].CanBe.Count, Is.EqualTo(5));
        }
        public void SolveSingleStepForSolvedPuzzle_ReturnsNull_Test()
        {
            var defaultSolver = new DefaultSolver();
            var sudokuPuzzle  = defaultSolver.Solve(_sudoku);

            Assert.That(sudokuPuzzle.IsSolved);
            Assert.That(defaultSolver.SolveSingleStep(sudokuPuzzle), Is.Null);
        }
Пример #8
0
    internal static void solve()
    {
        setProblem();

        Solver solver = new DefaultSolver(net);

        for (solver.Start(); solver.WaitNext(); solver.Resume())
        {
            Solution solution = solver.Solution;
            printSolution(solution);
        }
        solver.Stop();
    }
Пример #9
0
    internal static void golomb(int m)
    {
        int n = (1 << (m - 1)) - 1;
        Network net = new Network();
        IntVariable[] a = new IntVariable[m];
        a[0] = new IntVariable(net, 0);
        for (int i = 1; i < m; i++)
        {
            a[i] = new IntVariable(net, 1, n);
            a[i - 1].Lt(a[i]);
        }
        IntVariable[] d = new IntVariable[m * (m - 1) / 2];
        int k = 0;
        for (int i = 0; i < m; i++)
        {
            for (int j = i + 1; j < m; j++)
            {
                d[k++] = a[j].Subtract(a[i]);
            }
        }
        //d[0].lt((d[m - 1]));
        new NotEquals(net, d);
        net.Objective=a[m - 1];

        Solver solver = new DefaultSolver(net, Solver.Minimize);
        Solution solution;
        for (solver.Start(); solver.WaitNext(); solver.Resume())
        {
            solution = solver.Solution;
            //estSolution = solver.BestSolution;

            Console.Out.Write("0");
                   for (int i = 1; i < m; i++)
                   {
                       Console.Out.Write("," + solution.GetIntValue(a[i]));
                   }
                   Console.Out.WriteLine();
        }
        solver.Stop();
        solution = solver.FindBest();
        Console.Out.WriteLine("===========================");
        Console.Out.Write("0");
        for (int i = 1; i < m; i++)
        {
            Console.Out.Write("," + solution.GetIntValue(a[i]));
        }
        Console.Out.WriteLine();
    }
Пример #10
0
    public static void Main(String[] args)
    {
        Network net = new Network();
        int     n   = 3;
        int     sum = n * (n * n + 1) / 2;

        IntVariable[][] v = new IntVariable[n][];
        for (int i = 0; i < n; i++)
        {
            v[i] = new IntVariable[n];
        }
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                v[i][j] = new IntVariable(net, 1, n * n);
            }
        }
        IntVariable[] u = new IntVariable[] { v[0][0], v[0][1], v[0][2], v[1][0], v[1][1], v[1][2], v[2][0], v[2][1], v[2][2] };
        new NotEquals(net, u);
        for (int i = 0; i < n; i++)
        {
            v[i][0].Add(v[i][1]).Add(v[i][2]).Equals(sum);
        }
        for (int j = 0; j < n; j++)
        {
            v[0][j].Add(v[1][j]).Add(v[2][j]).Equals(sum);
        }
        v[0][0].Add(v[1][1]).Add(v[2][2]).Equals(sum);
        v[0][2].Add(v[1][1]).Add(v[2][0]).Equals(sum);
        Solver solver = new DefaultSolver(net);

        for (solver.Start(); solver.WaitNext(); solver.Resume())
        {
            Solution solution = solver.Solution;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.Out.Write(solution.GetIntValue(v[i][j]) + " ");
                }
                Console.Out.WriteLine();
            }
            Console.Out.WriteLine();
        }
        solver.Stop();
        Console.ReadLine();
    }
Пример #11
0
        public MainWindow()
        {
            InitializeComponent();
            TextBox.TextChanged += (s, e) => TextBox.ScrollToEnd();

            var puzzleProvider = new TrueMagicSudokuGeneratorPuzzleProvider();
            var solver         = new DefaultSolver();
            var viewModel      = new MainViewModel(puzzleProvider, solver);

            viewModel.LoadGame            += OnLoadGame;
            viewModel.SaveGame            += OnSaveGame;
            viewModel.ExitGame            += OnExitGame;
            viewModel.InvalidSudokuLoaded += OnInvalidSudokuLoaded;

            DataContext = viewModel;
        }
Пример #12
0
        public Form1()
        {
            InitializeComponent();
            new Form1(map);
            //Form1 fc = new Form1(map);
            Network net = new Network();
            int     n   = neighbors.Length;

            IntVariable[] region = new IntVariable[n];
            for (int i = 0; i < n; i++)
            {
                region[i] = new IntVariable(net, 1, 4);
            }
            for (int i = 0; i < n; i++)
            {
                IntVariable v = region[neighbors[i][0] - 1];
                for (int j = 1; j < neighbors[i].Length; ++j)
                {
                    if (neighbors[i][0] < neighbors[i][j])
                    {
                        v.NotEquals(region[neighbors[i][j] - 1]);
                    }
                }
            }
            Solver solver = new DefaultSolver(net);

            solver.Start();
            //while (true)
            {
                //fc.setStateWait();
                //fc.waitAction();
                //if (fc.state == QUIT)
                //   break;
                if (!solver.WaitNext())
                {
                    // break;
                    setSolution(solver.Solution, region);
                }
                solver.Resume();
            }
            solver.Stop();
        }
Пример #13
0
        internal static void runExample(Network net, int opt)
        {
            Console.Out.WriteLine("# Problem");
            //UPGRADE_TODO: Method 'java.io.PrintStream.println' was converted to 'System.Console.Out.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintStreamprintln_javalangObject'"
            Console.Out.WriteLine(net);
            Console.Out.WriteLine("# Solutions");
            Solver solver = new DefaultSolver(net, opt);

            for (solver.Start(); solver.WaitNext(); solver.Resume())
            {
                Solution solution = solver.Solution;
                Console.Out.WriteLine(solution);
            }
            solver.Stop();
            long count = solver.GetCount();
            long time  = solver.GetElapsedTime();

            Console.Out.WriteLine("Found " + count + " solutions in " + time + " milli seconds");
            Console.Out.WriteLine();
        }
Пример #14
0
    public static void Main(String[] args)
    {
        // Create a constraint network
        Network net = new Network();
        // Declare variables
        IntVariable x = new IntVariable(net);
        IntVariable y = new IntVariable(net);

        // x >= 0
        x.Ge(0);
        // y >= 0
        y.Ge(0);
        // x + y == 7
        x.Add(y).Equals(7);
        // 2x + 4y == 20
        x.Multiply(2).Add(y.Multiply(4)).Equals(20);
        // Solve the problem
        Solver solver = new DefaultSolver(net);

        /*Solution solution = solver.findAll(Solution);
         * int xv = solution.GetIntValue(x);
         * int yv = solution.GetIntValue(y);
         * Console.Out.WriteLine("x = " + xv + ", y = " + yv);
         */

        for (solver.Start(); solver.WaitNext(); solver.Resume())
        {
            Solution solution = solver.Solution;
            int      xv       = solution.GetIntValue(x);
            int      yv       = solution.GetIntValue(y);
            Console.Out.WriteLine("x8 = " + xv + ", y = " + yv);
        }
        solver.Stop();


        //solver.findAll(new FirstStepHandler(x, y));
        Console.In.ReadLine();
    }
Пример #15
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();
            //------------------------------------------------------------------------------------------
        }
Пример #16
0
        public static void solve()
        {
            int FAMILIES = 2;
            int CHILDREN = 6;
            int maxAge   = 9;

            Network net = new Network();

            IntVariable[][] isBoy = new IntVariable[FAMILIES][];
            for (int i = 0; i < FAMILIES; i++)
            {
                isBoy[i] = new IntVariable[CHILDREN];
            }
            IntVariable[][] age = new IntVariable[FAMILIES][];
            for (int i2 = 0; i2 < FAMILIES; i2++)
            {
                age[i2] = new IntVariable[CHILDREN];
            }
            IntVariable[][] boyAge = new IntVariable[FAMILIES][];
            for (int i3 = 0; i3 < FAMILIES; i3++)
            {
                boyAge[i3] = new IntVariable[CHILDREN];
            }
            IntVariable[][] girlAge = new IntVariable[FAMILIES][];
            for (int i4 = 0; i4 < FAMILIES; i4++)
            {
                girlAge[i4] = new IntVariable[CHILDREN];
            }

            for (int family = 0; family < FAMILIES; family++)
            {
                for (int child = 0; child < CHILDREN; child++)
                {
                    isBoy[family][child] = new IntVariable(net, 0, 1);
                    age[family][child]   = new IntVariable(net, 0, maxAge);
                    if (child > 0)
                    {
                        age[family][child].Gt(age[family][child - 1]);
                    }
                    boyAge[family][child]  = age[family][child].Multiply(isBoy[family][child]);
                    girlAge[family][child] = age[family][child].Subtract(boyAge[family][child]);
                }
            }

            isBoy[0][0].Equals(0);
            isBoy[1][0].Equals(0);
            age[1][0].Equals(0);

            for (int family = 0; family < FAMILIES; family++)
            {
                sum(isBoy[family]).Equals(3);
                sum(boyAge[family]).Equals(sum(girlAge[family]));
                sum2(boyAge[family]).Equals(sum2(girlAge[family]));
            }
            sum(age).Equals(60);

            Solver solver = new DefaultSolver(net);

            for (solver.Start(); solver.WaitNext(); solver.Resume())
            {
                Solution solution = solver.Solution;
                for (int family = 0; family < FAMILIES; family++)
                {
                    Console.Out.Write("Family " + family + ": ");
                    for (int child = 0; child < CHILDREN; child++)
                    {
                        int _isBoy = solution.GetIntValue(isBoy[family][child]);
                        int _age   = solution.GetIntValue(age[family][child]);
                        Console.Out.Write(_isBoy != 0 ? "Boy  " : "Girl ");
                        Console.Out.Write(_age + "  ");
                    }
                    Console.Out.WriteLine();
                }
            }
            Console.ReadLine();
        }
Пример #17
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="net"></param>
        /// <param name="course"></param>
        /// <param name="noOfCourses"></param>
        private void Solve(CourseNetwork net, IntVariable[] course, int noOfCourses)
        {
            //net.Objective = course[0];
            Solver solver;

            if (radioButton6.Checked)
            {
                solver = new IterativeBranchAndBoundSearch(net, Solver.Minimize);
            }
            else if (radioButton4.Checked)
            {
                solver = new DefaultSolver(net, Solver.Minimize);
                solver.SolverStrategy = (Solver.StrategyMethod)numericUpDown3.Value;
            }
            else if (radioButton3.Checked)
            {
                //int opt = Solver.BETTER;
                solver = new TabooSearch(net, Solver.Minimize);
            }
            else
            {
                net.Objective = course[0];
                solver        = new SimulatedAnneallingSearch(net, Solver.Minimize);
            }

            long timer = DateTime.Now.Ticks;
            int  count = 1;

            //StreamWriter sw = new StreamWriter(".\\out.txt");
            DBSolution.DeleteAll();
            Notes = new IList[(int)numericUpDown1.Value];
            Solution bestSolution = null;

            for (solver.Start((long)numericUpDown2.Value); solver.WaitNext(); solver.Resume())
            {
                Solution sol = solver.Solution;
                if (count == 1)
                {
                    bestSolution = sol;
                }
                else
                {
                    if (bestSolution != null)
                    {
                        if (sol.Weight > bestSolution.Weight)
                        {
                            bestSolution = sol;
                        }
                    }
                }
                Notes[count - 1] = new ArrayList {
                    "Weight= " + sol.Weight + "\n"
                };
                for (int i = 0; i < net.Professors.Count; i++)
                {
                    int pcount = 0;
                    for (int j = 0; j < net.Variables.Count; j++)
                    {
                        if (!((Variable)net.Variables[j]).IsValueType)
                        {
                            if (sol.GetIntValue(((Variable)net.Variables[j])) == i)
                            {
                                pcount++;
                            }
                        }
                    }
                    if (pcount < ((Professor)net.Professors[i]).RealNoOfCourses)
                    {
                        Notes[count - 1].Add("Prof. " + ((Professor)net.Professors[i]).ToString() +
                                             " not consistent.. needs " +
                                             (((Professor)net.Professors[i]).RealNoOfCourses - pcount) +
                                             " assignment(s) more!!" + "\n");
                        // sw.WriteLine("Prof. " + ((Professor)net.Professors[i]).toString() + " not consistent.. needs " +
                        //     (((Professor)net.Professors[i]).Courses - pcount) + " assignment(s) more!!");
                    }
                }

                Console.Out.WriteLine();
                for (int i = 0; i < noOfCourses; i++)
                {
                    //if (!((Variable)(net.Variables[i])).IsValueType)
                    //{
                    //sw.WriteLine(course[i].Name + " = " + ((Professor)net.Professors[sol.getIntValue(course[i])]).Name);
                    var dbSolution = new DBSolution
                    {
                        SolutionID    = count,
                        CourseName    = course[i].Name,
                        ProfessorName =
                            ((Professor)net.Professors[sol.GetIntValue(course[i])]).Name
                    };
                    dbSolution.AddSolution();
                    //}
                }
                //sw.WriteLine("=================================");
                count++;
                //if (solver is DefaultSolver)
                //{
                if (count == numericUpDown1.Value + 1)
                {
                    break;
                }
                //}
                //else
                //{
                //   break;
                //}
            }
            Console.WriteLine(bestSolution);
            timer = DateTime.Now.Ticks - timer;
            //sw.WriteLine("timer: " + timer);
            //sw.WriteLine("Count=" + count);
            //sw.Close();
            solutionBS = new BindingSource();
            if (count > 1)
            {
                solutionBS.DataSource           = DBSolution.GetByID(1);
                bindingNavigator1.BindingSource = solutionBS;
                solutionViewGrid.DataSource     = solutionBS;
                solIndex          = 1;
                firstSol.Enabled  = false;
                prevSol.Enabled   = false;
                SolUpDown.Minimum = 1;
                SolUpDown.Maximum = count - 1;
                SolUpDown.Value   = 1;
                SolUpDown.Enabled = true;
                if (count == 2)
                {
                    nextSol.Enabled = false;
                    lastSol.Enabled = false;
                }
                else
                {
                    nextSol.Enabled = true;
                    lastSol.Enabled = true;
                }
            }
            else
            {
                SolUpDown.Minimum = 0;
                SolUpDown.Maximum = 0;
                SolUpDown.Enabled = false;
                firstSol.Enabled  = false;
                prevSol.Enabled   = false;
                nextSol.Enabled   = false;
                lastSol.Enabled   = false;
            }
            label2.Text = "";
            if (count > 1)
            {
                for (int y = 0; y < Notes[solIndex - 1].Count; y++)
                {
                    label2.Text += Convert.ToString(Notes[solIndex - 1][y]);
                }
            }
            numberOfSolutions = count - 1;
            solutionViewGrid.Columns[2].Visible = false;
            if (timer / 10000 / 1000 == 0)
            {
                MessageBox.Show((count - 1) + " Solution(s) found in " + timer / 1000.0 + " MS");
            }
            else
            {
                MessageBox.Show((count - 1) + " Solution(s) found in " + timer / 10000.0 / 1000 + " second(s)");
            }
        }
Пример #18
0
        public void sudoku(int[][] v0)
        {
            Network net = new Network();
            int     n   = 9;

            IntVariable[][] v = new IntVariable[n][];
            for (int i = 0; i < n; i++)
            {
                v[i] = new IntVariable[n];
            }
            IntVariable[] vs = new IntVariable[n];
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (v0[i][j] == 0)
                    {
                        v[i][j] = new IntVariable(net, 1, n);
                    }
                    else
                    {
                        v[i][j] = new IntVariable(net, v0[i][j]);
                    }
                }
            }
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    vs[j] = v[i][j];
                }
                new NotEquals(net, vs);
            }
            for (int j = 0; j < n; j++)
            {
                for (int i = 0; i < n; i++)
                {
                    vs[i] = v[i][j];
                }
                new NotEquals(net, vs);
            }
            for (int i0 = 0; i0 < n; i0 += 3)
            {
                for (int j0 = 0; j0 < n; j0 += 3)
                {
                    int k = 0;
                    for (int i = i0; i < i0 + 3; i++)
                    {
                        for (int j = j0; j < j0 + 3; j++)
                        {
                            vs[k++] = v[i][j];
                        }
                    }
                    new NotEquals(net, vs);
                }
            }
            Solver solver = new DefaultSolver(net);
            Int64  timer  = DateTime.Now.Ticks;

            //for (solver.start(); solver.waitNext(); solver.resume())
            //solver.start();
            //solver.waitNext();
            {
                Solution solution = solver.FindFirst();
                sol = new int[9][];

                for (int i = 0; i < n; i++)
                {
                    sol[i] = new int[9];
                    for (int j = 0; j < n; j++)
                    {
                        sol[i][j] = solution.GetIntValue(v[i][j]);
                    }
                }
            }
            timer = DateTime.Now.Ticks - timer;
            solver.Stop();
            Console.WriteLine("Time = " + timer / 10000);
        }
Пример #19
0
    public static void Main(String[] args)
    {
        Network net = new Network();

        ft06(net);

        String solverName = "ibb";
        int    opt        = Solver.Minimize;
        long   timeout    = 180000;

        if (args.Length >= 1)
        {
            solverName = args[0];
        }

        Solver solver;

        if (solverName.Equals("bb"))
        {
            solver = new DefaultSolver(net, opt, "bb");
        }
        else if (solverName.Equals("random"))
        {
            solver = new LocalSearch(net, opt, "rs");
        }
        else if (solverName.Equals("sa"))
        {
            solver = new SimulatedAnneallingSearch(net, opt, "sa");
        }
        else if (solverName.Equals("ibb"))
        {
            solver = new IterativeBranchAndBoundSearch(net, opt, "ibb");
        }
        else if (solverName.Equals("taboo"))
        {
            solver = new TabooSearch(net, opt, "taboo");
        }
        else
        {
            Solver sa  = new SimulatedAnneallingSearch((Network)net.Clone(), opt, "sa");
            Solver ibb = new IterativeBranchAndBoundSearch((Network)net.Clone(), opt, "ibb");
            solver = new ParallelSolver(new Solver[] { sa, ibb });
        }
        solver.SolverStrategy = Solver.StrategyMethod.Bisect;
        //Cream.Monitor monitor = new Monitor();
        //monitor.setX(0, (int)(timeout / 1000));
        //solver.setMonitor(monitor);

        Console.Out.WriteLine("Start " + solver + ", timeout = " + timeout + " msecs");

        Solution bestSolution;
        int      c = 0;

        if (true)
        {
            for (solver.Start(timeout); solver.WaitNext(); solver.Resume())
            {
                Solution solution = solver.Solution;
                Console.Out.WriteLine(++c);
                Console.Out.WriteLine(solution);
                int value_Renamed = solution.ObjectiveIntValue;
                Console.Out.WriteLine(value_Renamed);
                Console.Out.WriteLine("=======================");
            }
            solver.Stop();
            bestSolution = solver.BestSolution;
        }
        else
        {
            bestSolution = solver.FindBest(timeout);
        }

        Console.Out.WriteLine("Best = " + bestSolution.ObjectiveIntValue);
        Console.Out.WriteLine("Best = " + bestSolution);
        Console.In.ReadLine();
    }
Пример #20
0
    public static void magic(int n)
    {
        Network net = new Network();

        IntVariable[][] square = new IntVariable[n][];
        for (int i = 0; i < n; i++)
        {
            square[i] = new IntVariable[n];
        }

        // All squares have different numbers 1 .. n*n
        IntVariable[] v = new IntVariable[n * n];
        int           k = 0;

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                square[i][j] = new IntVariable(net, 1, n * n);
                v[k++]       = square[i][j];
            }
        }
        new NotEquals(net, v);

        // Sum of each row is n*(n*n+1)/2
        IntVariable s;
        int         sum = n * (n * n + 1) / 2;

        for (int i = 0; i < n; i++)
        {
            s = square[i][0];
            for (int j = 1; j < n; j++)
            {
                s = s.Add(square[i][j]);
            }
            s.Equals(sum);
        }

        // Sum of each column is n*(n*n+1)/2
        for (int j = 0; j < n; j++)
        {
            s = square[0][j];
            for (int i = 1; i < n; i++)
            {
                s = s.Add(square[i][j]);
            }
            s.Equals(sum);
        }

        // Sum of down-diagonal is n*(n*n+1)/2
        s = square[0][0];
        for (int i = 1; i < n; i++)
        {
            s = s.Add(square[i][i]);
        }
        s.Equals(sum);

        // Sum of up-diagonal is n*(n*n+1)/2
        s = square[0][n - 1];
        for (int i = 1; i < n; i++)
        {
            s = s.Add(square[i][n - i - 1]);
        }
        s.Equals(sum);

        // Left-upper corner is minimum
        square[0][0].Lt(square[0][n - 1]);
        square[0][0].Lt(square[n - 1][0]);
        square[0][0].Lt(square[n - 1][n - 1]);

        // Upper-right is less than lower-left
        square[0][n - 1].Lt(square[n - 1][0]);

        Console.Out.WriteLine("Start");
        long   time0  = (DateTime.Now.Ticks - 621355968000000000) / 10000;
        int    count  = 0;
        bool   output = true;
        Solver solver = new DefaultSolver(net);

        for (solver.Start(); solver.WaitNext(); solver.Resume())
        {
            if (output)
            {
                Solution solution = solver.Solution;
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        Console.Out.Write(solution.GetIntValue(square[i][j]) + " ");
                    }
                    Console.Out.WriteLine();
                }
                Console.Out.WriteLine();
            }
            count++;
        }
        solver.Stop();
        int time = (int)(((DateTime.Now.Ticks - 621355968000000000) / 10000 - time0) / 1000);

        Console.Out.WriteLine(count + " solutions found in " + time + " seconds");
    }
Пример #21
0
        static void Main(string[] args)
        {
            CourseNetwork net   = new CourseNetwork();
            StreamReader  re    = File.OpenText("courses.txt");//"Anum1.txt");//"fn1.txt"); //"numfile.txt");
            string        input = re.ReadLine();
            int           n;

            try
            {
                n = Convert.ToInt16(input);
            }
            catch (Exception)
            {
                Console.WriteLine("Failed to read no of  profs ");
                return;
            }
            Professor[] professors = new Professor[n];
            for (int i = 0; i < n; i++)
            {
                input = re.ReadLine();
                int      m;
                string[] s = input.Split(' ');

                try
                {
                    m = Convert.ToInt16(s[1]);    // no of events
                }
                catch
                {
                    Console.WriteLine("Failed to read no of courses per prof " + s[0]);
                    return;
                }
                professors[i] = new Professor(net, m, s[0]);
            }



            IntVariable[] course = new IntVariable[6];
            int           max    = net.Professors.Count - 1;

            course[0] = new IntVariable(net, 0, max, "CS110");
            course[1] = new IntVariable(net, 0, max, "CS200");
            course[2] = new IntVariable(net, 0, max, "CS420");
            course[3] = new IntVariable(net, 0, max, "CS310");
            course[4] = new IntVariable(net, 0, max, "MA200");
            course[5] = new IntVariable(net, 0, max, "ST101");
            course[0].NotEquals(course[1]);
            course[2].NotEquals(course[3]);
            course[4].NotEquals(course[5]);
            course[4].Equals(1);
            re.Close();
            Professor dummyPorf = professors[net.Professors.Count - 1];

            dummyPorf.Courses = 0;    // always initialize with 0
            Count cc = new Count(net, course);


            int sum = 0;

            if (net.Professors != null)
            {
                foreach (Professor p in net.Professors)
                {
                    sum += p.Courses;
                }
            }
            if (sum < net.Variables.Count)
            {
                dummyPorf.Courses = net.Variables.Count - sum;
            }
            //net.Objective = course[5];
            //int opt = Solver.BETTER;
            Solver solver = new DefaultSolver(net);
            long   timer  = DateTime.Now.Ticks;
            int    count  = 0;

            for (solver.Start(); solver.WaitNext(); solver.Resume())
            {
                Solution sol = solver.Solution;
                for (int i = 0; i < net.Professors.Count; i++)
                {
                    int pcount = 0;
                    foreach (Variable v in net.Variables)
                    {
                        if (sol.GetIntValue(v) == i)
                        {
                            pcount++;
                        }
                    }
                    if (pcount < ((Professor)net.Professors[i]).Courses)
                    {
                        Console.WriteLine("Prof. " + ((Professor)net.Professors[i]).ToString() + " not consistent.. needs " +
                                          (((Professor)net.Professors[i]).Courses - pcount) + " assignment(s) more!!");
                    }
                }

                Console.Out.WriteLine();
                foreach (Variable v in net.Variables)
                {
                    Console.Out.WriteLine(v.Name + " = " + ((Professor)net.Professors[sol.GetIntValue(v)]).Name);
                }
                Console.Out.WriteLine("=================================");
                count++;
            }
            timer = DateTime.Now.Ticks - timer;
            Console.Out.WriteLine("timer: " + timer);
            Console.WriteLine("Count=" + count);
            Console.ReadLine();
        }