Exemplo n.º 1
0
    bluePrint[] mergeSort(bluePrint[] a)
    {
        if (a.Length <= 1) {
                        return a;

                }

                int middle = a.Length / 2;
                bluePrint[] left = new bluePrint[middle];
                bluePrint[] right = new bluePrint[a.Length - middle];

                for (int i = 0, j = 0; i < a.Length; i ++) {
                        if (i < middle) {
                                left [i] = a [i];
                        } else {
                                right [j] = a [i];
                                j++;
                        }

                }
                left = mergeSort (left);
                right = mergeSort (right);

                return merge (left, right);
    }
Exemplo n.º 2
0
    void generateInitialPopulation()
    {
        //			int initialStrutID = 1000;

                // Use this for initialization

                // int identityStrut = initialStrutID;
                //			int currentStrut;

                for (int i = 0; i < populationSize; i ++) {
                        bluePrint robot;
                        string[] chromosomes = new string[numberOfUnits - 1];
                        string ID = "B" + i;
                        //sr.WriteLine (ID);
        //						string[] IDList = new string[numberOfUnits];
                        //		int originID = 5000;
                        robot = new bluePrint (ID);
                        int originUnitType = UnityEngine.Random.Range (0, maxValueOfUnit + 1);

                        functionalUnit origin = new functionalUnit ("5000", new point3 (0, 0, 0));
                        robot.unitArray.Add (origin);
                        for (int j = 0; j < numberOfUnits-1; j ++) {

                                int id = 5001 + j;

                                int side = UnityEngine.Random.Range (0, 6);
                                int connectObject = 5000 + UnityEngine.Random.Range (0, j + 1);
                                functionalUnit con = robot.getUnit ("" + connectObject);
                                point3 newPoint = translatePoint (con.location, side);
                                while (robot.unitAreaOccupied(newPoint)) {
                                        side = UnityEngine.Random.Range (0, 6);
                                        connectObject = 5000 + UnityEngine.Random.Range (0, j + 1);
                                        con = robot.getUnit ("" + connectObject);
                                        newPoint = translatePoint (con.location, side);

                                }
                                robot.unitArray.Add (new functionalUnit ("" + id, newPoint));

                                int unitType = UnityEngine.Random.Range (0, maxValueOfUnit + 1);
                                string chromosome = connectObject + "," + id + "," + originUnitType + "," + unitType + "," + side;
                                chromosomes [j] = chromosome;

                        }
                        robot.chromosomes = chromosomes;
                        currentPopulation [i] = robot;

                }
    }
Exemplo n.º 3
0
    bluePrint[] merge(bluePrint[] left, bluePrint[] right)
    {
        bluePrint [] result = new bluePrint[left.Length + right.Length];
                int leftPointer = 0, rightPointer = 0, returnPointer = 0;

                while (leftPointer < left.Length || rightPointer < right.Length) {
                        if (leftPointer < left.Length && rightPointer < right.Length) {
                                if (left [leftPointer].score <= right [rightPointer].score) {
                                        result [returnPointer] = left [leftPointer];
                                        returnPointer ++;
                                        leftPointer++;
                                } else {
                                        result [returnPointer] = right [rightPointer];
                                        returnPointer++;
                                        rightPointer++;
                                }

                        } else if (leftPointer < left.Length) {
                                result [returnPointer] = left [leftPointer];
                                returnPointer ++;
                                leftPointer++;

                        } else if (rightPointer < right.Length) {
                                result [returnPointer] = right [rightPointer];
                                returnPointer++;
                                rightPointer++;

                        }

                }

                return result;
    }
Exemplo n.º 4
0
    bluePrint[] evalPopulation(bluePrint[] pop)
    {
        //Debug.Log ("eval call");
                for (int i = 0; i < pop.Length; i ++) {
                        pop [i].score = -(pop [i].endLocation - pop [i].startLocation);
                        //Debug.Log(pop[i].score);
                }

                return pop;
    }
