Solutions. Solutions are returned by {@linkplain Solver constraint solvers}. A solution consists of {@linkplain Domain domains} for variables and {@linkplain Code a code}.
コード例 #1
0
ファイル: ParallelSolver.cs プロジェクト: samplet/HalfAndHalf
 public virtual void Solved(Solver solver, Solution sol)
 {
     lock (this)
     {
         if (Aborted || sol == null)
         {
             return ;
         }
         int oldBestValue = bestValue;
         solution = sol;
         Success();
         if (!(solver is LocalSearch))
             return ;
         if (network.Objective == null)
             return ;
         int objectiveIntValue = sol.ObjectiveIntValue;
         if (!IsBetter(objectiveIntValue, oldBestValue))
         {
             double rate;
             {
                 rate = ((LocalSearch) solver).ExchangeRate;
             }
             if (SupportClass.Random.NextDouble() < rate)
             {
                 //System.out.println(header + "Get " + best);
                 ((LocalSearch) solver).Candidate = bestSolution;
             }
         }
     }
 }
コード例 #2
0
ファイル: Solver.cs プロジェクト: kikoanis/CSharpCream
 protected internal virtual void Fail()
 {
     lock (this)
     {
         if (debug)
         {
             Console.Out.WriteLine(name + " Fail");
         }
         solution = null;
         _running = false;
         System.Threading.Monitor.PulseAll(this);
     }
 }
コード例 #3
0
ファイル: Solver.cs プロジェクト: kikoanis/CSharpCream
 protected internal virtual bool UpdateBest()
 {
     if (solution == null)
         return false;
     if (network.Objective == null)
     {
         bestSolution = solution;
         return true;
     }
     int value = solution.ObjectiveIntValue;
     if (IsBetter(value, bestValue))
     {
         bestSolution = solution;
         bestValue = value;
         return true;
     }
     return false;
 }
コード例 #4
0
ファイル: Solver.cs プロジェクト: kikoanis/CSharpCream
 /// <summary> Starts the solver in a new thread with the timeout, and immediately returns to the caller.
 /// When the <tt>timeout</tt> milliseconds have been elapsed
 /// since the Start of the solver, it stops the execution.
 /// The {@link #WaitNext()} and {@link #WaitNext(long timeout)} methods can be used
 /// to wait the next solution, or to detect the timeout.
 /// When a solution is found, the solver suspends the execution until
 /// the {@link #Resume()} method is called.
 /// You can Stop the solver anytime by calling the {@link #Stop()} method.
 /// </summary>
 /// <param name="timeout">timeout in milliseconds (non-positive value means no timeout)
 /// </param>
 public virtual void Start(long timeout)
 {
     lock (this)
     {
         // For bug fix. (ParallelSolver would not Stop)
         // Modified by Muneyuki Kawatani 05/12/09
         _thread = null;
         if (debug)
         {
             Console.Out.WriteLine(name + " Start");
         }
         startTime = (DateTime.Now.Ticks - 621355968000000000) / 10000;
         totalTimeout = timeout;
         _abort = false;
         _running = true;
         _ready = false;
         solution = null;
         _thread = new SupportClass.ThreadClass(new System.Threading.ThreadStart(Run));
         _thread.Start();
         System.Threading.Monitor.PulseAll(this);
     }
 }
コード例 #5
0
ファイル: Solver.cs プロジェクト: kikoanis/CSharpCream
 /// <summary> Clears the best solution this solver has been found.</summary>
 public void ClearBest()
 {
     bestSolution = null;
     bestValue = IsOption(Minimize) ? IntDomain.MaxValue : IntDomain.MinValue;
 }
