예제 #1
0
        // ReSharper disable once UnusedMember.Global
        public bool TrySolve()
        {
            var count = 0;

            void CountSolutions(object sender, ChangeMakerSolutionEventArgs e) => ++ count;

            Solved += CountSolutions;

            using (var solver = new Solver(SolverName))
            {
                var compositionVars = DeclareVariables(solver);

                // ReSharper disable once IdentifierTypo
                decimal GetActualDenomCompositeValue(Denomination d)
                => compositionVars[(int)d, 1].Value();

                // ReSharper disable once IdentifierTypo
                Tuple <Denomination, decimal> GetActualDenomComposite(Denomination d)
                => Create(d, GetActualDenomCompositeValue(d));

                DefineConstraints(solver, compositionVars).ToList().ForEach(solver.Add);

                using (var sc = solver.MakeAllSolutionCollector())
                {
                    using (var db = solver.MakePhase(compositionVars.Flatten().ToArray()
                                                     , INT_VAR_DEFAULT, INT_VALUE_DEFAULT))
                    {
                        StartedOn = UtcNow;
                        solver.NewSearch(db, sc);

                        while (solver.NextSolution())
                        {
                            var solution = new ChangeMaker(
                                GetActualDenomComposite(TwoPounds)
                                , GetActualDenomComposite(OnePence)
                                , GetActualDenomComposite(TwoPence)
                                , GetActualDenomComposite(FivePence)
                                , GetActualDenomComposite(TenPence)
                                , GetActualDenomComposite(TwentyPence)
                                , GetActualDenomComposite(FiftyPence)
                                , GetActualDenomComposite(OnePound)
                                );
                            OnSolved(solution);
                        }

                        solver.EndSearch();
                        FinishedOn = UtcNow;
                    }
                }
            }

            Solved -= CountSolutions;
            return(count > 0);
        }
예제 #2
0
 private void OnSolved(ChangeMaker solution)
 => Solved?.Invoke(this, new ChangeMakerSolutionEventArgs(solution));
 internal ChangeMakerSolutionEventArgs(ChangeMaker solution)
 {
     Solution = solution;
 }