#region License Information /* HeuristicLab * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.IO; using System.ServiceModel.Configuration; using System.Threading; using DistributedGA.Core.Domain; using DistributedGA.Core.Implementation; using DistributedGA.Core.Interface; using HeuristicLab.Clients.Hive; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Optimization.Operators { [Item("P2PMigrationAnalyzer", "Migrates individuals using a P2P network.")] [StorableClass] public class P2PMigrationAnalyzer : SingleSuccessorOperator, IAnalyzer { // state: messagehandler private IMessageHandler h; public ILookupParameter MigrationIterationsParameter { get { return (ILookupParameter)Parameters["MigrationIterations"]; } } public ILookupParameter RandomParameter { get { return (ILookupParameter)Parameters["Random"]; } } public IValueParameter MigrationIntervalParameter { get { return (IValueParameter)Parameters["MigrationInterval"]; } } public IValueParameter LogParameter { get { return (IValueParameter)Parameters["Log"]; } } public IntValue MigrationIterations { get { return MigrationIterationsParameter.ActualValue; } } public IntValue MigrationInterval { get { return MigrationIntervalParameter.Value; } } public IRandom Random { get { return RandomParameter.ActualValue; } } [StorableConstructor] protected P2PMigrationAnalyzer(bool deserializing) : base(deserializing) { } protected P2PMigrationAnalyzer(P2PMigrationAnalyzer original, Cloner cloner) : base(original, cloner) { } public P2PMigrationAnalyzer() : base() { Parameters.Add(new LookupParameter("MigrationIterations")); Parameters.Add(new ValueParameter("MigrationInterval", "", new IntValue(1))); Parameters.Add(new ValueParameter("LanIpPrefix", "", new StringValue("10."))); Parameters.Add(new LookupParameter("Random", "The random number generator")); Parameters.Add(new ValueParameter("ContactServerURL", "", new StringValue("net.tcp://10.42.1.150:9090/DistributedGA.ContactServer/ContactService"))); Parameters.Add(new ValueParameter("JobGUID", "", new StringValue(Guid.NewGuid().ToString()))); Parameters.Add(new ValueParameter("Log", "The log", new Log(1000))); } public override IDeepCloneable Clone(Cloner cloner) { return new P2PMigrationAnalyzer(this, cloner); } public override void ClearState() { base.ClearState(); h.Dispose(); h = null; } public override void InitializeState() { base.InitializeState(); // init P2P h = new PeerNetworkMessageHandler(); var lanIpPrefix = ((StringValue)(Parameters["LanIpPrefix"].ActualValue)).Value; var contactServerUri = ((StringValue)(Parameters["ContactServerURL"].ActualValue)).Value; h.Init(lanIpPrefix, contactServerUri); var peer = h.GetPeerInfo(); peer.ProblemInstance = ((StringValue)Parameters["JobGUID"].ActualValue).Value; } public override IOperation Apply() { if (MigrationIterationsParameter.ActualValue == null) { MigrationIterationsParameter.ActualValue = new IntValue(0); } if (MigrationIterations.Value % MigrationInterval.Value == 0) { IScope scope = ExecutionContext.Scope; List emigrantsList = new List(); // select best as emigrant IScope emigrants = scope.SubScopes[1]; emigrantsList.Add(emigrants); { // send var message = new byte[emigrantsList.Count][]; for (int ei = 0; ei < emigrantsList.Count; ei++) { using (var stream = new MemoryStream()) { var emigrantScope = emigrantsList[ei]; emigrantScope.ClearParentScopes(); HeuristicLab.Persistence.Default.Xml.XmlGenerator.Serialize(emigrantScope, stream); message[ei] = stream.GetBuffer(); } } h.PublishDataToNetwork(message); } { // recieve var message = h.GetDataFromNetwork(); for (int ei = 0; ei < message.Length; ei++) { using (var stream = new MemoryStream(message[ei])) { var immigrantScope = HeuristicLab.Persistence.Default.Xml.XmlParser.Deserialize(stream); // replace random individual in current population var rand = Random; var replIdx = rand.Next(scope.SubScopes.Count); scope.SubScopes.RemoveAt(replIdx); scope.SubScopes.Insert(replIdx, immigrantScope); var log = LogParameter.Value; double quality = 0.0; if (immigrantScope.Variables.ContainsKey("Quality")) quality = ((DoubleValue)immigrantScope.Variables["Quality"].Value).Value; log.LogMessage(string.Format("Recieved individual with quality {0}", quality)); } } } } MigrationIterations.Value++; return base.Apply(); } public bool EnabledByDefault { get { return false; } } } }