void OnEnable() { existingCollections = NeatController.GetExistingCollections(); existingTrees = TreeInputAgent.GetExistingTrees(); // No collections on disc so cannot load if (existingCollections.Length == 0) { populationName.gameObject.SetActive(false); treeCount.SetRange(0, MaxCharacterCount); treeCount.Value = treeCount.Max; treeCount.Interactable = false; neuralCount.SetRange(0, MaxCharacterCount); neuralCount.Value = 0; neuralCount.Interactable = false; existingCollections = new string[] { "AiArena" }; } else { populationName.gameObject.SetActive(true); treeCount.Interactable = true; neuralCount.Interactable = true; // Update displayed options populationName.ClearOptions(); List <Dropdown.OptionData> options = new List <Dropdown.OptionData>(); foreach (string collection in existingCollections) { options.Add(new Dropdown.OptionData(collection)); } populationName.AddOptions(options); } if (existingTrees.Length == 0) { existingTrees = new string[] { "Null" }; } // Update displayed options treeName.ClearOptions(); List <Dropdown.OptionData> treeOptions = new List <Dropdown.OptionData>(); foreach (string collection in existingTrees) { treeOptions.Add(new Dropdown.OptionData(collection)); } treeName.AddOptions(treeOptions); populationName.value = 0; treeName.value = 0; }
/// <summary> /// Select the fitest network to view /// </summary> public void SelectNewTarget() { Character[] characters = GameMode.main.characters; viewTarget = null; if (GameMode.main.aliveCount == 0 || characters == null) { return; } for (int i = 0; i < characters.Length; ++i) { // Always select the player to view if (characters[i].IsPlayer && characters[i].isAlive) { viewTarget = characters[i]; break; } // Select character with highest score if (characters[i].isAlive && (viewTarget == null || characters[i].TotalScore > viewTarget.TotalScore)) { viewTarget = characters[i]; } } // Pre-emptively disable eveything networkPreview.gameObject.SetActive(false); playerPreview.gameObject.SetActive(false); treePreview.gameObject.SetActive(false); // Update UI if (viewTarget == null) { return; } PlayerInput playerInput = viewTarget.GetComponent <PlayerInput>(); NeuralInputAgent neuralInput = viewTarget.GetComponent <NeuralInputAgent>(); TreeInputAgent treeInput = viewTarget.GetComponent <TreeInputAgent>(); if (playerInput != null) { playerPreview.gameObject.SetActive(true); playerPreview.SetVisualisation(playerInput); } if (neuralInput != null) { networkPreview.gameObject.SetActive(true); networkPreview.SetVisualisation(neuralInput); } if (treeInput != null) { treePreview.gameObject.SetActive(true); treePreview.SetVisualisation(treeInput); } }
/// <summary> /// Create objects for all the characters /// </summary> private void SpawnCharacters() { // Cleanup old characters if (characters != null) { foreach (Character character in characters) { if (character != null) { Destroy(character.gameObject); } } } // Spawn new collections Debug.Log("Spawning " + characterCount + " characters"); characters = new Character[characterCount]; for (int i = 0; i < characterCount; ++i) { characters[i] = Instantiate(defaultCharacter); } // Add player if (playerExists) { characters[0].gameObject.AddComponent <PlayerInput>(); } // Add agents int totalAgents = playerExists ? characterCount - 1 : characterCount; NeuralInputAgent[] neuralAgents = new NeuralInputAgent[neuralAgentCount]; for (int i = 0; i < totalAgents; ++i) { int index = playerExists ? i + 1 : i; if (i < neuralAgentCount) { NeuralInputAgent agent = characters[index].gameObject.AddComponent <NeuralInputAgent>(); neuralAgents[i] = agent; } else { TreeInputAgent agent = characters[index].gameObject.AddComponent <TreeInputAgent>(); agent.LoadTree(desiredTreeAiName); } } PlaceCharactersInRing(); // Place characters NeuralController.main.AttachNetworks(neuralAgents); // Attach networks to nn agents }
/// <summary> /// Update this visualisation /// </summary> /// <param name="input">The player to view</param> public void SetVisualisation(TreeInputAgent input) { currentTarget = input; // Clear columns foreach (KeyValueText kv in leftColumn) { Destroy(kv.gameObject); } foreach (KeyValueText kv in rightColumn) { Destroy(kv.gameObject); } leftColumn.Clear(); rightColumn.Clear(); // Create new columns if (currentTarget != null) { // Left column is agent vars int i = 0; foreach (var pair in currentTarget.tree.agentProfile.variables) { KeyValueText kv = Instantiate(leftColumnDefault, leftColumnDefault.transform.parent); RectTransform trans = kv.GetComponent <RectTransform>(); trans.position = trans.position + new Vector3(0, -trans.rect.height * i * 1.25f, 0); kv.Key = pair.Key; kv.Value = "" + pair.Value; kv.gameObject.SetActive(true); leftColumn.Add(kv); ++i; } // Right column is global vars i = 0; foreach (var pair in currentTarget.tree.globalVars.variables) { KeyValueText kv = Instantiate(rightColumnDefault, rightColumnDefault.transform.parent); RectTransform trans = kv.GetComponent <RectTransform>(); trans.position = trans.position + new Vector3(0, -trans.rect.height * i * 1.25f, 0); kv.Key = pair.Key; kv.Value = "" + pair.Value; kv.gameObject.SetActive(true); rightColumn.Add(kv); ++i; } } }