Example #1
0
        static void Main(string[] args)
        {
            PMGGenomeSet testset = new PMGGenomeSet();

            testset.TestPMGGenomeSet();
            PMGGenomeParse Parsedtestset = new PMGGenomeParse();

            Parsedtestset.DecodeGenomeSet(testset);
            PMGSingleGameInstance testGameInstance = new PMGSingleGameInstance();

            //decodes the entire set
            //testGameInstance.ParsedSet.DecodeGenomeSet(testset);
            testGameInstance.SetInternalParsedSet(Parsedtestset);
            testGameInstance.BuildInstance(true);



            /*/test find one actor's event's method's value function
             * if (debug)
             * {
             *  Console.WriteLine("Total number of created actor : " + CreatedActors.Count);
             *  Console.WriteLine("first actor's first event's first method's first executelist's first step: " + CreatedActors[0].Events[0]._method._steps[0]._functions[0].Type);
             *
             * }
             *
             * /*///jsut for checking that genomes got correctly decoded
            //testGameInstance.ParsedSet.DisplayActorTypeList();
            //testGameInstance.ParsedSet.DisplayActorTypePossplit3List();

            //genome error report
            //testGameInstance.ParsedSet.GenomeSetErrorReport();


            bool programRunning = true;
            bool programPaused  = false;


            //pretend update function
            for (int timestep = 0; timestep < 2; timestep++)
            {
                Console.WriteLine("-- T = {0} --", timestep);
                testGameInstance.UpdateActors();
            }
            // Console.WriteLine("X: "+e.position[0]+"Y: "+e.position[1]);

            /*/pretedn update function
             * while (programRunning)
             * {
             *
             *  if (Keyboard.IsKeyDown(Key.Q))
             *  {
             *      programRunning = false;
             *  }
             *  if (Keyboard.IsKeyDown(Key.P))
             *  {
             *      programPaused = true;
             *  }
             *  if (!programPaused)
             *  {
             *      //Console.WriteLine("-- T = {0} --", i);
             *      testGameInstance.UpdateActors();
             *
             *
             *      i++;
             *  }
             *
             *
             *
             * }//*/
            Console.ReadKey();
        }
            private double ExtrinsicFitness(PMGSingleGameInstance GInstance)
            {
                // Extrinsic fitnesses //
                double efit = 0.0;

                // Run with various players and get extrinsic fitness


                bool GameTimedOut   = false;
                bool TrialsTimedOut = false;

                PlayerType PType = PlayerType.Passive;

                // Exceptions
                List <double> VisitedTilesRatio = new List <double> ();

                List <int> PoppingEmptyStack             = new List <int> ();
                List <int> ReadEmptyStack                = new List <int> ();
                List <int> ReadOutsideStack              = new List <int> ();
                List <int> PushNullToStack               = new List <int> ();
                List <int> ExecuteListOwnerNull          = new List <int> ();
                List <int> CastingOfFunctionFailed       = new List <int> ();
                List <int> ExecutingFunctionsOutsideList = new List <int> ();
                List <int> StackIsNull          = new List <int> ();
                List <int> NoStepsInMethod      = new List <int> ();
                List <int> CastingOfOwnerFailed = new List <int> ();
                List <int> UndefinedError       = new List <int> ();


                //ExceptionWeights
                ExtrinsicExceptionWeights.Add(1);                       //PoppingEmptyStack
                ExtrinsicExceptionWeights.Add(1);                       //ReadEmptyStack
                ExtrinsicExceptionWeights.Add(1);                       //ReadOutsideStack
                ExtrinsicExceptionWeights.Add(1);                       //PushNullToStack
                ExtrinsicExceptionWeights.Add(1);                       //ExecuteListOwnerNull
                ExtrinsicExceptionWeights.Add(1);                       //CastingOfFunctionFailed
                ExtrinsicExceptionWeights.Add(1);                       //ExecutingFunctionsOutsideList
                ExtrinsicExceptionWeights.Add(1);                       //StackIsNull
                ExtrinsicExceptionWeights.Add(2);                       //NoStepsInMethod
                ExtrinsicExceptionWeights.Add(1);                       //CastingOfOwnerFailed
                ExtrinsicExceptionWeights.Add(1);                       //UndefinedError


                timer.Start();

                for (int trials = 0; trials < NumTrialGames; trials++)
                {
                    PoppingEmptyStack.Add(0);
                    ReadEmptyStack.Add(0);
                    ReadOutsideStack.Add(0);
                    PushNullToStack.Add(0);
                    ExecuteListOwnerNull.Add(0);
                    CastingOfOwnerFailed.Add(0);
                    ExecutingFunctionsOutsideList.Add(0);
                    StackIsNull.Add(0);
                    CastingOfFunctionFailed.Add(0);
                    PushNullToStack.Add(0);
                    NoStepsInMethod.Add(0);
                    UndefinedError.Add(0);
                    List <List <int> > VisitedTiles = new List <List <int> >();

                    for (int timestep = 0; timestep < InGameRunSteps; timestep++)
                    {
                        // Get some diagnostics?
                        if (timer.Elapsed.Seconds > extrinsicMaxRunPerGame)
                        {
                            // timeout
                            GameTimedOut = true;
                        }

                        // Try to do a step. Catch any exceptions and keep running
                        try
                        {
                            GInstance.UpdateActors();
                        }
                        catch (Exception e) {
                            // Yes this is ugly and messy and hacky
                            switch (e.Message)
                            {
                            case "Popping empty stack":
                                PoppingEmptyStack[trials]++;
                                break;

                            case "Reading from empty stack":
                                ReadEmptyStack[trials]++;
                                break;

                            case "Reading from outside stack":
                                ReadOutsideStack[trials]++;
                                break;

                            case "Pushing null to stack":
                                PushNullToStack[trials]++;
                                break;

                            case "ExecuteList owner is null":
                                ExecuteListOwnerNull[trials]++;
                                break;

                            case "Casting of owner as PMGEvent failed.":
                                CastingOfOwnerFailed[trials]++;
                                break;

                            case "Casting of owner as PMGMethod failed.":
                                CastingOfOwnerFailed[trials]++;
                                break;

                            case "Tried to execute functions outside of list.":
                                ExecutingFunctionsOutsideList[trials]++;
                                break;

                            case "localStack is null":
                                StackIsNull[trials]++;
                                break;

                            case "Casting of function as PMGValueFunction failed.":
                                CastingOfFunctionFailed[trials]++;
                                break;

                            case "Casting of function as PMGUtilityFunction failed.":
                                CastingOfFunctionFailed[trials]++;
                                break;

                            case "Casting of function as PMGConditionFunction failed.":
                                CastingOfFunctionFailed[trials]++;
                                break;

                            case "Casting of function as PMGChangeFunction failed.":
                                CastingOfFunctionFailed[trials]++;
                                break;

                            case "Pushing null to valuestack":
                                PushNullToStack[trials]++;
                                break;

                            case "Method has no steps.":
                                NoStepsInMethod[trials]++;
                                break;

                            default:
                                UndefinedError [trials]++;
//								Console.WriteLine ("Some other exception from inside the game:");
//								Console.WriteLine (e.Message);
//								Console.WriteLine ("Full Genome:");
//								PMGExporter exporter = new PMGExporter ();
//								exporter.ExportSetToFile (GInstance.GameSet._genomeSet, "./genome_for_no_method_functions");
//								GInstance.GameSet._genomeSet.ExportSerializedGenomeSet ().ForEach (t => Console.WriteLine (t));
//								throw new Exception (e.Message);
                                break;
                            }
                        }
                        // Loop through all the actors
                        foreach (PMGActor actor in GInstance.SpawnedActors)
                        {
                            // Check if any new tiles have been visited
                            bool TileHasBeenCounted = false;
                            foreach (List <int> visitedTile in VisitedTiles)
                            {
                                if (Enumerable.SequenceEqual(visitedTile, actor.position))
                                {
                                    TileHasBeenCounted = true;
                                }
                            }
                            // Add them if they have not yet been visited
                            if (!TileHasBeenCounted)
                            {
                                VisitedTiles.Add(new List <int> (actor.position.ToArray()));
                            }
                        }
                        // if game ended - how many timesteps did it take?
                    }

                    // Add number of visited tiles to list
                    VisitedTilesRatio.Add(Convert.ToDouble(VisitedTiles.Count) / Convert.ToDouble(TotalCells));

                    if (timer.Elapsed.Seconds > extrinsicMaxTimeForTrials)
                    {
                        TrialsTimedOut = true;
                    }
                }

                // Completion

                // Timeout

                // Resilience


                //Board Coverage
                double BoardCoverage = Convert.ToDouble(VisitedTilesRatio.Sum()) / Convert.ToDouble(NumTrialGames);

                double Exceptions = LogFunction(PoppingEmptyStack.Average()) * realWeight(ExtrinsicExceptionWeights [0], ExtrinsicExceptionWeights)
                                    + LogFunction(ReadEmptyStack.Average()) * realWeight(ExtrinsicExceptionWeights [0], ExtrinsicExceptionWeights)
                                    + LogFunction(ReadOutsideStack.Average()) * realWeight(ExtrinsicExceptionWeights [1], ExtrinsicExceptionWeights)
                                    + LogFunction(PushNullToStack.Average()) * realWeight(ExtrinsicExceptionWeights [2], ExtrinsicExceptionWeights)
                                    + LogFunction(ExecuteListOwnerNull.Average()) * realWeight(ExtrinsicExceptionWeights [3], ExtrinsicExceptionWeights)
                                    + LogFunction(CastingOfOwnerFailed.Average()) * realWeight(ExtrinsicExceptionWeights [4], ExtrinsicExceptionWeights)
                                    + LogFunction(ExecutingFunctionsOutsideList.Average()) * realWeight(ExtrinsicExceptionWeights [5], ExtrinsicExceptionWeights)
                                    + LogFunction(StackIsNull.Average()) * realWeight(ExtrinsicExceptionWeights [6], ExtrinsicExceptionWeights)
                                    + LogFunction(CastingOfFunctionFailed.Average()) * realWeight(ExtrinsicExceptionWeights [7], ExtrinsicExceptionWeights)
                                    + LogFunction(PushNullToStack.Average()) * realWeight(ExtrinsicExceptionWeights [8], ExtrinsicExceptionWeights)
                                    + LogFunction(NoStepsInMethod.Average()) * realWeight(ExtrinsicExceptionWeights [9], ExtrinsicExceptionWeights)
                                    + LogFunction(UndefinedError.Average()) * realWeight(ExtrinsicExceptionWeights [10], ExtrinsicExceptionWeights);


                efit = BoardCoverage * realWeight(ExtrinsicWeights [(int)EW.BoardCoverage], ExtrinsicWeights)
                       + Exceptions * realWeight(ExtrinsicWeights[(int)EW.Exceptions], ExtrinsicWeights);

                return(efit);
            }