Exemple #1
0
        internal void solve(MiniZincInterface mzinc, SolverName solverName)
        {
            string result = mzinc.solve(this, solverName);

            if (result.Contains("UNSATISFIABLE") || result.Trim() == "")
            {
                solved = false;
            }

            else
            {
                solved = true;

                String[] solutions = result.Split(new string[] { "==========" }, StringSplitOptions.None);

                if (solutions.Length >= 2)
                {
                    result = solutions[solutions.Length - 2];
                }



                using (StringReader reader = new StringReader(result))
                {
                    string line = string.Empty;
                    do
                    {
                        line = reader.ReadLine();
                        if (line != null)
                        {
                            if (line.Contains("=") && !line.Contains("set_"))
                            {
                                string[] x = line.Split('=');

                                Variable v = variables[x[0].Trim()];

                                if (v != null)
                                {
                                    v.solvedValue = x[1].Trim().Split(';')[0];
                                }
                            }
                        }
                    } while (line != null);
                }
            }
        }
Exemple #2
0
 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (comboBox1.SelectedIndex == 0)
     {
         solver = SolverName.gecode;
     }
     if (comboBox1.SelectedIndex == 1)
     {
         solver = SolverName.minizinc;
     }
     if (comboBox1.SelectedIndex == 2)
     {
         solver = SolverName.or_tools;
     }
     if (comboBox1.SelectedIndex == 3)
     {
         solver = SolverName.chuffed;
     }
     if (comboBox1.SelectedIndex == 4)
     {
         solver = SolverName.all;
     }
 }
Exemple #3
0
        internal string solve(CPInstance instance, SolverName solver)
        {
            String instance_path = instance.writeFile();

            result = "";

            gotResult = false;

            List <Solver> solvers       = new List <Solver>();
            List <Thread> solverThreads = new List <Thread>();

            processList = new List <Process>();

            if (solver == SolverName.minizinc || solver == SolverName.all)
            {
                solvers.Add(new FlatZincSolver(instance_path));
            }
            if (solver == SolverName.gecode || solver == SolverName.all)
            {
                solvers.Add(new GecodeSolver(instance_path));
            }
            if (solver == SolverName.or_tools || solver == SolverName.all)
            {
                solvers.Add(new ORToolsSolver(instance_path));
            }
            if (solver == SolverName.chuffed || solver == SolverName.all)
            {
                solvers.Add(new ChuffedSolver(instance_path));
            }



            foreach (Solver s in solvers)
            {
                solverThreads.Add(new Thread(() =>
                {
                    try
                    {
                        result = s.solve();
                        Console.Out.WriteLine("Solver sucessfully finished: " + s);
                        gotResult = true;
                    }
                    catch (Exception e)
                    {
                        Console.Out.WriteLine("Solver crashed: " + s);
                    }
                }));
            }

            WaitingScreen wait = new WaitingScreen(solverThreads);

            wait.ShowDialog();

            var list = new List <Process>(processList);

            foreach (Process process in list)
            {
                try
                {
                    if (!process.HasExited)
                    {
                        Kill(process.Id.ToString());
                    }
                }
                catch (InvalidOperationException e)
                {
                    // do nothing, process already gone
                }
            }


            if (result != "")
            {
                if (result.Contains("UNSATISFIABLE"))
                {
                    instance.unsatisfiable = true;
                    Console.WriteLine("Instance unsatisfiable!");
                }

                return(result);
            }
            else
            {
                return("");
            }
        }