//Similarly generate abled boats
    private void GenerateAbledBoats(BoatLogic[] abledBoatParents)
    {
        _activeAbledBoats = new List <BoatLogic>();
        List <GameObject> objects = abledBoatGenerator.RegenerateObjects();

        foreach (GameObject obj in objects)
        {
            BoatLogic boat = obj.GetComponent <BoatLogic>();
            if (boat != null)
            {
                _activeAbledBoats.Add(boat);
                if (abledBoatParents != null)
                {
                    BoatLogic boatParent = abledBoatParents[Random.Range(0, abledBoatParents.Length)];
                    boat.Birth(boatParent.GetData());
                }

                boat.Mutate(mutationFactor, mutationChance);
                boat.AwakeUp();
            }
        }
    }
Example #2
0
    /// <summary>
    /// Generates the list of boats using the parents list. The parent list can be null and, if so, it will be ignored.
    /// Newly created boats will go under mutation (MutationChances and MutationFactor will be applied).
    /// /// Newly create agents will be Awaken (calling AwakeUp()).
    /// </summary>
    /// <param name="boatParents"></param>
    public void GenerateBoats(BoatLogic[] boatParents = null)
    {
        _activeBoats = new List <BoatLogic>();
        List <GameObject> objects = boatGenerator.RegenerateObjects();

        if (boatParents != null && crossover && (boatParents.Length % 2 != 0 || objects.Count % 2 != 0))
        {
            crossover = false;
            Debug.Log("Crossover has been automaticaly disabled. It requires an even number of parents and an even number of children to generate.");
        }

        BoatLogic prevBoat = null; //used for crossover

        foreach (GameObject obj in objects)
        {
            BoatLogic boat = obj.GetComponent <BoatLogic>();
            if (boat != null)
            {
                _activeBoats.Add(boat);
                if (boatParents != null)
                {
                    if (!crossover)
                    {
                        BoatLogic boatParent = boatParents[Random.Range(0, boatParents.Length)];
                        boat.Birth(boatParent.GetData());
                        boat.Mutate(mutationFactor, mutationChance);
                        boat.AwakeUp();
                    }
                    else //wait to have two children then perform crossover
                    {
                        if (prevBoat == null)
                        {
                            prevBoat = boat;
                        }
                        else
                        {
                            BoatLogic boatParent      = boatParents[Random.Range(0, boatParents.Length)];
                            BoatLogic otherBoatParent = boatParents[Random.Range(0, boatParents.Length)];
                            // a parent can happen to match himself, then the offspring will be just two copies of himself
                            // this helps limiting how much the parents can mix

                            boat.NPointCrossoverBirth(boatParent.GetData(), otherBoatParent.GetData(), prevBoat, groupedGenes);

                            boat.Mutate(mutationFactor, mutationChance);
                            boat.AwakeUp();

                            prevBoat.Mutate(mutationFactor, mutationChance);
                            prevBoat.AwakeUp();

                            prevBoat = null;
                        }
                    }
                }
                else
                {
                    boat.Mutate(mutationFactor, mutationChance);
                    boat.AwakeUp();
                }
            }
        }
    }