Exemplo n.º 5
0
    bluePrint[] crossOver(bluePrint[] leaders)
    {
        //Debug.Log ("crossover call");
                ArrayList combinations = new ArrayList ();
                bluePrint[] children = new bluePrint[populationSize];
                int i = 0;
                int topleaders = (int) (populationSize * 0.01);
                float leaderVal = leaders [populationSize - 1].score;
                bluePrint parent1 = null, parent2 = null;
                bool parent1Selected = false, parent2Selected = false;

        for (int leads = populationSize-1; leads > populationSize - topleaders-1; leads--) {
            children[i] = leaders[leads];
            i++;
                }
                //int count = 0;

                while (i < populationSize) {
                        while (!parent1Selected || !parent2Selected) {
                                float selectionChance = UnityEngine.Random.Range (0.0f, 1.0f);
                                int selectedBot = UnityEngine.Random.Range (0, populationSize);

                                //	float normalVal = leaders [selectedBot].score / leaderVal;
                                //count++;
                                if (leaders [selectedBot].normalScore > selectionChance) {

        //										if(parent1Selected){
        //
        //						foreach(string a in combinations){
        //
        //							if(a.Equals(parent1.ID + leaders[selectedBot])){
        //
        //								duplicate = true;
        //							}
        //
        //
        //
        //										}
                                        //}

                                        if (!parent1Selected) {
                                                parent1 = leaders [selectedBot];
                                                //leaders[selectedBot].score = -100;
                                                parent1Selected = true;

                                        } else if (!combinations.Contains (parent1.ID + leaders [selectedBot].ID) && !parent1.ID.Equals (leaders [selectedBot].ID)) {
                                                parent2 = leaders [selectedBot];
                                                //leaders[selectedBot].score = -100;
                                                parent2Selected = true;
                                                //	Debug.Log(parent1.ID + parent2.ID);
                                                //	Debug.Log(parent2.ID + parent1.ID);
                                                combinations.Add (parent1.ID + parent2.ID);
                                                combinations.Add (parent2.ID + parent1.ID);

                                                //Debug.Log("p1 " + parent1.chromosomes[0]);
                                                //Debug.Log("p2 " + parent2.chromosomes[0]);
                                                //	Debug.Log("it took me: " + count + " tries to find the parents");
                                                //	count = 0;
                                                bluePrint[] newbies = breed (parent1, parent2);
                                                //Debug.Log(newbies.Length);

                                                for (int j = 0; j < newbies.Length && i < populationSize; j ++) {
                                                        //	Debug.Log("newbiechrom: " +newbies[j].chromosomes[0]);
                                                        children [i] = newbies [j];
                                                        children [i].ID = "B" + i + "G" + currentGeneration;
                                                        i ++;

                                                }

                                        } else {
                                                //Debug.Log ("parent combination already exists or same parents were selected");
                                        }

                                }

                        }
                        parent1Selected = false;
                        parent2Selected = false;

                }

                //	Debug.Log ("howmanychildren:" + i);
                return children;
    }
Exemplo n.º 6
0
    bluePrint[] breed(bluePrint a, bluePrint b)
    {
        //	Debug.Log ("breed call");

                bluePrint[] children = new bluePrint[2];
                String [] aChroms = a.chromosomes;
                String [] bChroms = b.chromosomes;
                int chromA = UnityEngine.Random.Range (0, numberOfUnits);
                int chromB = UnityEngine.Random.Range (0, numberOfUnits);

                String[] newChromsA = swapChrom (aChroms, bChroms, chromA, chromB);
                //Debug.Log ("swappin B:");

                String[] newChromsB = swapChrom (bChroms, aChroms, chromB, chromA);

                children [0] = new bluePrint ("blank", newChromsA);

                children [1] = new bluePrint ("blank", newChromsB);

                //children [0] = new bluePrint ("blank", a.chromosomes);

                //children [1] = new bluePrint ("blank", b.chromosomes);

        //		}
                return children;
    }
