[13905] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.IO;
|
---|
| 25 | using System.ServiceModel.Configuration;
|
---|
| 26 | using System.Threading;
|
---|
| 27 | using DistributedGA.Core.Domain;
|
---|
| 28 | using DistributedGA.Core.Implementation;
|
---|
| 29 | using DistributedGA.Core.Interface;
|
---|
| 30 | using HeuristicLab.Clients.Hive;
|
---|
| 31 | using HeuristicLab.Common;
|
---|
| 32 | using HeuristicLab.Core;
|
---|
| 33 | using HeuristicLab.Data;
|
---|
| 34 | using HeuristicLab.Operators;
|
---|
| 35 | using HeuristicLab.Parameters;
|
---|
| 36 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 37 |
|
---|
| 38 | namespace HeuristicLab.Optimization.Operators {
|
---|
| 39 | [Item("P2PMigrationAnalyzer", "Migrates individuals using a P2P network.")]
|
---|
| 40 | [StorableClass]
|
---|
| 41 | public class P2PMigrationAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
| 42 | // state: messagehandler
|
---|
| 43 | private IMessageHandler h;
|
---|
| 44 |
|
---|
| 45 | public ILookupParameter<IntValue> MigrationIterationsParameter {
|
---|
| 46 | get { return (ILookupParameter<IntValue>)Parameters["MigrationIterations"]; }
|
---|
| 47 | }
|
---|
| 48 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 49 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 50 | }
|
---|
| 51 | public IValueParameter<IntValue> MigrationIntervalParameter {
|
---|
| 52 | get { return (IValueParameter<IntValue>)Parameters["MigrationInterval"]; }
|
---|
| 53 | }
|
---|
| 54 | public IValueParameter<ILog> LogParameter {
|
---|
| 55 | get { return (IValueParameter<ILog>)Parameters["Log"]; }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public IntValue MigrationIterations {
|
---|
| 59 | get { return MigrationIterationsParameter.ActualValue; }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public IntValue MigrationInterval {
|
---|
| 63 | get { return MigrationIntervalParameter.Value; }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | public IRandom Random {
|
---|
| 67 | get { return RandomParameter.ActualValue; }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | [StorableConstructor]
|
---|
| 71 | protected P2PMigrationAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
| 72 | protected P2PMigrationAnalyzer(P2PMigrationAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
| 73 |
|
---|
| 74 | public P2PMigrationAnalyzer()
|
---|
| 75 | : base() {
|
---|
| 76 | Parameters.Add(new LookupParameter<IntValue>("MigrationIterations"));
|
---|
| 77 | Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "", new IntValue(1)));
|
---|
| 78 | Parameters.Add(new ValueParameter<StringValue>("LanIpPrefix", "", new StringValue("10.")));
|
---|
| 79 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator"));
|
---|
| 80 | Parameters.Add(new ValueParameter<StringValue>("ContactServerURL", "", new StringValue("net.tcp://10.42.1.150:9090/DistributedGA.ContactServer/ContactService")));
|
---|
| 81 | Parameters.Add(new ValueParameter<StringValue>("JobGUID", "", new StringValue(Guid.NewGuid().ToString())));
|
---|
| 82 | Parameters.Add(new ValueParameter<ILog>("Log", "The log", new Log(1000)));
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 86 | return new P2PMigrationAnalyzer(this, cloner);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | public override void ClearState() {
|
---|
| 90 | base.ClearState();
|
---|
| 91 | h.Dispose();
|
---|
| 92 | h = null;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | public override void InitializeState() {
|
---|
| 96 | base.InitializeState();
|
---|
| 97 | // init P2P
|
---|
| 98 | h = new PeerNetworkMessageHandler();
|
---|
| 99 | var lanIpPrefix = ((StringValue)(Parameters["LanIpPrefix"].ActualValue)).Value;
|
---|
| 100 | var contactServerUri = ((StringValue)(Parameters["ContactServerURL"].ActualValue)).Value;
|
---|
| 101 | h.Init(lanIpPrefix, contactServerUri);
|
---|
[13943] | 102 | var peer = h.GetPeerInfo();
|
---|
| 103 | peer.ProblemInstance = ((StringValue)Parameters["JobGUID"].ActualValue).Value;
|
---|
[13905] | 104 | }
|
---|
| 105 |
|
---|
| 106 | public override IOperation Apply() {
|
---|
| 107 | if (MigrationIterationsParameter.ActualValue == null) {
|
---|
| 108 | MigrationIterationsParameter.ActualValue = new IntValue(0);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | if (MigrationIterations.Value % MigrationInterval.Value == 0) {
|
---|
| 112 |
|
---|
| 113 | IScope scope = ExecutionContext.Scope;
|
---|
| 114 | List<IScope> emigrantsList = new List<IScope>();
|
---|
| 115 |
|
---|
| 116 | // select best as emigrant
|
---|
| 117 | IScope emigrants = scope.SubScopes[1];
|
---|
| 118 | emigrantsList.Add(emigrants);
|
---|
| 119 |
|
---|
| 120 | {
|
---|
| 121 | // send
|
---|
| 122 | var message = new byte[emigrantsList.Count][];
|
---|
| 123 | for (int ei = 0; ei < emigrantsList.Count; ei++) {
|
---|
| 124 | using (var stream = new MemoryStream()) {
|
---|
| 125 | var emigrantScope = emigrantsList[ei];
|
---|
| 126 | emigrantScope.ClearParentScopes();
|
---|
| 127 | HeuristicLab.Persistence.Default.Xml.XmlGenerator.Serialize(emigrantScope, stream);
|
---|
| 128 | message[ei] = stream.GetBuffer();
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | h.PublishDataToNetwork(message);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 | {
|
---|
| 136 | // recieve
|
---|
| 137 | var message = h.GetDataFromNetwork();
|
---|
| 138 | for (int ei = 0; ei < message.Length; ei++) {
|
---|
| 139 | using (var stream = new MemoryStream(message[ei])) {
|
---|
| 140 | var immigrantScope = HeuristicLab.Persistence.Default.Xml.XmlParser.Deserialize<IScope>(stream);
|
---|
| 141 |
|
---|
| 142 | // replace random individual in current population
|
---|
| 143 | var rand = Random;
|
---|
| 144 | var replIdx = rand.Next(scope.SubScopes.Count);
|
---|
| 145 |
|
---|
| 146 | scope.SubScopes.RemoveAt(replIdx);
|
---|
| 147 | scope.SubScopes.Insert(replIdx, immigrantScope);
|
---|
| 148 | var log = LogParameter.Value;
|
---|
| 149 | double quality = 0.0;
|
---|
| 150 | if (immigrantScope.Variables.ContainsKey("Quality")) quality = ((DoubleValue)immigrantScope.Variables["Quality"].Value).Value;
|
---|
| 151 | log.LogMessage(string.Format("Recieved individual with quality {0}", quality));
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | MigrationIterations.Value++;
|
---|
| 158 | return base.Apply();
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | public bool EnabledByDefault { get { return false; } }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|