Esempio n. 1
0
    public void PrintStats()
    {
        // MUST BE CALLED AFTER RunEA
        NeatAlgorithmStats stats = contentEA.Statistics;

        Debug.Log("************* NEAT STATS **************");
        Debug.Log("Generation = " + stats._generation + ", Total Evaluation = " + stats._totalEvaluationCount
                  + ", Max Fitness = " + stats._maxFitness + ", MeanFitness = " + stats._meanFitness);

        IGenomeDecoder <NeatGenome, IBlackBox> decoder = experiment.CreateGenomeDecoder();
        IBlackBox          box      = decoder.Decode(contentEA.CurrentChampGenome);
        FastAcyclicNetwork concrete = (FastAcyclicNetwork)box;

        Debug.Log("Champ:Num hidden nodes = " + concrete.hiddenNodeCount + ", num connections = " + concrete.connectionCount);

        // Get highlevel features
        PCGSharpHighLevelFeatures featureCounts = new PCGSharpHighLevelFeatures();

        PCGSharpHighLevelFeatures.C_HL_ROOMTYPE lastRoomType = PCGSharpHighLevelFeatures.C_HL_ROOMTYPE.NoPreviousRoom;
        for (int i = 0; i < (experiment as PCGNeatExperiment).geomNodeList.Count; i++)
        {
            // Get cppn output for each node
            box.InputSignalArray[0] = (experiment as PCGNeatExperiment).geomNodeList[i].normDepth;
            box.InputSignalArray[1] = (experiment as PCGNeatExperiment).geomNodeList[i].normSiblingIndex;

            box.ResetState();
            box.Activate();

            string   outputString = box.OutputSignalArray[0].ToString();
            double[] outputs      = new double[box.OutputCount];
            outputs[0] = box.OutputSignalArray[0];
            for (int j = 1; j < box.OutputCount; j++)
            {
                outputString = outputString + "," + box.OutputSignalArray[j];
                outputs[j]   = box.OutputSignalArray[j];
            }

            Debug.Log("(" + (experiment as PCGNeatExperiment).geomNodeList[i].normDepth + ","
                      + (experiment as PCGNeatExperiment).geomNodeList[i].normSiblingIndex + ") -> (" + outputString + ")");

            // Convert each nodes cppn output into a contentId
            int combinedContentID = featureCounts.CPPNOutputToSettingsId(outputs);
            lastRoomType = featureCounts.UpdateFeatures(combinedContentID, lastRoomType);
        }

        // Pass the highlevel features into the player model to get the fitness
        if ((experiment as PCGNeatExperiment).playerModel != null)
        {
            double[] distributions = (experiment as PCGNeatExperiment).playerModel.ClassifyNewData(featureCounts.ToDoubleArray());
            Debug.Log("Classifier: Champ Distributions: Dislike = " + distributions[0] + ", Like = " + distributions[1]);
            (experiment as PCGNeatExperiment).playerModel.PrintClassifierTestReport();
        }

        Debug.Log("**************END NEAT STATS**************");
    }
    // Use this for initialization
    IEnumerator Start()
    {
        /*** Initialize experiment ***/
        INeatExperiment experiment = new TestExperiment() as INeatExperiment;

        // Null because not reading settings from xmlFile
        experiment.Initialize("this is a test", null);


        /*** Randomly generate population ***/
        // Set initial settings
        // ? means it is nullable
        int?popSize = experiment.DefaultPopulationSize;
        //double? initConnProportion = experiment.NeatGenomeParameters.InitialInterconnectionsProportion;

        // Create a genome factory appropriate for the experiment.
        IGenomeFactory <NeatGenome> genomeFactory = experiment.CreateGenomeFactory();

        // Create an initial population of randomly generated genomes.
        // 0u is a struct for a 32 bit unsigned integer
        List <NeatGenome> genomeList = genomeFactory.CreateGenomeList(popSize.Value, 0u);

        // Check number of species is <= the number of the genomes.
        if (genomeList.Count < experiment.NeatEvolutionAlgorithmParameters.SpecieCount)
        {
            Debug.Log("Genome count must be >= specie count. Genomes=" + genomeList.Count
                      + "  Species=" + experiment.NeatEvolutionAlgorithmParameters.SpecieCount);
            return(false);
        }


        /*** Run the algorithm ***/
        ea = experiment.CreateEvolutionAlgorithm(genomeFactory, genomeList);
        //for (int j = 0; j < 100; j++) {
        yield return(new WaitForSeconds(0.5f));

        //Debug.Log(j);
        ea.StartContinue();
        NeatAlgorithmStats stats = ea.Statistics;

        Debug.Log(stats._generation + ", " + stats._maxFitness + ", " + stats._meanFitness + ", " + stats._totalEvaluationCount + ", " + stats._maxComplexity);
        //}

        //NeatAlgorithmStats stats = ea.Statistics;
        //Debug.Log(stats._generation+", "+stats._maxFitness+", "+stats._meanFitness+", "+stats._totalEvaluationCount+", "+stats._maxComplexity);

        IGenomeDecoder <NeatGenome, IBlackBox> decoder = experiment.CreateGenomeDecoder();
        IBlackBox          box      = decoder.Decode(ea.CurrentChampGenome);
        FastAcyclicNetwork concrete = (FastAcyclicNetwork)box;

        Debug.Log("Num hidden nodes = " + concrete.hiddenNodeCount + ", num connections = " + concrete.connectionCount);

        box.InputSignalArray[0] = 0;
        box.InputSignalArray[1] = 0;

        box.Activate();

        for (int i = 0; i < box.OutputCount; i++)
        {
            Debug.LogWarning("Output[" + i + "] = " + box.OutputSignalArray[i]);
        }
    }