/// <inheritdoc /> public void CalculateScore(IGenome g) { // try rewrite Rules.Rewrite(g); // decode IMLMethod phenotype = CODEC.Decode(g); double score; // deal with invalid decode if (phenotype == null) { score = BestComparer.ShouldMinimize ? Double.PositiveInfinity : Double.NegativeInfinity; } else { var context = phenotype as IMLContext; if (context != null) { context.ClearContext(); } score = ScoreFunction.CalculateScore(phenotype); } // now set the scores g.Score = score; g.AdjustedScore = score; }
/// <summary> /// See if the best genome has changed, and decode a new best network, if /// needed. /// </summary> private void UpdateBestNetwork() { if (BestGenome != _cachedBestGenome) { _cachedBestGenome = BestGenome; _bestNetwork = (NEATNetwork)CODEC.Decode(BestGenome); } }
private void _ReadCallback(IAsyncResult ar) { var myData = Thread.GetNamedDataSlot("device"); try { _read_bytes = _socket.EndReceiveFrom(ar, ref _remote_address); if (Hacker.UDP.DisableSocketRead) { STrace.Debug(GetType().FullName, String.Format("udp reactor, hackeado, tamaño={0}", _read_bytes)); _socket.BeginReceiveFrom(_read_buffer, 0, _buffersize, 0, ref _remote_address, _ReadCallback, _socket); Thread.SetData(myData, null); return; } try { var ret = Codes.DecodeErrors.NoError; var instance_buffer = new byte[get_read_bytes()]; Array.Copy(get_buffer(), instance_buffer, get_read_bytes()); PDU pdu = null; try { pdu = CODEC.Decode(instance_buffer, ref ret); } catch (Exception) { STrace.Debug(GetType().FullName, "PDU malformada!"); } if (pdu == null || ret != Codes.DecodeErrors.NoError) { STrace.Debug(GetType().FullName, String.Format("Error la PDU no se pudo decodificar reason={0}", ret)); Thread.SetData(myData, null); _socket.BeginReceiveFrom(_read_buffer, 0, _buffersize, 0, ref _remote_address, _ReadCallback, _socket); return; } pdu.Transporte = this; pdu.Destino = new Destino { UDP = (_remote_address as IPEndPoint) }; var d = Devices.I().FindById(pdu.IdDispositivo); Thread.SetData(myData, d); if (d != null && CODEC.AllowOfflineMessages) { d.Transporte = this; d.Touch(pdu.Destino); } else if (d != null && d.State == DeviceTypes.States.ONLINE) { STrace.Debug(GetType().FullName, String.Format("DEVICE[{0}]: TOUCH por mensaje CH={1}", d.LogId, pdu.CH)); d.Transporte = this; d.Touch(pdu.Destino); } else if (d != null) { if ((pdu.CH < 0x80) && ((Codes.HighCommand)pdu.CH) != Codes.HighCommand.LoginRequest) { if (CODEC.DontReplayWhenOffline) { // ignoramos todos los mensajes excepto LRQ cuando esta offline. STrace.Debug(GetType().FullName, String.Format("DEVICE[{0}]: Ignorando mensaje CH={1} por estar offline.", d.LogId, pdu.CH)); Thread.SetData(myData, null); _socket.BeginReceiveFrom(_read_buffer, 0, _buffersize, 0, ref _remote_address, _ReadCallback, _socket); return; } if (CODEC.SupportsLoginRequired) { // si el dispositivo no esta registrado, entonces enviamos // mensaje de registracion requerida. STrace.Debug(GetType().FullName, String.Format("DEVICE[{0}]: Enviando Login Requerido.", d.LogId)); var rta = new PDU { IdDispositivo = pdu.IdDispositivo, Seq = pdu.Seq, Options = pdu.Options, Destino = pdu.Destino, CH = ((byte)Codes.HighCommand.LoginRequerido), CL = 0x00 }; Send(rta); } Thread.SetData(myData, null); _socket.BeginReceiveFrom(_read_buffer, 0, _buffersize, 0, ref _remote_address, _ReadCallback, _socket); return; } } var devlog = (d != null ? d.LogId : "?"); var t = ObtenerTransaccion(pdu.IdTransaccion); if (t == null) { if (pdu.CH < 0x80) { var mrs = new MRS(pdu, this, TransactionUser); NuevaTransaccion(mrs, pdu); mrs.Start(); } else { STrace.Debug(GetType().FullName, String.Format("DEVICE[{0}]: RTA HUERFANA CH={1}", devlog, pdu.CH)); } } else { STrace.Debug(GetType().FullName, String.Format("DEVICE[{0}]: Recivo Retransmision CH={1}", devlog, pdu.CH)); t.RecibePDU(pdu); } } catch (ObjectDisposedException e) { STrace.Debug(GetType().FullName, e.Message); } catch (Exception e) { STrace.Exception(GetType().FullName, e); } Thread.SetData(myData, null); _socket.BeginReceiveFrom(_read_buffer, 0, _buffersize, 0, ref _remote_address, _ReadCallback, _socket); } catch (Exception e) { if (e.Message != "An existing connection was forcibly closed by the remote host") { if (e is ObjectDisposedException) { STrace.Debug(GetType().FullName, e.Message); } else { STrace.Exception(GetType().FullName, e); } } Thread.SetData(myData, null); _socket.BeginReceiveFrom(_read_buffer, 0, _buffersize, 0, ref _remote_address, _ReadCallback, _socket); } }
public void Receive(byte[] source_buffer, int size, XBeeAddress addr) { try { var ret = Codes.DecodeErrors.NoError; var instance_buffer = new byte[size]; Array.Copy(source_buffer, instance_buffer, size); var pdu = CODEC.Decode(instance_buffer, ref ret); if (pdu == null || ret != Codes.DecodeErrors.NoError) { STrace.Debug(GetType().FullName, String.Format("XBEE: Error la PDU no se pudo decodificar reason={0}", ret)); return; } pdu.Transporte = this; pdu.Destino = new Destino { XBee = addr }; var d = Devices.I().FindById(pdu.IdDispositivo); var devlog = (d != null ? d.LogId : "?"); var myData = Thread.GetNamedDataSlot("device"); Thread.SetData(myData, d); STrace.Debug(GetType().FullName, pdu.Trace("")); if (d != null) { d.Trace(); } if (d != null && d.State == DeviceTypes.States.ONNET) { d.Transporte = this; d.Touch(pdu.Destino); } else if (d != null) { if ((Codes.HighCommand)pdu.CH != Codes.HighCommand.LoginRequest) { // si el dispositivo no esta registrado, entonces enviamos // mensaje de registracion requerida. STrace.Debug(GetType().FullName, String.Format("DEVICE[{0}]/XBEE: Enviando Login requerido", devlog)); var rta = new PDU { IdDispositivo = pdu.IdDispositivo, Seq = pdu.Seq, Options = pdu.Options, Destino = pdu.Destino, CH = ((byte)Codes.HighCommand.LoginRequerido), CL = 0x00 }; Send(rta); Thread.SetData(myData, null); return; } } var t = ObtenerTransaccion(pdu.IdTransaccion); if (t == null) { if (pdu.CH < 0x80) { STrace.Debug(GetType().FullName, String.Format("DEVICE[{0}]/XBEE: Nueva Transaccion Entrante CH={1} Seq={2}", devlog, pdu.CH, pdu.Seq)); var mrs = new MRS(pdu, this, TransactionUser); if (d != null && d.HackBugXBEEv99) { d.Transporte = this; } NuevaTransaccion(mrs, pdu); mrs.Start(); } else { STrace.Debug(GetType().FullName, String.Format("DEVICE[{0}]/XBEE: RTA HUERFANA CH={1}", devlog, pdu.CH)); } } else { STrace.Debug(GetType().FullName, String.Format("DEVICE[{0}]/XBEE: Recivo Retransmision CH={1} Seq={2}", devlog, pdu.CH, pdu.Seq)); t.RecibePDU(pdu); } } catch (Exception e) { STrace.Exception(GetType().FullName, e); } }