public static void AddPossibleSamples(bool randomAllowed)
    {
        if (freeIndices.Count > 0)         // If there are free slots
        {
            if (readyPhenotypes.Count > 0) // If there are phenotypes to use
            {
                int range = Mathf.Min(freeIndices.Count, readyPhenotypes.Count);

                for (int i = 0; i < range; i++)
                {
                    // Create dragons with the stored phenotypes

                    Vector3    position = populationPositions[freeIndices[i]];
                    GameObject body     = DragonFactory.CreateDragon(readyPhenotypes[i], position, Quaternion.Euler(20, 0, 0), freeIndices[i]);
                    population.Add(body);

                    GlobalSettings.SetBodyAndBonesRendering(body, GlobalSettings.renderingBodyAndBones);
                }

                freeIndices.RemoveRange(0, range);
                readyPhenotypes.RemoveRange(0, range);
            }
            else
            if (randomAllowed)
            {
                int range = freeIndices.Count;

                for (int i = 0; i < range; i++)
                {
                    // Create dragons with the new, random phenotypes
                    Phenotype  phenotype = PhenotypeFactory.CreateRandom();
                    Vector3    position  = populationPositions[freeIndices[i]];
                    GameObject body      = DragonFactory.CreateDragon(phenotype, position, Quaternion.Euler(20, 0, 0), freeIndices[i]);
                    population.Add(body);

                    GlobalSettings.SetBodyAndBonesRendering(body, GlobalSettings.renderingBodyAndBones);
                }
                freeIndices.Clear();
            }
        }
    }
    void Start()
    {
        if (Global.GetCurrentScene() == Global.mainAIScene)
        {
            print("Initialisizing Learning Mode");

            // Create the first population
            PopulationControler.ResizePopulation(GlobalSettings.startPopulationSize);

            LoadRules();


            // Add a random phenotype to the evaluated list for the case that the evry first sample cannot be evaluated successfully.
            float[] em = new float[20];
            for (int i = 0; i < em.Length; i++)
            {
                em[i] = 1;
            }
            PopulationControler.evaluatedPhenotypesGood.Add(new EvaluatedPhenotype(PhenotypeFactory.CreateRandom(), -100, 1, em));
        }
    }
Пример #3
0
    public void ChangeTaskMode(string mode)
    {
        GlobalSettings.taskMode = mode;

        switch (mode)
        {
        case "Learning":
            Debug.Log("Learning Mode");

            // Destroy the full dragon and recreate population
            if (inLearningMode == false)
            {
                Destroy(completeDragon);
                Destroy(bestSample.gameObject);

                completeDragon = null;

                bestSample = null;

                PopulationControler.AddPossibleSamples();

                GlobalSettings.SetBodyAndBonesRendering(renderPhTemp);
                GlobalSettings.SetMembraneRendering(renderMemTemp);

                ResizeFloorsFitToPopulation();
            }

            renderPhTemp  = GlobalSettings.renderingBodyAndBones;
            renderMemTemp = GlobalSettings.renderingMembranes;

            inLearningMode = true;

            if (bestSample == null)
            {
                Global.mainCameraScr.GotoWatchObject(PopulationControler.population[0]);
            }
            else
            {
                Global.mainCameraScr.GotoWatchObject(bestSample);
            }

            Global.mainCameraScr.GotoWatchObjectDistance(35 + Mathf.Sqrt(GlobalSettings.populationSize) * 35);


            testSampleScript = null;
            break;

        case "Testing":
            if (completeDragon != null)
            {
                return;
            }

            Debug.Log("Testing Mode");

            renderPhTemp  = GlobalSettings.renderingBodyAndBones;
            renderMemTemp = GlobalSettings.renderingMembranes;

            // Enable rendering of the new phenotype
            GlobalSettings.SetBodyAndBonesRendering(true);
            GlobalSettings.SetMembraneRendering(true);
            //

            // Save the current population for future use
            PopulationControler.SaveAndDestroyPopulation();


            // Copy the phenotype of the best dragon
            Phenotype bestPhenotype;

            if (bestSample == null)
            {
                bestPhenotype = PopulationControler.GetBestPhenotype();
            }
            else
            {
                bestPhenotype = bestSample.GetComponent <DragonScript>().wingSet.phenotype;
            }

            if (bestPhenotype == null)
            {
                bestPhenotype = PhenotypeFactory.CreateRandom();
            }

            bestPhenotype.SetDependantBonethickness(0.6f, 0.1f);                                       // Sets bone thicknes depending on the distance from the origin
            bestPhenotype.SetDependantBonecolor(new Color(0.3f, 1, 0.3f), new Color(0.25f, 0.25f, 1)); // Sets bone color depending on the distance from the origin

            bestSample = DragonFactory.CreateDragon(bestPhenotype, GlobalSettings.originPoint, Quaternion.Euler(20, 0, 0), 0);
            //

            // Calculate optimal gravity for floating
            float curMax;
            if (Global.GUIControlerScr.sampleToObserve != null)
            {
                curMax = Mathf.Max(bestSampleHeight, Global.GUIControlerScr.sampleVisScriptToObserve.localBestHeight);
            }
            else
            {
                curMax = bestSampleHeight;
            }

            customGravityOnBestSample = new Vector3(0, -(curMax / (bestPhenotype.maxFlapSteps * (GlobalSettings.requiredEvaluationFlaps + 0.5f))) * 75, 0);
            //


            ResizeFloors(1);
            inLearningMode = false;

            // Make everything visible
            GlobalSettings.SetBodyAndBonesRendering(bestSample, true);

            // Make body invisible (leaves wings visible)
            bestSample.gameObject.GetComponent <MeshRenderer>().enabled = false;

            // Set interpolation
            GlobalSettings.SetPhysInterpolation(bestSample, true);

            // Reenable membrane
            testSampleScript = bestSample.GetComponent <DragonScript>();
            testSampleScript.wingSet.RenderVisualisation(true);


            Global.mainCameraScr.GotoWatchObject(bestSample);
            floorOfBest.transform.position = bestSample.transform.position;
            Global.GUIControlerScr.SwitchObjectToObserve(bestSample);
            justMovedToWatchNew = true;
            Global.mainCameraScr.GotoWatchObjectDistance(70);


            // Create the new surrounding full dragon model

            completeDragon = (GameObject)Instantiate(CompleteDragonModel, bestSample.transform.position, bestSample.transform.rotation);
            completeDragon.GetComponent <DragonControler>().followerObject = bestSample;


            break;
        }
    }