コード例 #6
0
ファイル: Default.aspx.cs プロジェクト: kikoanis/CSharpCream
 private void doItOnce()
 {
     Session.Clear();
     net = new Network();
     John = new AllenVariable(net, 0, 40, 30, "John");
     Mary = new AllenVariable(net, 35, 60, 20, "Mary");
     Wendy = new AllenVariable(net, 0, 60, 50, "Wendy");
     Soccer = new AllenVariable(net, 30, 135, 105, "Soccer");
     John.Equals(Mary);
     John.Starts(Mary);
     John.StartedBy(Mary);
     John.Meets(Mary);
     John.Equals(Wendy);
     John.Starts(Wendy);
     John.StartedBy(Wendy);
     John.Meets(Wendy);
     John.Overlaps(Soccer);
     Mary.Finishes(Wendy);
     Mary.FinishedBy(Wendy);
     Mary.During(Soccer);
     Mary.Contains(Soccer);
     AllenDomain ad0 = (AllenDomain)(John.Domain);
     AllenDomain ad1 = (AllenDomain)(Mary.Domain);
     AllenDomain ad2 = (AllenDomain)(Wendy.Domain);
     AllenDomain ad3 = (AllenDomain)(Soccer.Domain);
     solver = new AllenSolver(net);
     for (solver.Start(); solver.WaitNext(); solver.Resume())
     {
         solution = solver.Solution;
         Session["jd"] = ad0.Duration;
         Session["md"] = ad1.Duration;
         Session["wd"] = ad2.Duration;
         Session["sd"] = ad3.Duration;
         Session["js" + Convert.ToInt16(counter)] = solution.GetIntValue(John);
         Session["ms" + Convert.ToInt16(counter)] = solution.GetIntValue(Mary);
         Session["ws" + Convert.ToInt16(counter)] = solution.GetIntValue(Wendy);
         Session["ss" + Convert.ToInt16(counter)] = solution.GetIntValue(Soccer);
         Session["jp" + Convert.ToInt16(counter)] = ad0.Duration;
         Session["mp" + Convert.ToInt16(counter)] = ad1.Duration;
         Session["wp" + Convert.ToInt16(counter)] = ad2.Duration;
         Session["sp" + Convert.ToInt16(counter)] = ad3.Duration;
         Session["jn" + Convert.ToInt16(counter)] = solution.GetIntValue(John).ToString();
         Session["mn" + Convert.ToInt16(counter)] = solution.GetIntValue(Mary).ToString();
         Session["wn" + Convert.ToInt16(counter)] = solution.GetIntValue(Wendy).ToString();
         Session["sn" + Convert.ToInt16(counter)] = solution.GetIntValue(Soccer).ToString();
         counter += 1;
     }
     Session["done"] = true;
     Session["total"] = counter;
     Session["counter"] = 0;
     total.Text = "There are " + counter + " solutions";
     counter = 0;
     Display();
     Button1.Enabled = true;
     Button2.Enabled = true;
     Button3.Enabled = true;
     Button4.Enabled = true;
     if (Convert.ToInt16(Session["counter"]) == 0)
     {
         Button1.Enabled = false;
         Button2.Enabled = false;
     }
     if (Convert.ToInt16(Session["counter"]) == Convert.ToInt16(Session["total"]) - 1)
     {
         Button3.Enabled = false;
         Button4.Enabled = false;
     }
 }
コード例 #7
0
ファイル: DefaultSolver.cs プロジェクト: kikoanis/CSharpCream
        private void GetGeneratedSolution()
        {
            var solutionWeight = GetSolutionWeight();

            // timeSpentToCheck = DateTime.Now.Ticks - timeSpentToCheck;
            if ((!IsBetterSolution(solutionWeight) || SolverStrategy != StrategyMethod.Soft) &&
                SolverStrategy == StrategyMethod.Soft) return;
            solution = new Solution(network) {Weight = solutionWeight, Time = GetElapsedTime()};
            //Console.WriteLine("solution: " + solution);
            //File.AppendAllText("c:\\test.out", "---------------------------------------\n");
            //File.AppendAllText("c:\\test.out", network.ToString());
            Success();
        }
