// Update is called once per frame void Update() { if (art != null && art.NeedsRedraw()) { art.ApplyImageProcess(); img = art.GetArtwork(); renderer.material.mainTexture = img; Debug.Log("Image applied"); } if (!PauseMenu.isPaused && Input.GetButtonDown("Fire2")) { img = new Texture2D(width, height, TextureFormat.ARGB32, true); art = new Artwork(); textbox.Text("New image and genome"); } if (!PauseMenu.isPaused && Input.GetButtonDown("Fire1")) { TWEANNGenotype geno = art.GetGenotype(); geno.Mutate(); textbox.Text("Mutating..."); art = new Artwork(geno); } }
private int MUTATION_CYCLES = 12; // maximum mutations per evolution public RoomConfiguration(RoomConfiguration parentRoom, int returnPortalID, int championPortalID, Artwork[] artworksPassed, Sculpture[] sculptures) { ArtGallery ag = ArtGallery.GetArtGallery(); Artwork champion = artworksPassed[championPortalID]; if (ArtGallery.DEBUG_LEVEL < ArtGallery.DEBUG.NONE) { Debug.Log("Creating a new room with " + artworksPassed.Length + " artworks"); } this.parentRoom = parentRoom; rooms = new RoomConfiguration[artworksPassed.Length]; if (ArtGallery.DEBUG_LEVEL < ArtGallery.DEBUG.NONE) { Debug.Log("Clearing artworks and sculptures..."); } artworks = new Artwork[artworksPassed.Length]; this.sculptures = sculptures; if (ArtGallery.DEBUG_LEVEL < ArtGallery.DEBUG.NONE) { Debug.Log("Created new artworks: " + artworksPassed.Length); } rooms[returnPortalID] = parentRoom; // clone champion to each artwork and mutate for (int i = 0; i < artworksPassed.Length; i++) { TWEANNGenotype geno = new TWEANNGenotype(champion.GetGenotype().Copy()); // champion art if (i == championPortalID) { //do little geno.Mutate(); } // return art else if (i == returnPortalID) { // do nothing - save some cpu } else { // all other art TWEANNCrossover cross = new TWEANNCrossover(false) { Sucessful = false }; //HACK PROTOTYPE hardcoded value //TWEANNGenotype crossedGeno = cross.Crossover(new TWEANNGenotype(geno.Copy()), new TWEANNGenotype(champion.GetGenotype().Copy())); //geno = crossedGeno; for (int m = 0; m < Random.Range(2, ag.artworkMutationChances); m++) { geno.Mutate(); } } artworks[i] = new Artwork(geno); } MutateSculptures(); }
private void MutateArtworks() { leftGeno = new TWEANNGenotype(NUM_INPUTS, NUM_OUTPUTS, 0); rightGeno = new TWEANNGenotype(NUM_INPUTS, NUM_OUTPUTS, 0); for (int i = 0; i < 10; i++) { leftGeno.Mutate(); rightGeno.Mutate(); } }
/// <summary> /// Create a new artwork in a room with a given genotype /// </summary> /// <param name="geno">Genotype for this artwrok to use</param> public Artwork(TWEANNGenotype geno) { ag = ArtGallery.GetArtGallery(); needsRedraw = false; processingCPPN = false; this.geno = geno; cppnProcess = new Thread(new ThreadStart(GenerateImageFromCPPN)); img = new Texture2D(width, height, TextureFormat.ARGB32, false); pixels = new Color[width * height]; //GenerateImageFromCPPN(); // non threaded version of generation cppnProcess.Start(); }
SaveGameArtwork(Artwork art) { TWEANNGenotype TGeno = art.GetGenotype(); geno = new SaveGameGenotype { ID = TGeno.ID, NumberInputs = TGeno.numInputs, NumberOutputs = TGeno.numOutputs, ArchetypeIndex = TGeno.archetypeIndex, }; }
/// <summary> /// Create a new sculpture from a geno /// </summary> /// <param name="geno">TWEANNGenotype</param> public void NewSculpture(TWEANNGenotype geno) { backupGeno = geno.Copy(); this.geno = geno; cppn = new TWEANN(geno); if (cppn.Running) { geno = backupGeno; } // GenerateCPPN(); DrawSculpture(); }
public TWEANN(TWEANNGenotype g) { Running = true; ArchetypeIndex = g.GetArchetypeIndex(); nodes = new List <TWEANNNode>(g.Nodes.Count); int countIn = 0, countOut = 0; if (ArtGallery.DEBUG_LEVEL > ArtGallery.DEBUG.NONE) { Debug.Log("Starting TWEANNNodes build..."); } foreach (NodeGene node in g.Nodes) { TWEANNNode tempNode = new TWEANNNode(node.fTYPE, node.nTYPE, node.Innovation, false, node.GetBias()); nodes.Add(tempNode); if (node.nTYPE == NTYPE.INPUT) { countIn++; } else if (node.nTYPE == NTYPE.OUTPUT) { countOut++; } } numInputs = countIn; numOutputs = countOut; if (ArtGallery.DEBUG_LEVEL > ArtGallery.DEBUG.NONE) { Debug.Log("Starting TWEANNLinks build..."); } foreach (LinkGene link in g.Links) { TWEANNNode source = GetNodeByInnovationID(link.GetSourceInnovation()); TWEANNNode target = GetNodeByInnovationID(link.GetTargetInnovation()); if (source == null) { throw new System.Exception("Source not found with innovation " + link.GetSourceInnovation() + " : " + ToString()); } if (target == null) { throw new System.Exception("Target not found with innovation " + link.GetTargetInnovation() + " : " + ToString()); } source.Connect(target, link.GetWeight(), link.Innovation, false, false); } if (ArtGallery.DEBUG_LEVEL > ArtGallery.DEBUG.NONE) { Debug.Log("TWEANN build from TWEANNGenotype completed"); } Running = false; }
public void MutateSculptures() { ArtGallery ag = ArtGallery.GetArtGallery(); //Sort Sculptures Sculpture[] toMutate = new Sculpture[sculptures.Length]; TWEANNGenotype sculptureChampion = null; for (int s = 0; s < sculptures.Length; s++) { if (sculptures[s].GetSelected()) { sculptureChampion = new TWEANNGenotype(sculptures[s].GetGenotype().Copy()); sculptures[s].SetSelected(false); toMutate[s] = sculptures[s]; } else { toMutate[s] = sculptures[s]; } } //Select sculpture champion and crossover / mutate if (sculptureChampion != null) { for (int m = 0; m < sculptures.Length; m++) { Sculpture ms = toMutate[m]; if (ms != null) { TWEANNGenotype crossedGeno = new TWEANNGenotype(sculptureChampion.Copy()); TWEANNCrossover cross = new TWEANNCrossover(false) { Sucessful = false }; // HACK PROTOTYPE hardcoded value //TWEANNGenotype crossedmgeno = cross.Crossover(new TWEANNGenotype(sculptureChampion.Copy()), new TWEANNGenotype(ms.GetGenotype().Copy())); //crossedGeno = crossedmgeno; for (int mr = 0; mr < Random.Range(2, ag.sculptureMutationChances); mr++) //HACK PROTOTYPE hardcoded value for mutation rate { crossedGeno.Mutate(); } ms.NewSculpture(new TWEANNGenotype(crossedGeno)); } } } }
private void Start() { cppnProcess = new Thread(new ThreadStart(DrawSculpture)); //inputs: (x,y,z) outputs: r,g,b and presence geno = new TWEANNGenotype(8, 4, 0); // Use archetype 0 for test chamber vox = new GameObject[SCULP_X, SCULP_Z, SCULP_Y]; platform = Instantiate(SculturePlatformObject) as GameObject; platform.transform.position = new Vector3(transform.position.x, transform.position.y - 1.25f, transform.position.z); voxelSize = 5 * 0.5f / SCULP_X; // ActivationFunctions.ActivateAllFunctions(); // FIXME all functions active BuildSculpture(); GenerateCPPN(); //DrawSculpture(); cppnProcess.Start(); }
// copy() public TWEANNGenotype Copy() { List <NodeGene> copyNodes = new List <NodeGene>(); List <LinkGene> copyLinks = new List <LinkGene>(); foreach (LinkGene lg in Links) { copyLinks.Add(new LinkGene(lg.GetSourceInnovation(), lg.GetTargetInnovation(), lg.GetWeight(), lg.Innovation)); } foreach (NodeGene ng in Nodes) { copyNodes.Add(new NodeGene(ng.nTYPE, ng.fTYPE, ng.Innovation)); } TWEANNGenotype copy = new TWEANNGenotype(copyNodes, copyLinks, archetypeIndex); return(copy); }
public TWEANNGenotype Crossover(TWEANNGenotype toModify, TWEANNGenotype toReturn) { AddToCSV("toModify pre", DebugNodes(toModify.Nodes)); AddToCSV("toReturn pre", DebugNodes(toReturn.Nodes)); AddToCSV("toModify links pre", DebugLinks(toModify.Links)); AddToCSV("toReturn links pre", DebugLinks(toReturn.Links)); List <List <NodeGene> > alignedNodes = new List <List <NodeGene> >(2) { AlignNodesToArchetype(toModify.Nodes, toModify.GetArchetypeIndex()), AlignNodesToArchetype(toReturn.Nodes, toReturn.GetArchetypeIndex()) }; AddToCSV("toModify aligned", DebugNodes(alignedNodes[0])); AddToCSV("toReturn aligned", DebugNodes(alignedNodes[1])); List <List <NodeGene> > crossedNodes = CrossNodes(alignedNodes[0], alignedNodes[1]); AddToCSV("toModify crossed", DebugNodes(crossedNodes[0])); AddToCSV("toReturn crossed", DebugNodes(crossedNodes[1])); List <List <LinkGene> > alignedLinks = AlignLinkGenes(toModify.Links, toReturn.Links); AddToCSV("toModify aligned links", DebugLinks(alignedLinks[0])); AddToCSV("toReturn aligned links", DebugLinks(alignedLinks[1])); List <List <LinkGene> > crossedLinks = CrossLinks(alignedLinks[0], alignedLinks[1]); AddToCSV("toModify crossed links", DebugLinks(crossedLinks[0])); AddToCSV("toReturn crossed links", DebugLinks(crossedLinks[1])); toModify.Nodes = crossedNodes[0]; toModify.Links = crossedLinks[0]; toReturn.Nodes = crossedNodes[1]; toReturn.Links = crossedLinks[1]; Sucessful = true; //SaveCSV(); return(toReturn); }
// Update is called once per frame void Update() { if (leftArt.NeedsRedraw()) { leftArt.ApplyImageProcess(); leftImg = leftArt.GetArtwork(); leftRenderer.material.mainTexture = leftImg; Debug.Log("leftImg applied"); } if (rightArt.NeedsRedraw()) { rightArt.ApplyImageProcess(); rightImg = rightArt.GetArtwork(); rightRenderer.material.mainTexture = rightImg; Debug.Log("rightImg applied"); } if (!PauseMenu.isPaused && Input.GetButtonDown("Fire2")) { leftImg = new Texture2D(width, height, TextureFormat.ARGB32, true); rightImg = new Texture2D(width, height, TextureFormat.ARGB32, true); BuildArtworks(); //leftArt = new Artwork(leftGeno); //rightArt = new Artwork(rightGeno); } if (!PauseMenu.isPaused && Input.GetButtonDown("Fire1")) { leftGeno = new TWEANNGenotype(leftArt.GetGenotype().Copy()); rightGeno = new TWEANNGenotype(rightArt.GetGenotype().Copy()); TWEANNCrossover cr = new TWEANNCrossover(false); cr.Crossover(leftGeno, rightGeno); leftImg = new Texture2D(width, height, TextureFormat.ARGB32, true); rightImg = new Texture2D(width, height, TextureFormat.ARGB32, true); leftArt = new Artwork(leftGeno); rightArt = new Artwork(rightGeno); } }
private void Update() { if (Input.GetMouseButtonDown(0)) { RaycastHit hit; Ray ray = new Ray(camera.transform.position, camera.transform.forward * interactionDistance); if (Physics.Raycast(ray, out hit)) { Transform objectHit = hit.transform; if (hit.collider.tag == "portal") { Portal p = hit.collider.gameObject.GetComponent <Portal>(); Texture2D img = new Texture2D(p.GetImage().width, p.GetImage().height, TextureFormat.ARGB32, false); Graphics.CopyTexture(p.GetImage(), img); int portalID = p.GetPortalID(); TWEANNGenotype geno = new TWEANNGenotype(ag.GetArtwork(portalID).GetGenotype().Copy()); SavedArtwork newArtwork = new SavedArtwork { Image = Sprite.Create(img, new Rect(0, 0, img.width, img.height), new Vector2(0.5f, 0.5f)) as Sprite, Geno = geno }; AddItem(newArtwork); FindNextEmptySlot(); } if (hit.collider.tag == "sculpture") { Sculpture s = hit.collider.gameObject.GetComponent <Sculpture>(); sculptureGeno = s.GetComponent <Sculpture>().GetGenotype(); ag.SelectSculpture(s); //s.GetComponent<Sculpture>().SetSelected(!s.GetComponent<Sculpture>().GetSelected()); } } } if (Input.GetMouseButtonDown(1)) { RaycastHit hit; Ray ray = new Ray(camera.transform.position, camera.transform.forward * interactionDistance); if (Physics.Raycast(ray, out hit)) { Transform objectHit = hit.transform; if (hit.collider.tag == "portal") { Portal p = hit.collider.gameObject.GetComponent <Portal>(); int portalID = p.GetPortalID(); Artwork art = ag.GetArtwork(portalID); if (GetActiveSlotItem() != null) { ag.RemoveRoom(portalID); art.SetGenotype(GetActiveSlotItem().Geno); // FIXME Null ref possible here - add checks art.Refresh(); art.ApplyImageProcess(); items[ActiveSlot] = null; hud.UpdateInventoryThumbnail(ActiveSlot, null); } else { // do nothing for now } } if (hit.collider.tag == "sculpture") { Sculpture s = hit.collider.gameObject.GetComponent <Sculpture>(); sculptureGeno = s.GetComponent <Sculpture>().GetGenotype(); ag.ResetSculpture(s); //s.GetComponent<Sculpture>().SetSelected(!s.GetComponent<Sculpture>().GetSelected()); } } } if (Input.GetKeyDown(KeyCode.Keypad4)) { CycleActiveSlot(-1); } if (Input.GetKeyDown(KeyCode.Keypad6)) { CycleActiveSlot(1); } float wheel = Input.GetAxis("Mouse ScrollWheel"); if (wheel < 0f) { //scroll down CycleActiveSlot(-1); } else if (wheel > 0f) { //scroll up CycleActiveSlot(1); } }
public void SetGenotype(TWEANNGenotype geno) { this.geno = geno; }
public TWEANNGenotype(TWEANNGenotype copy) : this(copy.Nodes, copy.Links, copy.archetypeIndex) { }
public void Start() { xorTest = new TWEANNGenotype(2, 1, 0); List <NodeGene> nodes = xorTest.Nodes; /* Quick test for dotProduct */ float[] dotProdTestInputs = new float[] { 3, 5 }; Debug.Log("Staring test: dotproduct using inputs 3, 5"); float[] dotProdTestResults = new TWEANN(xorTest).Process(dotProdTestInputs); foreach (float sum in dotProdTestResults) { Debug.Log("Ending test: dotproduct = " + sum); } xorTest.SpliceNode(FTYPE.TANH, 2158, -1, -3, -0.3964445706032944f, -0.4269614531487551f, 100, 101); xorTest.AddLink(-1, -3, -0.5081867337293002f, 106); xorTest.SpliceNode(FTYPE.TANH, 150, -2, -3, -3.275181751309399f, -0.9183280870360113f, 102, 103); xorTest.AddLink(-1, 150, -2.202539496376981f, 104); xorTest.AddLink(-2, 2158, 0.9295958853236486f, 105); // Set activation function of output node xorTest.GetNodeByInnovationID(-1).fTYPE = FTYPE.TANH; xorTest.GetNodeByInnovationID(-2).fTYPE = FTYPE.TANH; xorTest.GetNodeByInnovationID(-3).fTYPE = FTYPE.TANH; // Set bias manually // Set weights manually /* List all nodes to output to verify network */ foreach (NodeGene ng in xorTest.Nodes) { Debug.Log(ng.ToString()); } foreach (LinkGene lg in xorTest.Links) { Debug.Log(lg.ToString()); } /* XOR test */ List <float[]> inputs = new List <float[]>(); inputs.Add(new float[] { 0, 0 }); inputs.Add(new float[] { 0, 1 }); inputs.Add(new float[] { 1, 0 }); inputs.Add(new float[] { 1, 1 }); TWEANN XORNetwork = new TWEANN(xorTest); for (int test = 0; test < inputs.Count; test++) { Debug.Log(""); string debugString = "Starting test using inputs "; foreach (float f in inputs[test]) { debugString += f + ", "; } Debug.Log(debugString); float[] results = XORNetwork.Process(inputs[test]); foreach (float sum in results) { Debug.Log("Ending test: result = " + sum); } } }