/// <summary> /// Start the server listening on the specified endpoint. /// </summary> /// <param name="endPoint">End point.</param> public void Start(IPEndPoint endPoint) { Log.Info(string.Format("Attempting to load the assemby {0}.", FitnessAssemblyName)); if (File.Exists(FitnessAssemblyName)) { _fitnessAssembly = new FitnessAssembly(FitnessAssemblyName); if (FitnessAssemblyName == null) { Log.Error(string.Format("The Assembly {0} exists, but could not be loaded.", FitnessAssemblyName)); } else { Log.Info(string.Format("Assembly {0} has been loaded.", FitnessAssemblyName)); } } else { Log.Warning(string.Format("Assembly {0} not loaded. File does not exist. Server may need initialising", FitnessAssemblyName)); } Log.Info(string.Format("GAF Evaluation Server Listening on {0}:{1}.", endPoint.Address, endPoint.Port)); SocketListener.OnPacketReceived += listener_OnPacketReceived; SocketListener.StartListening(endPoint); }
/// <summary> /// Initializes a new instance of the <see cref="GAF.Net.EvaluationClient"/> class. /// </summary> /// <param name="endPoints">End points.</param> public EvaluationClient(List <IPEndPoint> endPoints, string fitnessAssemblyName) { if (endPoints == null) { throw new ArgumentNullException(nameof(endPoints), "The parameter is null."); } if (string.IsNullOrWhiteSpace(fitnessAssemblyName)) { throw new ArgumentException("The specified fitness assembly name is null or empty.", nameof(fitnessAssemblyName)); } _socketPool = new SocketPool(endPoints); _pcQueue = new GAF.Network.Threading.ProducerConsumerQueue(endPoints.Count); _fitnessAssemblyName = fitnessAssemblyName; _fitnessAssembly = new FitnessAssembly(fitnessAssemblyName); InitialiseServers(true); }
private void listener_OnPacketReceived(object sender, PacketEventArgs e) { try { Log.Debug(string.Format("Packet Received, PacketId:{0} ObjectId:{1} Data Bytes:{2}", e.Packet.Header.PacketId, e.Packet.Header.ObjectId, e.Packet.Data.Length)); switch ((PacketId)e.Packet.Header.PacketId) { case PacketId.Data: { if (e.Packet.Header.DataLength > 0) { //deserialise to get the genes, create a new chromosome from these //this saves having to send the whole chromosome var genes = Binary.DeSerialize <List <Gene> > (e.Packet.Data, _fitnessAssembly.KnownTypes); var chromosome = new Chromosome(genes); e.Result = chromosome.Evaluate(_fitnessAssembly.FitnessFunction); if (OnEvaluationComplete != null) { var eventArgs = new RemoteEvaluationEventArgs(chromosome); this.OnEvaluationComplete(this, eventArgs); } } break; } case PacketId.Init: { Log.Info("Initialisation initiated."); if (e.Packet.Header.DataLength > 0) { Log.Info("Writing Fitness Assembly to filesystem."); File.WriteAllBytes(_fitnessAssemblyName, e.Packet.Data); //check for fitness file if (File.Exists(_fitnessAssemblyName)) { Log.Info("Loading the Fitness Assembly."); _fitnessAssembly = new FitnessAssembly(_fitnessAssemblyName); } else { Log.Error("Fitness Assembly not accesible or missing."); } } break; } case PacketId.Status: { var result = 0x0; //check for fitness file if (File.Exists(_fitnessAssemblyName)) { result = result | (int)ServerStatusFlags.Initialised; } if (_serverDefinedFitness) { result = result | (int)ServerStatusFlags.ServerDefinedFitness; } e.Result = (double)result; break; } } } catch (Exception ex) { while (ex.InnerException != null) { ex = ex.InnerException; } Log.Error(ex); } }