コード例 #8
0
ファイル: AllenSolver.cs プロジェクト: kikoanis/CSharpCream
        /// <summary>
        /// This method is based on Dr Malek Mouhoub Tempor application
        /// http://www2.cs.uregina.ca/~mouhoubm/=postscript/=papers/32mouh.ps.gz
        ///  </summary>
        /// <param name="level">integer value for the level of the solver</param>
        protected internal override void Solve(int level)
        {
            // do it only once
            if (!_reductionDone)
            {
                ConvertNumericToSymbolic(); // convert from nuymeric to symbolic
                PathConsistency(); // PC2
                // AC3.1
                if (!ArcConsistency())
                {
                    return;
                }

                // ConvertNumericToSymbolic(); // convert from nuymeric to symbolic
                GetDisjunctiveConstraints(); // get all edges' disjunctive constraints
                _reductionDone = true;
            }

            Variable objective = network.Objective;
            while (!Aborted)
            {
                if (IsOption(Minimize))
                {
                    if (bestValue < IntDomain.MaxValue)
                    {
                        var d = (IntDomain)objective.Domain;
                        d = d.Delete(bestValue, IntDomain.MaxValue);
                        if (d.Empty)
                        {
                            break;
                        }

                        objective.UpdateDomain(d, trail);
                    }
                }
                else if (IsOption(Maximize))
                {
                    if (bestValue > IntDomain.MinValue)
                    {
                        var d = (IntDomain)objective.Domain;
                        d = d.Delete(IntDomain.MinValue, bestValue);
                        if (d.Empty)
                        {
                            break;
                        }

                        objective.UpdateDomain(d, trail);
                    }
                }

                bool sat = SatisfyDisjCons();
                if (Aborted || !sat)
                {
                    break;
                }

                Variable v0 = SelectVariable();
                if (v0 == null)
                {
                    IEnumerator v = network.Variables.GetEnumerator();
                    while (v.MoveNext())
                    {
                        if (((AllenVariable)v.Current).Domain.Size() == 0)
                        {
                            break;
                        }
                    }

                    solution = new Solution(network);
                    Success();
                    break;
                }

                if (v0.Domain is AllenDomain)
                {
                    var d = (AllenDomain)v0.Domain;
                    switch (Choice)
                    {
                        case (int)StrategyMethod.Step:
                            int value = d.Min();
                            if (!Aborted)
                            {
                                int t0 = trail.Size();
                                v0.UpdateDomain(new AllenDomain(value, value + d.Duration, d.Duration, d.Step), trail);
                                Solve(level + 1);
                                trail.Undo(t0);
                            }

                            if (!Aborted)
                            {
                                v0.UpdateDomain(d.Delete(value), trail);
                                continue;
                            }

                            break;

                        case (int)StrategyMethod.Enum:
                            IEnumerator iter = v0.Domain.Elements();
                            while (!Aborted && iter.MoveNext())
                            {
                                int t0 = trail.Size();
                                v0.UpdateDomain((Domain)iter.Current, trail);
                                Solve(level + 1);
                                trail.Undo(t0);
                            }

                            break;

                        case (int)StrategyMethod.Bisect:
                            int mid;
                            if (d.Min() + 1 == d.Max())
                            {
                                mid = d.Min();
                            }
                            else
                                mid = (d.Min() + d.Max()) / 2;
                            if (!Aborted)
                            {
                                int t0 = trail.Size();
                                v0.UpdateDomain(d.CapInterval(d.Min(), mid), trail);
                                Solve(level + 1);
                                trail.Undo(t0);
                            }

                            if (!Aborted)
                            {
                                int t0 = trail.Size();
                                v0.UpdateDomain(d.CapInterval(mid + 1, d.Max()), trail);
                                Solve(level + 1);
                                trail.Undo(t0);
                            }

                            break;
                    }
                }
                else
                {
                    IEnumerator iter = v0.Domain.Elements();
                    while (!Aborted && iter.MoveNext())
                    {
                        int t0 = trail.Size();
                        v0.UpdateDomain((Domain)iter.Current, trail);
                        Solve(level + 1);
                        trail.Undo(t0);
                    }
                }

                break;
            }
        }