[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;
|
---|
[13960] | 24 | using System.Data.SqlTypes;
|
---|
[13905] | 25 | using System.IO;
|
---|
| 26 | using System.ServiceModel.Configuration;
|
---|
| 27 | using System.Threading;
|
---|
| 28 | using DistributedGA.Core.Domain;
|
---|
| 29 | using DistributedGA.Core.Implementation;
|
---|
| 30 | using DistributedGA.Core.Interface;
|
---|
[13957] | 31 | using DistributedGA.Hive;
|
---|
[13905] | 32 | using HeuristicLab.Clients.Hive;
|
---|
| 33 | using HeuristicLab.Common;
|
---|
| 34 | using HeuristicLab.Core;
|
---|
| 35 | using HeuristicLab.Data;
|
---|
| 36 | using HeuristicLab.Operators;
|
---|
| 37 | using HeuristicLab.Parameters;
|
---|
| 38 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 39 |
|
---|
| 40 | namespace HeuristicLab.Optimization.Operators {
|
---|
| 41 | [Item("P2PMigrationAnalyzer", "Migrates individuals using a P2P network.")]
|
---|
| 42 | [StorableClass]
|
---|
[13969] | 43 | public class P2PMigrationAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
|
---|
[13905] | 44 | // state: messagehandler
|
---|
| 45 | private IMessageHandler h;
|
---|
| 46 |
|
---|
[13969] | 47 | public bool EnabledByDefault { get { return false; } }
|
---|
| 48 |
|
---|
[13960] | 49 | public ILookupParameter<BoolValue> MaximizationParameter {
|
---|
| 50 | get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 51 | }
|
---|
| 52 | // for name translation
|
---|
| 53 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 54 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 55 | }
|
---|
[13905] | 56 | public ILookupParameter<IntValue> MigrationIterationsParameter {
|
---|
| 57 | get { return (ILookupParameter<IntValue>)Parameters["MigrationIterations"]; }
|
---|
| 58 | }
|
---|
[13960] | 59 | public ILookupParameter<PercentValue> MigrationRatesParameter {
|
---|
| 60 | get { return (ILookupParameter<PercentValue>)Parameters["MigrationRate"]; }
|
---|
[13957] | 61 | }
|
---|
[13960] | 62 | public ILookupParameter<PercentValue> CommunicationRatesParameter {
|
---|
| 63 | get { return (ILookupParameter<PercentValue>)Parameters["CommunicationRate"]; }
|
---|
[13957] | 64 | }
|
---|
[13959] | 65 | public ILookupParameter<IntValue> MessageCacheCapacityParameter {
|
---|
[13957] | 66 | get { return (ILookupParameter<IntValue>)Parameters["MessageCacheCapacity"]; }
|
---|
| 67 | }
|
---|
[13905] | 68 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 69 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 70 | }
|
---|
| 71 | public IValueParameter<IntValue> MigrationIntervalParameter {
|
---|
| 72 | get { return (IValueParameter<IntValue>)Parameters["MigrationInterval"]; }
|
---|
| 73 | }
|
---|
| 74 | public IValueParameter<ILog> LogParameter {
|
---|
| 75 | get { return (IValueParameter<ILog>)Parameters["Log"]; }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[13960] | 78 | public IConstrainedValueParameter<EnumValue<MigrationStrategy>> MigrationStrategyParameter {
|
---|
| 79 | get { return (IConstrainedValueParameter<EnumValue<MigrationStrategy>>)Parameters["MigrationStrategy"]; }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | public BoolValue Maximization {
|
---|
| 83 | get { return MaximizationParameter.ActualValue; }
|
---|
| 84 | }
|
---|
[13905] | 85 | public IntValue MigrationIterations {
|
---|
| 86 | get { return MigrationIterationsParameter.ActualValue; }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | public IntValue MigrationInterval {
|
---|
| 90 | get { return MigrationIntervalParameter.Value; }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | public IRandom Random {
|
---|
| 94 | get { return RandomParameter.ActualValue; }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | [StorableConstructor]
|
---|
| 98 | protected P2PMigrationAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
| 99 | protected P2PMigrationAnalyzer(P2PMigrationAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
| 100 |
|
---|
| 101 | public P2PMigrationAnalyzer()
|
---|
| 102 | : base() {
|
---|
| 103 | Parameters.Add(new LookupParameter<IntValue>("MigrationIterations"));
|
---|
[13960] | 104 | Parameters.Add(new LookupParameter<BoolValue>("Maximization"));
|
---|
| 105 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", 1));
|
---|
[13905] | 106 | Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "", new IntValue(1)));
|
---|
[13960] | 107 | Parameters.Add(new ValueParameter<PercentValue>("MigrationRate", "", new PercentValue(0.05)));
|
---|
| 108 | Parameters.Add(new ValueParameter<PercentValue>("CommunicationRate", "", new PercentValue(0.10)));
|
---|
[13957] | 109 | Parameters.Add(new ValueParameter<IntValue>("MessageCacheCapacity", "", new IntValue(100)));
|
---|
[13905] | 110 | Parameters.Add(new ValueParameter<StringValue>("LanIpPrefix", "", new StringValue("10.")));
|
---|
| 111 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator"));
|
---|
| 112 | Parameters.Add(new ValueParameter<StringValue>("ContactServerURL", "", new StringValue("net.tcp://10.42.1.150:9090/DistributedGA.ContactServer/ContactService")));
|
---|
| 113 | Parameters.Add(new ValueParameter<StringValue>("JobGUID", "", new StringValue(Guid.NewGuid().ToString())));
|
---|
| 114 | Parameters.Add(new ValueParameter<ILog>("Log", "The log", new Log(1000)));
|
---|
[13960] | 115 |
|
---|
| 116 | var validValues = new ItemSet<EnumValue<MigrationStrategy>>();
|
---|
| 117 | validValues.Add(new EnumValue<MigrationStrategy>(MigrationStrategy.TakeBestReplaceBad));
|
---|
| 118 | validValues.Add(new EnumValue<MigrationStrategy>(MigrationStrategy.TakeBestReplaceRandom));
|
---|
| 119 | validValues.Add(new EnumValue<MigrationStrategy>(MigrationStrategy.TakeRandomReplaceBad));
|
---|
| 120 | validValues.Add(new EnumValue<MigrationStrategy>(MigrationStrategy.TakeRandomReplaceRandom));
|
---|
| 121 |
|
---|
[13965] | 122 | Parameters.Add(new ConstrainedValueParameter<EnumValue<MigrationStrategy>>("MigrationStrategy", validValues, (new EnumValue<MigrationStrategy>(MigrationStrategy.TakeBestReplaceBad))));
|
---|
[13905] | 123 | }
|
---|
| 124 |
|
---|
| 125 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 126 | return new P2PMigrationAnalyzer(this, cloner);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | public override void ClearState() {
|
---|
| 130 | base.ClearState();
|
---|
| 131 | h.Dispose();
|
---|
| 132 | h = null;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | public override void InitializeState() {
|
---|
| 136 | base.InitializeState();
|
---|
| 137 | // init P2P
|
---|
[13969] | 138 | Init();
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | private void Init() {
|
---|
[13905] | 142 | h = new PeerNetworkMessageHandler();
|
---|
| 143 | var lanIpPrefix = ((StringValue)(Parameters["LanIpPrefix"].ActualValue)).Value;
|
---|
| 144 | var contactServerUri = ((StringValue)(Parameters["ContactServerURL"].ActualValue)).Value;
|
---|
[13956] | 145 | var problemInstance = ((StringValue)Parameters["JobGUID"].ActualValue).Value;
|
---|
[13960] | 146 | var communicationRate = ((PercentValue)Parameters["CommunicationRate"].ActualValue).Value;
|
---|
[13957] | 147 | var messageCacheCapacity = ((IntValue)Parameters["MessageCacheCapacity"].ActualValue).Value;
|
---|
[13960] | 148 | h.Init(lanIpPrefix, contactServerUri, problemInstance, (int)(100 * messageCacheCapacity), (int)(100 * communicationRate));
|
---|
[13969] | 149 | h.ExceptionOccurend += ExceptionThrown;
|
---|
[13905] | 150 | }
|
---|
| 151 |
|
---|
| 152 | public override IOperation Apply() {
|
---|
[13969] | 153 | if (h == null) {
|
---|
| 154 | Init();
|
---|
| 155 | }
|
---|
[13905] | 156 | if (MigrationIterationsParameter.ActualValue == null) {
|
---|
| 157 | MigrationIterationsParameter.ActualValue = new IntValue(0);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | if (MigrationIterations.Value % MigrationInterval.Value == 0) {
|
---|
| 161 |
|
---|
| 162 | IScope scope = ExecutionContext.Scope;
|
---|
| 163 | List<IScope> emigrantsList = new List<IScope>();
|
---|
| 164 |
|
---|
[13957] | 165 | //define how many migrants to send
|
---|
[13960] | 166 | var migrationRate = ((PercentValue)Parameters["MigrationRate"].ActualValue).Value;
|
---|
[13970] | 167 | int noOfEmigrants = Convert.ToInt32(scope.SubScopes.Count * migrationRate);
|
---|
| 168 | if (noOfEmigrants == 0 && scope.SubScopes.Count > 0) {
|
---|
| 169 | noOfEmigrants = 0;
|
---|
| 170 | }
|
---|
[13960] | 171 | var popQualities = QualityParameter.ActualValue;
|
---|
| 172 | var selectedMigStrat = MigrationStrategyParameter.Value.Value;
|
---|
| 173 |
|
---|
[13969] | 174 | var rand = Random;
|
---|
| 175 | int replIdx = 0;
|
---|
| 176 | IScope emigrants = null;
|
---|
[13905] | 177 |
|
---|
[13970] | 178 | for (int i = 0; i < noOfEmigrants; i++) {
|
---|
[13969] | 179 | //select emigrant depending on strategy
|
---|
| 180 | switch (selectedMigStrat) {
|
---|
| 181 | case MigrationStrategy.TakeBestReplaceBad:
|
---|
| 182 | emigrants = scope.SubScopes[i];
|
---|
| 183 | emigrantsList.Add(emigrants);
|
---|
| 184 | break;
|
---|
| 185 |
|
---|
| 186 | case MigrationStrategy.TakeBestReplaceRandom:
|
---|
| 187 | emigrants = scope.SubScopes[i];
|
---|
| 188 | emigrantsList.Add(emigrants);
|
---|
| 189 | break;
|
---|
| 190 |
|
---|
| 191 | case MigrationStrategy.TakeRandomReplaceBad:
|
---|
| 192 | replIdx = rand.Next(scope.SubScopes.Count);
|
---|
| 193 | emigrants = scope.SubScopes[replIdx];
|
---|
| 194 | emigrantsList.Add(emigrants);
|
---|
| 195 | break;
|
---|
| 196 |
|
---|
| 197 | case MigrationStrategy.TakeRandomReplaceRandom:
|
---|
| 198 | replIdx = rand.Next(scope.SubScopes.Count);
|
---|
| 199 | emigrants = scope.SubScopes[replIdx];
|
---|
| 200 | emigrantsList.Add(emigrants);
|
---|
| 201 | break;
|
---|
| 202 |
|
---|
| 203 | default:
|
---|
| 204 | break;
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[13905] | 208 | {
|
---|
| 209 | // send
|
---|
| 210 | var message = new byte[emigrantsList.Count][];
|
---|
| 211 | for (int ei = 0; ei < emigrantsList.Count; ei++) {
|
---|
| 212 | using (var stream = new MemoryStream()) {
|
---|
| 213 | var emigrantScope = emigrantsList[ei];
|
---|
[13969] | 214 |
|
---|
[13960] | 215 | var msgScope = new Scope();
|
---|
| 216 | var cloner = new Cloner();
|
---|
| 217 | foreach (var variable in emigrantScope.Variables) {
|
---|
| 218 | msgScope.Variables.Add((IVariable)variable.Clone(cloner));
|
---|
| 219 | }
|
---|
| 220 | HeuristicLab.Persistence.Default.Xml.XmlGenerator.Serialize(msgScope, stream);
|
---|
[13905] | 221 | message[ei] = stream.GetBuffer();
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 | h.PublishDataToNetwork(message);
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 |
|
---|
| 228 | {
|
---|
| 229 | // recieve
|
---|
| 230 | var message = h.GetDataFromNetwork();
|
---|
| 231 | for (int ei = 0; ei < message.Length; ei++) {
|
---|
| 232 | using (var stream = new MemoryStream(message[ei])) {
|
---|
| 233 | var immigrantScope = HeuristicLab.Persistence.Default.Xml.XmlParser.Deserialize<IScope>(stream);
|
---|
| 234 |
|
---|
[13969] | 235 | // replace individual in current population
|
---|
| 236 | switch (selectedMigStrat) {
|
---|
| 237 | case MigrationStrategy.TakeBestReplaceBad:
|
---|
| 238 | scope.SubScopes.RemoveAt(0);
|
---|
| 239 | break;
|
---|
[13905] | 240 |
|
---|
[13969] | 241 | case MigrationStrategy.TakeRandomReplaceBad:
|
---|
| 242 | scope.SubScopes.RemoveAt(0);
|
---|
| 243 | break;
|
---|
| 244 |
|
---|
| 245 | case MigrationStrategy.TakeBestReplaceRandom:
|
---|
| 246 | //replace random
|
---|
| 247 | replIdx = rand.Next(scope.SubScopes.Count);
|
---|
| 248 | scope.SubScopes.RemoveAt(replIdx);
|
---|
| 249 | break;
|
---|
| 250 |
|
---|
| 251 | case MigrationStrategy.TakeRandomReplaceRandom:
|
---|
| 252 | //replace random
|
---|
| 253 | replIdx = rand.Next(scope.SubScopes.Count);
|
---|
| 254 | scope.SubScopes.RemoveAt(replIdx);
|
---|
| 255 | break;
|
---|
| 256 |
|
---|
| 257 | default:
|
---|
| 258 | break;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | //insert individual sortet in population
|
---|
[13960] | 262 | var qualities = QualityParameter.ActualValue;
|
---|
| 263 | var qualityTranslatedName = QualityParameter.TranslatedName;
|
---|
| 264 | var qImmigrant = ((DoubleValue)immigrantScope.Variables[qualityTranslatedName].Value).Value;
|
---|
| 265 | var insertPos = scope.SubScopes.Count;
|
---|
| 266 | var maximization = Maximization.Value;
|
---|
| 267 | for (int i = 0; i < qualities.Length; i++) {
|
---|
| 268 | var qi = qualities[i].Value;
|
---|
| 269 | if ((maximization && qi < qImmigrant) || (!maximization && qi > qImmigrant)) {
|
---|
| 270 | insertPos = i;
|
---|
| 271 | break;
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | scope.SubScopes.Insert(insertPos, immigrantScope);
|
---|
| 276 |
|
---|
[13905] | 277 | var log = LogParameter.Value;
|
---|
| 278 | double quality = 0.0;
|
---|
[13960] | 279 | quality = qImmigrant;
|
---|
[13905] | 280 | log.LogMessage(string.Format("Recieved individual with quality {0}", quality));
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | MigrationIterations.Value++;
|
---|
| 287 | return base.Apply();
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[13969] | 290 | private void ExceptionThrown(object sender, Exception e) {
|
---|
| 291 | var log = LogParameter.Value;
|
---|
| 292 | log.LogMessage(e.Message);
|
---|
[13965] | 293 | }
|
---|
| 294 |
|
---|
[13905] | 295 | }
|
---|
| 296 | }
|
---|