Exemplo n.º 1
0
        public override void BuildProgram(ProgramSettings settings, Action <string, CandidateSolution <bool, ProblemState> > callback)
        {
            this.settings = settings;
            this.perGenerationCallback = callback;

            //for (int upcardRank = 14; upcardRank > 1; upcardRank--)
            foreach (var upcardRank in Card.ListOfRanks)
            {
                // skip K, Q, J
                if (upcardRank == Card.Ranks.King || upcardRank == Card.Ranks.Queen || upcardRank == Card.Ranks.Jack)
                {
                    continue;
                }

                Card dealerCard = new Card(upcardRank, Card.Suits.Diamonds);

                solutions[dealerCard.Rank] = FindStrategyForUpcard(dealerCard);
                SaveSolutionToDisk(dealerCard.Rank + "_upcard_solution.txt", solutions[dealerCard.Rank].ToString());
            }

            // convert the dictionary of GP solutions to a single strategy
            FinalStrategy = StrategyFactory.GetStrategyForGP(solutions);
            FinalStatus   = "Generations needed to find: " + NumGenerationsNeeded;
        }
        public override void BuildProgram(ProgramSettings settings, Action <string> currentStatusCallback)
        {
            this.settings             = settings;
            displayGenerationCallback = currentStatusCallback;

            // create the engine.  each tree (and node within the tree) will return a bool.
            // we also indicate the type of our problem state data (used by terminal functions and stateful functions)
            var engine = new Engine <bool, ProblemState>(settings.GPsettings);

            // no constants for this problem

            // no variables for this solution - we can't pass in information about our hand
            // via boolean variables, so we do it via some terminal functions instead

            // for a boolean tree, we use the standard operators
            engine.AddFunction((a, b) => a || b, "Or");
            engine.AddFunction((a, b, c) => a || b || c, "Or3");
            engine.AddFunction((a, b) => a && b, "And");
            engine.AddFunction((a, b, c) => a && b && c, "And3");
            engine.AddFunction((a) => !a, "Not");

            // then add functions to indicate a strategy
            engine.AddStatefulFunction(HitIf, "HitIf");
            engine.AddStatefulFunction(StandIf, "StandIf");
            engine.AddStatefulFunction(DoubleIf, "DoubleIf");
            engine.AddStatefulFunction(SplitIf, "SplitIf");

            //----------------------------------------------
            // terminal functions to look at game state
            //----------------------------------------------

            // soft hands
            engine.AddTerminalFunction(AcePlus2, "AcePlus2");
            engine.AddTerminalFunction(AcePlus3, "AcePlus3");
            engine.AddTerminalFunction(AcePlus4, "AcePlus4");
            engine.AddTerminalFunction(AcePlus5, "AcePlus5");
            engine.AddTerminalFunction(AcePlus6, "AcePlus6");
            engine.AddTerminalFunction(AcePlus7, "AcePlus7");
            engine.AddTerminalFunction(AcePlus8, "AcePlus8");
            engine.AddTerminalFunction(AcePlus9, "AcePlus9");

            // pairs
            engine.AddTerminalFunction(HasPairTwos, "HasPair2");
            engine.AddTerminalFunction(HasPairThrees, "HasPair3");
            engine.AddTerminalFunction(HasPairFours, "HasPair4");
            engine.AddTerminalFunction(HasPairFives, "HasPair5");
            engine.AddTerminalFunction(HasPairSixes, "HasPair6");
            engine.AddTerminalFunction(HasPairSevens, "HasPair7");
            engine.AddTerminalFunction(HasPairEights, "HasPair8");
            engine.AddTerminalFunction(HasPairNines, "HasPair9");
            engine.AddTerminalFunction(HasPairTens, "HasPairT");
            engine.AddTerminalFunction(HasPairAces, "HasPairA");

            // hard hand totals
            engine.AddTerminalFunction(HandVal5, "Hard5");
            engine.AddTerminalFunction(HandVal6, "Hard6");
            engine.AddTerminalFunction(HandVal7, "Hard7");
            engine.AddTerminalFunction(HandVal8, "Hard8");
            engine.AddTerminalFunction(HandVal9, "Hard9");
            engine.AddTerminalFunction(HandVal10, "Hard10");
            engine.AddTerminalFunction(HandVal11, "Hard11");
            engine.AddTerminalFunction(HandVal12, "Hard12");
            engine.AddTerminalFunction(HandVal13, "Hard13");
            engine.AddTerminalFunction(HandVal14, "Hard14");
            engine.AddTerminalFunction(HandVal15, "Hard15");
            engine.AddTerminalFunction(HandVal16, "Hard16");
            engine.AddTerminalFunction(HandVal17, "Hard17");
            engine.AddTerminalFunction(HandVal18, "Hard18");
            engine.AddTerminalFunction(HandVal19, "Hard19");
            engine.AddTerminalFunction(HandVal20, "Hard20");

            // upcards
            engine.AddTerminalFunction(DealerShows2, "Dlr2");
            engine.AddTerminalFunction(DealerShows3, "Dlr3");
            engine.AddTerminalFunction(DealerShows4, "Dlr4");
            engine.AddTerminalFunction(DealerShows5, "Dlr5");
            engine.AddTerminalFunction(DealerShows6, "Dlr6");
            engine.AddTerminalFunction(DealerShows7, "Dlr7");
            engine.AddTerminalFunction(DealerShows8, "Dlr8");
            engine.AddTerminalFunction(DealerShows9, "Dlr9");
            engine.AddTerminalFunction(DealerShows10, "Dlr10");
            engine.AddTerminalFunction(DealerShowsA, "DlrA");

            // pass a fitness evaluation function and run
            engine.AddFitnessFunction((t) => EvaluateCandidate(t));

            // and add something so we can track the progress
            engine.AddProgressFunction((t) => PerGenerationCallback(t));

            Solution    = engine.FindBestSolution();
            Fitness     = Solution.Fitness;
            FinalStatus = "Final Score: " + Fitness.ToString() + "\n" +
                          "Generations needed to find: " + NumGenerationsNeeded;
        }
Exemplo n.º 3
0
 public abstract void BuildProgram(ProgramSettings settings, Action <string, CandidateSolution <bool, ProblemState> > currentStatusCallback);