Free cookie consent management tool by TermsFeed Policy Generator

source: branches/thasling/DistributedGA/DistributedGA.Hive/P2PMigrationAnalyzer.cs @ 14013

Last change on this file since 14013 was 14013, checked in by gkronber, 8 years ago

#2615: fixed sorting of individuals and re-added ClearState method

File size: 12.1 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using DistributedGA.Core.Implementation;
27using DistributedGA.Core.Interface;
28using DistributedGA.Hive;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31using HeuristicLab.Data;
32using HeuristicLab.Operators;
33using HeuristicLab.Parameters;
34using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
35
36namespace HeuristicLab.Optimization.Operators {
37  [Item("P2PMigrationAnalyzer", "Migrates individuals using a P2P network.")]
38  [StorableClass]
39  public class P2PMigrationAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
40    // state: messagehandler
41    private IMessageHandler h;
42
43    public bool EnabledByDefault { get { return false; } }
44
45    public ILookupParameter<BoolValue> MaximizationParameter {
46      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
47    }
48    // for name translation
49    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
50      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
51    }
52    public ILookupParameter<IntValue> MigrationIterationsParameter {
53      get { return (ILookupParameter<IntValue>)Parameters["MigrationIterations"]; }
54    }
55    public ILookupParameter<PercentValue> MigrationRatesParameter {
56      get { return (ILookupParameter<PercentValue>)Parameters["MigrationRate"]; }
57    }
58    public ILookupParameter<PercentValue> CommunicationRatesParameter {
59      get { return (ILookupParameter<PercentValue>)Parameters["CommunicationRate"]; }
60    }
61    public ILookupParameter<IntValue> MessageCacheCapacityParameter {
62      get { return (ILookupParameter<IntValue>)Parameters["MessageCacheCapacity"]; }
63    }
64    public ILookupParameter<IRandom> RandomParameter {
65      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
66    }
67    public IValueParameter<IntValue> MigrationIntervalParameter {
68      get { return (IValueParameter<IntValue>)Parameters["MigrationInterval"]; }
69    }
70    public IValueParameter<ILog> LogParameter {
71      get { return (IValueParameter<ILog>)Parameters["Log"]; }
72    }
73
74    public IConstrainedValueParameter<EnumValue<MigrationStrategy>> MigrationStrategySelectParameter {
75      get { return (IConstrainedValueParameter<EnumValue<MigrationStrategy>>)Parameters["MigrationStrategySelect"]; }
76    }
77
78    public IConstrainedValueParameter<EnumValue<MigrationStrategy>> MigrationStrategyReplaceParameter {
79      get { return (IConstrainedValueParameter<EnumValue<MigrationStrategy>>)Parameters["MigrationStrategyReplace"]; }
80    }
81
82    public BoolValue Maximization {
83      get { return MaximizationParameter.ActualValue; }
84    }
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"));
104      Parameters.Add(new LookupParameter<BoolValue>("Maximization"));
105      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", 1));
106      Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "", new IntValue(5)));
107      Parameters.Add(new ValueParameter<PercentValue>("MigrationRate", "", new PercentValue(0.05)));
108      Parameters.Add(new ValueParameter<PercentValue>("CommunicationRate", "", new PercentValue(0.10)));
109      Parameters.Add(new ValueParameter<IntValue>("MessageCacheCapacity", "", new IntValue(1000)));
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)));
115
116      var validValues = new ItemSet<EnumValue<MigrationStrategy>>();
117      validValues.Add(new EnumValue<MigrationStrategy>(MigrationStrategy.Best));
118      validValues.Add(new EnumValue<MigrationStrategy>(MigrationStrategy.Worst));
119      validValues.Add(new EnumValue<MigrationStrategy>(MigrationStrategy.Random));
120      Parameters.Add(new ConstrainedValueParameter<EnumValue<MigrationStrategy>>("MigrationStrategySelect", validValues, (new EnumValue<MigrationStrategy>(MigrationStrategy.Random))));
121      Parameters.Add(new ConstrainedValueParameter<EnumValue<MigrationStrategy>>("MigrationStrategyReplace", validValues, (new EnumValue<MigrationStrategy>(MigrationStrategy.Random))));
122
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    private void Init() {
136      h = new PeerNetworkMessageHandler();
137      var lanIpPrefix = ((StringValue)(Parameters["LanIpPrefix"].ActualValue)).Value;
138      var contactServerUri = ((StringValue)(Parameters["ContactServerURL"].ActualValue)).Value;
139      var problemInstance = ((StringValue)Parameters["JobGUID"].ActualValue).Value;
140      var communicationRate = ((PercentValue)Parameters["CommunicationRate"].ActualValue).Value;
141      var messageCacheCapacity = ((IntValue)Parameters["MessageCacheCapacity"].ActualValue).Value;
142      h.Init(lanIpPrefix, contactServerUri, problemInstance, (int)(100 * messageCacheCapacity), (int)(100 * communicationRate));
143      h.ExceptionOccurend += ExceptionThrown;
144    }
145
146    public override IOperation Apply() {
147      if (h == null) {
148        Init();
149      }
150
151      if (MigrationIterationsParameter.ActualValue == null) {
152        MigrationIterationsParameter.ActualValue = new IntValue(0);
153      }
154
155      if (MigrationIterations.Value % MigrationInterval.Value == 0) {
156
157        IScope scope = ExecutionContext.Scope;
158        List<IScope> emigrantsList = new List<IScope>();
159
160        //define how many migrants to send
161        var migrationRate = ((PercentValue)Parameters["MigrationRate"].ActualValue).Value;
162        int noOfEmigrants = Convert.ToInt32(scope.SubScopes.Count * migrationRate);
163        if (noOfEmigrants == 0 && scope.SubScopes.Count > 0) {
164          noOfEmigrants = 1;
165        }
166        var popQualities = QualityParameter.ActualValue;
167        var pop = new List<IScope>(scope.SubScopes);
168
169        List<IScope> sortedPop;
170        if (Maximization.Value) {
171          sortedPop = pop.Zip(popQualities, Tuple.Create).OrderByDescending(t => t.Item2).Select(t => t.Item1).ToList();
172        } else {
173          sortedPop = pop.Zip(popQualities, Tuple.Create).OrderBy(t => t.Item2).Select(t => t.Item1).ToList();
174        }
175
176        var selectedMigStratSelect = MigrationStrategySelectParameter.Value.Value;
177        var selectedMigStratReplace = MigrationStrategyReplaceParameter.Value.Value;
178
179        var rand = Random;
180        int replIdx = 0;
181        IScope emigrants = null;
182
183        for (int i = 0; i < noOfEmigrants; i++) {
184          //select emigrant depending on strategy
185          switch (selectedMigStratSelect) {
186            case MigrationStrategy.Best:
187              emigrants = sortedPop[i];
188              emigrantsList.Add(emigrants);
189              break;
190
191            case MigrationStrategy.Random:
192              replIdx = rand.Next(sortedPop.Count);
193              emigrants = sortedPop[replIdx];
194              emigrantsList.Add(emigrants);
195              break;
196
197            case MigrationStrategy.Worst:
198              emigrants = sortedPop[scope.SubScopes.Count - i - 1];
199              emigrantsList.Add(emigrants);
200              break;
201          }
202        }
203
204        {
205          // send
206          for (int ei = 0; ei < emigrantsList.Count; ei++) {
207            using (var stream = new MemoryStream()) {
208              byte[] message;
209              var emigrantScope = emigrantsList[ei];
210
211              var msgScope = new Scope();
212              var cloner = new Cloner();
213              foreach (var variable in emigrantScope.Variables) {
214                msgScope.Variables.Add((IVariable)variable.Clone(cloner));
215              }
216              HeuristicLab.Persistence.Default.Xml.XmlGenerator.Serialize(msgScope, stream);
217              message = stream.GetBuffer();
218              h.PublishDataToNetwork(message);
219            }
220          }
221        }
222
223
224        {
225          // recieve
226          var message = h.GetDataFromNetwork();
227         
228          // remove individuals from population to make place for immigrants
229          for (int i = 0; i < message.Count; i++) {
230            switch (selectedMigStratReplace) {
231              case MigrationStrategy.Best:
232                scope.SubScopes.Remove(sortedPop[0]);
233                sortedPop.RemoveAt(0);
234                break;
235
236              case MigrationStrategy.Random:
237                replIdx = rand.Next(sortedPop.Count);
238                scope.SubScopes.Remove(sortedPop[replIdx]);
239                sortedPop.RemoveAt(replIdx);
240                break;
241
242              case MigrationStrategy.Worst:
243                //replace random
244                scope.SubScopes.Remove(sortedPop[sortedPop.Count - 1]);
245                sortedPop.RemoveAt(sortedPop.Count - 1);
246                break;
247            }
248          }
249
250          //insert individual sorted in population
251          var qualities = QualityParameter.ActualValue;
252          var qualityTranslatedName = QualityParameter.TranslatedName;
253          foreach (var msg in message) {
254            using (var stream = new MemoryStream(msg.Value)) {
255              var immigrantScope = HeuristicLab.Persistence.Default.Xml.XmlParser.Deserialize<IScope>(stream);
256
257              var qImmigrant = ((DoubleValue)immigrantScope.Variables[qualityTranslatedName].Value).Value;
258              var insertPos = scope.SubScopes.Count;
259              var maximization = Maximization.Value;
260              for (int i = 0; i < qualities.Length; i++) {
261                var qi = qualities[i].Value;
262                if ((maximization && qi < qImmigrant) || (!maximization && qi > qImmigrant)) {
263                  insertPos = i;
264                  break;
265                }
266              }
267
268              scope.SubScopes.Insert(insertPos, immigrantScope);
269
270              var log = LogParameter.Value;
271              double quality = qImmigrant;
272              log.LogMessage(string.Format("Recieved individual with quality {0} from peer {1}:{2} ; Job: {3}",
273                                            quality, msg.Key.IpAddress, msg.Key.Port, msg.Key.ProblemInstance));
274            }
275          }
276        }
277      }
278
279      MigrationIterations.Value++;
280      return base.Apply();
281    }
282
283    private void ExceptionThrown(object sender, Exception e) {
284      var log = LogParameter.Value;
285      log.LogMessage(e.Message);
286    }
287
288  }
289}
Note: See TracBrowser for help on using the repository browser.