public static List <ShipChromosomeNode> TreeCrossover(List <ShipChromosomeNode> selectionList) { List <ShipChromosomeNode> outputPopulation = new List <ShipChromosomeNode>(); while (selectionList.Count >= 2) { //SELECT TWO PARENTS AT RANDOM ShipChromosomeNode p1 = selectRandomElement(selectionList, true); ShipChromosomeNode p2 = selectRandomElement(selectionList, true); //if (p1 == p2) // Debug.LogError("SAME PARENTS"); //Debug.Log("P1: \n" + p1.getString() + "\n"); //Debug.Log("P2: \n" + p2.getString() + "\n"); //RETRIEVE A LIST OF THEIR NODES List <ShipChromosomeNode> p1Nodes = p1.getListOfNodes(); List <ShipChromosomeNode> p2Nodes = p2.getListOfNodes(); //SELECT ONE AT RANDOM FROM EACH PARENT ShipChromosomeNode p1CutNode = selectRandomElement(p1Nodes, false); ShipChromosomeNode p2CutNode = selectRandomElement(p2Nodes, false); //Debug.Log("P1 cut point: \n" + p1CutNode.getString() + "\n"); //Debug.Log("P2 cut point: \n" + p2CutNode.getString() + "\n"); //ADD THE OFFSPRING CREATED TO THE OUTPUT POPULATION outputPopulation.Add(createOffspring(p1, p1CutNode, p2CutNode)); outputPopulation.Add(createOffspring(p2, p2CutNode, p1CutNode)); } return(outputPopulation); }
public void generatePhysicalShip(ShipChromosomeNode root) { GameObject g = (GameObject)GameObject.Instantiate(Resources.Load(Config.HEAVY_BLOCK_PREFAB_LOCATION), Vector3.zero, Quaternion.identity); Rigidbody r = g.AddComponent <Rigidbody>(); r.mass = Config.BLOCK_MASS * root.getListOfNodes().Count; r.drag = Config.BLOCK_DRAG; r.angularDrag = Config.BLOCK_ANGULAR_DRAG; //r.constraints = RigidbodyConstraints.FreezeAll; r.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezePositionZ; BlockScript b = g.GetComponent <BlockScript>(); ShipController s = g.AddComponent <ShipController>(); s.rootNode = root; s.setOrbs(orbsRoot); lastShip = s; b.initialize(root, s); b.combineSubMeshes(); g.transform.position = transform.position; g.SetActive(false); }
//c indicates where the child's parent is in relation to it private void createChild(ShipChromosomeNode n, ChildNode c, ShipController s) { GameObject g = (GameObject)GameObject.Instantiate( Resources.Load(n.isEngine ? Config.ENGINE_PREFAB_LOCATION : Config.HEAVY_BLOCK_PREFAB_LOCATION), Vector3.zero, Quaternion.identity); if (n.isEngine) { EngineScript engine = g.GetComponent <EngineScript>(); engine.initialize(s, n.startEngageAngle, n.rangeEngageAngle); } g.transform.parent = transform; g.transform.localPosition = Vector3.zero; g.transform.localRotation = Quaternion.Euler(0, 0, n.relativeRotation); Vector3 colliderSize = g.GetComponent <MeshCollider>().bounds.size; switch (c) { case ChildNode.TOP: g.transform.localPosition = g.transform.localPosition + rotateByThisRotation(new Vector3(0, -colliderSize.y, 0)); break; case ChildNode.BOTTOM: g.transform.localPosition = g.transform.localPosition + rotateByThisRotation(new Vector3(0, colliderSize.y, 0)); break; case ChildNode.LEFT: g.transform.localPosition = g.transform.localPosition + rotateByThisRotation(new Vector3(colliderSize.x, 0, 0)); break; case ChildNode.RIGHT: g.transform.localPosition = g.transform.localPosition + rotateByThisRotation(new Vector3(-colliderSize.x, 0, 0)); break; } if (Physics.OverlapSphere(g.transform.position, (colliderSize.y < colliderSize.x ? colliderSize.y : colliderSize.x) / 2.1f).Length > 1) { GameObject.Destroy(g); return; } //FixedJoint fixedJoint = g.AddComponent<FixedJoint>(); //fixedJoint.enableCollision = false; //fixedJoint.connectedBody = rigidbody; BlockScript b = g.GetComponent <BlockScript>(); b.initialize(n, s); }
//c indicates where the child's parent is in relation to it public static ShipChromosomeNode generateRandomShip(int d, ChildNode c) { ShipChromosomeNode root = new ShipChromosomeNode(); root.depth = d; root.parentPos = c; root.isEngine = Random.Range(0, 2) == 1; root.startEngageAngle = Random.Range(0, 360); root.rangeEngageAngle = Random.Range(0, 360); root.relativeRotation = Random.Range(-Config.MAX_CHILD_ROTATION, Config.MAX_CHILD_ROTATION); float f; if (root.depth < Config.MAX_SHIP_DEPTH) { //top if (c != ChildNode.TOP) { f = Random.Range(0.0f, 1.0f); if (f <= Config.CHANCE_OF_CHILD_NODE) { root.top = ShipChromosomeNode.generateRandomShip(root.depth + 1, ChildNode.BOTTOM); } } //bottom if (c != ChildNode.BOTTOM) { f = Random.Range(0.0f, 1.0f); if (f <= Config.CHANCE_OF_CHILD_NODE) { root.bottom = ShipChromosomeNode.generateRandomShip(root.depth + 1, ChildNode.TOP); } } //left if (c != ChildNode.LEFT) { f = Random.Range(0.0f, 1.0f); if (f <= Config.CHANCE_OF_CHILD_NODE) { root.left = ShipChromosomeNode.generateRandomShip(root.depth + 1, ChildNode.RIGHT); } } //right if (c != ChildNode.RIGHT) { f = Random.Range(0.0f, 1.0f); if (f <= Config.CHANCE_OF_CHILD_NODE) { root.right = ShipChromosomeNode.generateRandomShip(root.depth + 1, ChildNode.LEFT); } } } return(root); }
public static void TreeMutate(List <ShipChromosomeNode> crossoverList) { foreach (ShipChromosomeNode s in crossoverList) { if (rnd.NextDouble() < Config.MUTATION_PROBABILITY) { List <ShipChromosomeNode> subNodes = s.getListOfNodes(); ShipChromosomeNode mutateNode = selectRandomElement(subNodes, false); ChildNode parentPos = mutateNode.parentPos; ShipChromosomeNode parent = null; switch (parentPos) { case ChildNode.BOTTOM: parent = mutateNode.bottom; break; case ChildNode.LEFT: parent = mutateNode.left; break; case ChildNode.RIGHT: parent = mutateNode.right; break; case ChildNode.TOP: parent = mutateNode.top; break; } mutateNode = ShipChromosomeNode.generateRandomShip(mutateNode.depth, parentPos); switch (parentPos) { case ChildNode.BOTTOM: mutateNode.bottom = parent; parent.top = mutateNode; break; case ChildNode.LEFT: mutateNode.left = parent; parent.right = mutateNode; break; case ChildNode.RIGHT: mutateNode.right = parent; parent.left = mutateNode; break; case ChildNode.TOP: mutateNode.top = parent; parent.bottom = mutateNode; break; } } } }
private static ShipChromosomeNode createOffspring(ShipChromosomeNode p1, ShipChromosomeNode p1CutNode, ShipChromosomeNode p2CutNode, int depth, ShipChromosomeNode parent, ChildNode parentPos) { if (p1 == p1CutNode) { p1 = p2CutNode; } ShipChromosomeNode o = p1.copyNode(); o.depth = depth; o.parentPos = parentPos; switch (parentPos) { case ChildNode.BOTTOM: o.bottom = parent; break; case ChildNode.LEFT: o.left = parent; break; case ChildNode.RIGHT: o.right = parent; break; case ChildNode.TOP: o.top = parent; break; } if (p1.top != null && p1.parentPos != ChildNode.TOP) { o.top = createOffspring(p1.top, p1CutNode, p2CutNode, depth + 1, o, ChildNode.BOTTOM); } if (p1.left != null && p1.parentPos != ChildNode.LEFT) { o.left = createOffspring(p1.left, p1CutNode, p2CutNode, depth + 1, o, ChildNode.RIGHT); } if (p1.right != null && p1.parentPos != ChildNode.RIGHT) { o.right = createOffspring(p1.right, p1CutNode, p2CutNode, depth + 1, o, ChildNode.LEFT); } if (p1.bottom != null && p1.parentPos != ChildNode.BOTTOM) { o.bottom = createOffspring(p1.bottom, p1CutNode, p2CutNode, depth + 1, o, ChildNode.TOP); } return(o); }
public ShipChromosomeNode copyNode() { ShipChromosomeNode s = new ShipChromosomeNode(); s.isEngine = isEngine; s.relativeRotation = relativeRotation; s.startEngageAngle = startEngageAngle; s.rangeEngageAngle = rangeEngageAngle; s.parentPos = parentPos; s.depth = depth; return(s); }
public static void NodeMutate(List <ShipChromosomeNode> crossoverList) { foreach (ShipChromosomeNode s in crossoverList) { if (rnd.NextDouble() < Config.MUTATION_PROBABILITY) { List <ShipChromosomeNode> subNodes = s.getListOfNodes(); ShipChromosomeNode mutateNode = selectRandomElement(subNodes, false); mutateNode.isEngine = UnityEngine.Random.Range(0, 2) == 1; mutateNode.startEngageAngle = UnityEngine.Random.Range(0, 360); mutateNode.rangeEngageAngle = UnityEngine.Random.Range(0, 360); mutateNode.relativeRotation = UnityEngine.Random.Range(-Config.MAX_CHILD_ROTATION, Config.MAX_CHILD_ROTATION); } } }
private ShipChromosomeNode copyTree(ShipChromosomeNode parent) { ShipChromosomeNode s = copyNode(); if (parentPos == ChildNode.TOP) { s.top = parent; } else if (top != null) { s.top = top.copyTree(s); } if (parentPos == ChildNode.LEFT) { s.left = parent; } else if (left != null) { s.left = left.copyTree(s); } if (parentPos == ChildNode.RIGHT) { s.right = parent; } else if (right != null) { s.right = right.copyTree(s); } if (parentPos == ChildNode.BOTTOM) { s.bottom = parent; } else if (bottom != null) { s.bottom = bottom.copyTree(s); } return(s); }
public void nextGeneration() { if (lastShip != null) { GameObject.Destroy(lastShip.gameObject); } populationManager.GetComponents <Transform>(); ShipChromosomeNode bestOfAllTime = populationManager.getBestOfAllTime(); if (bestOfAllTime == null) { generateRandomShip(); } else { generatePhysicalShip(bestOfAllTime); //Random.Range(0, shipArchives.Count / 2)].root); } activate(); }
public void initialize(ShipChromosomeNode n, ShipController s) { shipController = s; s.limbCount++; if (n.top != null) { createChild(n.top, ChildNode.BOTTOM, s); } if (n.bottom != null) { createChild(n.bottom, ChildNode.TOP, s); } if (n.left != null) { createChild(n.left, ChildNode.RIGHT, s); } if (n.right != null) { createChild(n.right, ChildNode.LEFT, s); } }
/*public int limbCount() * { * int r = 1 + (top == null ? 0 : (parentPos == ChildNode.TOP ? 0 : top.limbCount())) + (bottom == null ? 0 : (parentPos == ChildNode.BOTTOM ? 0 : bottom.limbCount())) + (left == null ? 0 : (parentPos == ChildNode.LEFT ? 0 : left.limbCount())) + (right == null ? 0 : (parentPos == ChildNode.RIGHT ? 0 : right.limbCount())); + + return r; + }*/ public ShipChromosomeNode copyTree() { ShipChromosomeNode s = copyNode(); if (top != null) { s.top = top.copyTree(s); } if (left != null) { s.left = left.copyTree(s); } if (right != null) { s.right = right.copyTree(s); } if (bottom != null) { s.bottom = bottom.copyTree(s); } return(s); }
public void generateRandomShip() { ShipChromosomeNode n = ShipChromosomeNode.generateRandomShip(); generatePhysicalShip(n); }
public ShipArchive(ShipChromosomeNode rootNode, double fitness) { this.root = rootNode; this.fitness = fitness; }
private static ShipChromosomeNode createOffspring(ShipChromosomeNode p1, ShipChromosomeNode p1CutNode, ShipChromosomeNode p2CutNode) { return(createOffspring(p1, p1CutNode, p2CutNode, 0, null, ChildNode.NONE)); }