void SaveBestNetwork() { try { string m_Path = Application.dataPath + "/" + transform.root.gameObject.name + ".xml"; NeuralWeights neuralWeights = new NeuralWeights(); neuralWeights.bestScore = (float)bestScore; neuralWeights.Generation = generation; //Save this Network if (!newNetworkVersion) { neuralWeights.weights = networks[0].getWeigths(); } else { neuralWeights.HiddenLayers = newNetworks.HiddenLayers; neuralWeights.InputLayer = newNetworks.InputLayer; neuralWeights.OutputLayer = newNetworks.OutputLayer; } SaveWeights(m_Path, neuralWeights); } catch (Exception) { print("[" + transform.root.gameObject.name + "] Save error"); } }
void SaveWeights(string path, NeuralWeights neuralWeights) { using (FileStream fs = new FileStream(path, FileMode.Create)) { XmlSerializer xSer = new XmlSerializer(typeof(NeuralWeights)); xSer.Serialize(fs, neuralWeights); } }
// Use this for initialization void Start() { childFolder = new GameObject(); childFolder.name = "[" + transform.root.gameObject.name + "] Childs"; stats = new NeuralChildObject(); InvokeRepeating("mutationUpdate", 1, 1); startPosition = transform.position; startRotation = transform.rotation; stats.LastPosition = startPosition; ignoreCollide = ignoreFirstCollide; Debug.Log("[" + transform.root.gameObject.name + "] Generation " + generation); results = new double[4]; points = new double[population]; sensors = new double[10]; networks = new Network[population]; newNetworks = new NeuralNetwork.NetworkModels.Network(parameters[0], new int[] { 8, 8, 8 }, 4, 2, 1); gameObjectsChilds = new GameObject[population]; gameObjectsChilds[0] = this.gameObject; NeuralWeights loadedWeightsValues = null; if (LoadLastNerual) { string m_Path = Application.dataPath + "/" + transform.root.gameObject.name + ".xml"; print(m_Path); loadedWeightsValues = Load(m_Path); if (loadedWeightsValues != null) { bestScore = loadedWeightsValues.bestScore; generation = loadedWeightsValues.Generation; print("[" + transform.root.gameObject.name + "] LoadedWeights"); } } if (newNetworkVersion) { if (loadedWeightsValues != null) { newNetworks.HiddenLayers = loadedWeightsValues.HiddenLayers; newNetworks.InputLayer = loadedWeightsValues.InputLayer; newNetworks.OutputLayer = loadedWeightsValues.OutputLayer; } for (int i = 1; i < population; i++) { //Spawn childs if wanted and can if (spawnChild && child != null) { SpawnChild(); } } } else { networks[0] = new Network(parameters); for (int i = 1; i < population; i++) { //Spawn childs if wanted and can if (spawnChild && child != null) { SpawnChild(); } if (loadedWeightsValues == null) { networks[i] = new Network(parameters); } else { networks[i] = new Network(parameters); networks[i].setWeights(loadedWeightsValues.weights); networks[i] = new Network(this, networks[i]); } } } }