Exemplo n.º 7
0
    public GameObject generateRobot(bluePrint currentBP, Vector3 spawnLocation)
    {
        string line;
                GameObject previousInstance = null;
                GameObject instance1 = null;
                GameObject instance2 = null;
                int[] instructions = new int[4];
                String BUID;
                GameObject parent = null;
                //Debug.Log ("crhoms and sl" + currentBP.chromosomes [0]);
                BUID = currentBP.ID;

                FUArray = new List<GameObject> ();
                previousInstance = null;
                parent = (GameObject)Instantiate (Resources.Load ("parentObject"));
                parent.tag = "Empty";
                parent.transform.name = BUID;

                for (int chromosome = 0; chromosome < currentBP.chromosomes.Length; chromosome++) {

                        line = currentBP.chromosomes [chromosome];
                        if (line != null) {

                                instructions = stringToIntArray (line);

                                if (previousInstance == null) {
                                        instance1 = (GameObject)Instantiate (Resources.Load ("FunctionalUnits/F" + instructions [2]));
                                        Transform[] children = new Transform[instance1.transform.childCount];

                                        instance1.transform.position = spawnLocation;
                                        FUArray.Add (instance1);
                                        parent.transform.position = spawnLocation;
                                        instance1.transform.parent = parent.transform;
                                        instance1.transform.name = instructions [0].ToString ();
                                        int index = 0;
                                        foreach (Transform child in instance1.transform) {
                                                children [index++] = child;
                                                //Debug.Log(child);
                                        }
                                        currentBP.trackerObject = children [0];
                                }

                                if (findUnit (instructions [1].ToString ()) != null) {
                                        instance2 = findUnit (instructions [1].ToString ());

                                        Debug.Log ("Unit already exists: " + instructions [1].ToString ());
                                        //affixStruts (instance1, instance2, instructions [4]);
                                } else {
                                        instance1 = findUnit (instructions [0].ToString ());
                                        instance2 = (GameObject)Instantiate (Resources.Load ("FunctionalUnits/F" + instructions [3]));
                                        instance2.transform.position = spawnLocation;
                                        instance2.transform.name = instructions [1].ToString ();

                                        FUArray.Add (instance2);

                                        placeNewUnit (instance1, instance2, instructions [4]);

                                        affixStruts (returnClosest (instance1.transform.root.gameObject, instance2) [0].gameObject, returnClosest (instance1.transform.root.gameObject, instance2) [1].gameObject);
                                        instance2.transform.parent = parent.transform;

                                }
                                previousInstance = instance1;

                        }
                }

                //currentBP.instantiation = parent;

                return parent;
    }
Exemplo n.º 8
0
    public bool generateRobots(bluePrint[] population)
    {
        string line;
                GameObject previousInstance = null;
                GameObject instance1 = null;
                GameObject instance2 = null;
                int[] instructions = new int[4];
                String BUID;
                GameObject parent = null;

                for (int bluePrint = 0; bluePrint < population.Length; bluePrint ++) {
                        bluePrint currentBP = population [bluePrint];

                        BUID = currentBP.ID;
                        spawnLocation = spawnLocation + new Vector3 (10, 0, 0);
                        FUArray = new List<GameObject> ();
                        previousInstance = null;
                        parent = (GameObject)Instantiate (Resources.Load ("parentObject"));
                        parent.transform.name = BUID;

                        for (int chromosome = 0; chromosome < currentBP.chromosomes.Length; chromosome++) {

                                line = currentBP.chromosomes [chromosome];
                                if (line != null) {

                                        instructions = stringToIntArray (line);

                                        if (previousInstance == null) {
                                                instance1 = (GameObject)Instantiate (Resources.Load ("FunctionalUnits/F" + instructions [2]));
                                                instance1.transform.position = spawnLocation;
                                                FUArray.Add (instance1);
                                                parent.transform.position = instance1.transform.position;
                                                instance1.transform.parent = parent.transform;
                                                instance1.transform.name = instructions [0].ToString ();
                                        }

                                        if (findUnit (instructions [1].ToString ()) != null) {
                                                instance2 = findUnit (instructions [1].ToString ());
                                                Debug.Log ("Unit already exists");
                                                //affixStruts (instance1, instance2, instructions [4]);
                                        } else {
                                                instance1 = findUnit (instructions [0].ToString ());
                                                instance2 = (GameObject)Instantiate (Resources.Load ("FunctionalUnits/F" + instructions [3]));
                                                instance2.transform.position = spawnLocation;
                                                instance2.transform.name = instructions [1].ToString ();

                                                FUArray.Add (instance2);
                                                instance2.transform.parent = parent.transform;
                                                placeNewUnit (instance1, instance2, instructions [4]);

                                                affixStruts (returnClosest (instance1, instance2) [0].gameObject, returnClosest (instance1, instance2) [1].gameObject);

                                        }
                                        previousInstance = instance1;

                                }

                        }
                        //	createPrefab (parent);

                        currentBP.instantiation = parent;
                }

                return true;
    }