Пример #4
0
    void Start()
    {
        if (Global.GetCurrentScene() == Global.mainAIScene)
        {
            renderPhTemp  = GlobalSettings.renderingBodyAndBones;
            renderMemTemp = GlobalSettings.renderingMembranes;

            ChangeTaskMode("Learning");
        }
        else
        if (Global.GetCurrentScene() == Global.cliffScene)
        {
            print("Initialisizing Cliff Scene Mode");

            PopulationControler.Init();

            // Generate a standard-phenotype for a demonstrationd ragon
            Phenotype mainDragonType = PhenotypeFactory.CreateForDemoDragon();


            // Activate some graphical effects (not used during the phenotypes for the genetic algorithm)
            mainDragonType.SetDependantBonethickness(0.75f, 0.2f);                                          // Sets bone thicknes depending on the distance from the origin
            mainDragonType.SetDependantBonecolor(new Color(0.3f, 0.6f, 0.2f), new Color(0.5f, 0.8f, 0.4f)); // Sets bone color depending on the distance from the origin


            GameObject theDragon = GameObject.Find("TheDragon");
            GameObject dragonBody = null, dragonHead = null;


            foreach (Transform child in theDragon.transform)
            {
                if (child.gameObject.name == "MainBody")
                {
                    dragonBody = child.gameObject;
                }

                if (child.gameObject.name == "Head")
                {
                    dragonHead = child.gameObject;
                }
            }


            PopulationControler.population.Add(dragonBody);

            DragonFactory.AttachWing(dragonBody, mainDragonType);


            //Disable the wing movement
            foreach (HingeJoint joint in dragonBody.GetComponent <DragonScript>().wingSet.joints)
            {
                joint.useMotor = false;
            }

            dragonBody.GetComponent <DragonScript>().wingSet.running = false;
            dragonBody.GetComponent <DragonScript>().index           = 0;
            theDragon.GetComponent <DragonControler>().PrepareComponents();
            theDragon.GetComponent <DragonControler>().SwitchGravity(true);



            // Set rendering settings
            GlobalSettings.SetBodyAndBonesRendering(true);
            GlobalSettings.SetMembraneRendering(true);

            // Make the camera keep looking at the head
            Global.mainCameraScr.SetCameraFocusObject(dragonHead);

            bestSample     = dragonBody;
            inLearningMode = false;
        }
    }