public void AttachFenotipo(FenotipoRN f) { fenotipo = f; foreach (KeyValuePair<int,Nodo> k_val in nodi) k_val.Value.nodoFenotipo = f.GetNeuroneById(k_val.Key); }
public void Update(float dt) { bool returnFitness = false; double[] inputVector = new double[5]; SortedList<int, double> output; inputVector[0] = GetPoleRotation(1)/(Convert.ToSingle(Math.PI)); inputVector[1] = GetPoleRotation(2)/(Convert.ToSingle(Math.PI)); inputVector[2] = GetPoleAngularSpeed(1); inputVector[3] = GetPoleAngularSpeed(2); inputVector[4] = GetCartPosition()/10; // Normalizzato a 10 metri di scostamento if (fenotipo != null) { fenotipo.sensori(inputVector); fenotipo.Calcola(); output = fenotipo.aggiorna(); ApplyForce(Convert.ToSingle(output.First().Value)); if (Math.Abs(GetPoleRotation(1)) < Const.FITNESS_ANGLE) currentFitness++; if (Math.Abs(GetPoleRotation(2)) < Const.FITNESS_ANGLE) currentFitness++; if (timeStep > Const.TIME_STEP) { fenotipo = null; resetCart(); returnFitness = true; ReturnFitnessEvent(currentFitness); } timeStep++; } world.Step(dt); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { rectTexture = DrawingHelper.GenerateRectangularTexture(graphics.GraphicsDevice, 1000, 1000, Color.White); IsMouseVisible = true; IsFixedTimeStep = true; GestoreRN_NEAT gestore = new GestoreRN_NEAT(3, 2); GenotipoRN genotipo = gestore.getPerceptron(); GenotipoRN g_mutato = gestore.mutazioneAggiungiNeurone(genotipo); g_mutato = gestore.mutazioneAggiungiNeurone(g_mutato); g_mutato = gestore.mutazioneAggiungiAssone(g_mutato); grafo = new GrafoDisegno(g_mutato); fenotipo = new FenotipoRN(g_mutato); input = new double[fenotipo.numNeuroniSensori]; input[0] = -1; for (int i = 1; i < fenotipo.numNeuroniSensori; i++) input[i] = input[i-1] + 2/(double)(fenotipo.numNeuroniSensori-1); grafo.AttachFenotipo(fenotipo); base.Initialize(); }
public void SetFenotipo(FenotipoRN fenotipo) { this.fenotipo = fenotipo; timeStep = 0; currentFitness = 0; }
private void ServerMethod() { TcpListener server = null; NetworkStream stream = null; TcpClient client = null; Int32 port = 13001, i; IPAddress localAddr = IPAddress.Parse("127.0.0.1"); bool fine = false; Byte[] bytes = new Byte[256]; String data = null; //Thread trdBB; server = new TcpListener(localAddr, port); server.Start(); while (true) { fine = false; client = server.AcceptTcpClient(); stream = client.GetStream(); while (!fine) if ((i = stream.Read(bytes, 0, bytes.Length)) != 0) //legge fino a 256 caratteri dallo stream di rete { data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); // interpreta lo stream a blocchi di 1Byte cod.ASCII data.ToLower(); /* Qui si mettono i comandi a cui risponde il server */ if (data == "IRN") { //data = "Inizio Ricezione"; genotipoCorrente = LibreriaRN.GenotipoRN.receiveNetwork(stream); grafo = new GrafoDisegno(genotipoCorrente); data = "Ricezione rete neurale avvenuta\n"; } else if (data == "GF") { fenotipo = new FenotipoRN(genotipoCorrente); grafo.AttachFenotipo(fenotipo); data = "Fenotipo generato con successo\n"; } else if (data == "INPUT") { String sInput; String[] parsedInput; char[] separatori = { ' ' }; if ((i = stream.Read(bytes, 0, bytes.Length)) != 0) //legge fino a 256 caratteri dallo stream di rete { sInput = System.Text.Encoding.ASCII.GetString(bytes, 0, i); // interpreta lo stream a blocchi di 1Byte cod.ASCII parsedInput = sInput.Split(separatori, StringSplitOptions.RemoveEmptyEntries); for (i = 0; i < fenotipo.numNeuroniSensori; i++) input[i] = Double.Parse(parsedInput[i]); data = "Input ricevuti\n"; } else data = "Errore nella ricezione degli input\n"; } else if (data == "AGG") { SortedList<int, double> output; data = ""; fenotipo.sensori(input); fenotipo.Calcola(); output = fenotipo.aggiorna(); foreach (KeyValuePair<int, double> k_val in output) { data += k_val.Key.ToString() + ": " + k_val.Value.ToString() + "\n"; } } else if (data == "exit" || data == "quit") { fine = true; data = "Server disconnesso...\n"; } else data = data.ToUpper(); byte[] msg = System.Text.Encoding.ASCII.GetBytes(data); stream.Write(msg, 0, msg.Length); } // Shutdown and end connection if (fine) System.Threading.Thread.Sleep(50); //Aspetta che il client legga client.Close(); } }