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.Data.SqlTypes;
|
---|
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;
|
---|
31 | using DistributedGA.Hive;
|
---|
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]
|
---|
43 | public class P2PMigrationAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
|
---|
44 | // state: messagehandler
|
---|
45 | private IMessageHandler h;
|
---|
46 |
|
---|
47 | public bool EnabledByDefault { get { return false; } }
|
---|
48 |
|
---|
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 | }
|
---|
56 | public ILookupParameter<IntValue> MigrationIterationsParameter {
|
---|
57 | get { return (ILookupParameter<IntValue>)Parameters["MigrationIterations"]; }
|
---|
58 | }
|
---|
59 | public ILookupParameter<PercentValue> MigrationRatesParameter {
|
---|
60 | get { return (ILookupParameter<PercentValue>)Parameters["MigrationRate"]; }
|
---|
61 | }
|
---|
62 | public ILookupParameter<PercentValue> CommunicationRatesParameter {
|
---|
63 | get { return (ILookupParameter<PercentValue>)Parameters["CommunicationRate"]; }
|
---|
64 | }
|
---|
65 | public ILookupParameter<IntValue> MessageCacheCapacityParameter {
|
---|
66 | get { return (ILookupParameter<IntValue>)Parameters["MessageCacheCapacity"]; }
|
---|
67 | }
|
---|
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 |
|
---|
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 | }
|
---|
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(1)));
|
---|
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(100)));
|
---|
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.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 |
|
---|
122 | Parameters.Add(new ConstrainedValueParameter<EnumValue<MigrationStrategy>>("MigrationStrategy", validValues, (new EnumValue<MigrationStrategy>(MigrationStrategy.TakeBestReplaceBad))));
|
---|
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
|
---|
138 | if (h == null) {
|
---|
139 | //otherwhise old object is not disposed correctly
|
---|
140 | Init();
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | private void Init() {
|
---|
145 | h = new PeerNetworkMessageHandler();
|
---|
146 | var lanIpPrefix = ((StringValue)(Parameters["LanIpPrefix"].ActualValue)).Value;
|
---|
147 | var contactServerUri = ((StringValue)(Parameters["ContactServerURL"].ActualValue)).Value;
|
---|
148 | var problemInstance = ((StringValue)Parameters["JobGUID"].ActualValue).Value;
|
---|
149 | var communicationRate = ((PercentValue)Parameters["CommunicationRate"].ActualValue).Value;
|
---|
150 | var messageCacheCapacity = ((IntValue)Parameters["MessageCacheCapacity"].ActualValue).Value;
|
---|
151 | h.Init(lanIpPrefix, contactServerUri, problemInstance, (int)(100 * messageCacheCapacity), (int)(100 * communicationRate));
|
---|
152 | h.ExceptionOccurend += ExceptionThrown;
|
---|
153 | }
|
---|
154 |
|
---|
155 | public override IOperation Apply() {
|
---|
156 | if (h == null) {
|
---|
157 | Init();
|
---|
158 | }
|
---|
159 | if (MigrationIterationsParameter.ActualValue == null) {
|
---|
160 | MigrationIterationsParameter.ActualValue = new IntValue(0);
|
---|
161 | }
|
---|
162 |
|
---|
163 | if (MigrationIterations.Value % MigrationInterval.Value == 0) {
|
---|
164 |
|
---|
165 | IScope scope = ExecutionContext.Scope;
|
---|
166 | List<IScope> emigrantsList = new List<IScope>();
|
---|
167 |
|
---|
168 | //define how many migrants to send
|
---|
169 | var migrationRate = ((PercentValue)Parameters["MigrationRate"].ActualValue).Value;
|
---|
170 | int noOfEmigrants = Convert.ToInt32(scope.SubScopes.Count * migrationRate);
|
---|
171 | if (noOfEmigrants == 0 && scope.SubScopes.Count > 0) {
|
---|
172 | noOfEmigrants = 0;
|
---|
173 | }
|
---|
174 | var popQualities = QualityParameter.ActualValue;
|
---|
175 | var selectedMigStrat = MigrationStrategyParameter.Value.Value;
|
---|
176 |
|
---|
177 | var rand = Random;
|
---|
178 | int replIdx = 0;
|
---|
179 | IScope emigrants = null;
|
---|
180 |
|
---|
181 | for (int i = 0; i < noOfEmigrants; i++) {
|
---|
182 | //select emigrant depending on strategy
|
---|
183 | switch (selectedMigStrat) {
|
---|
184 | case MigrationStrategy.TakeBestReplaceBad:
|
---|
185 | emigrants = scope.SubScopes[i];
|
---|
186 | emigrantsList.Add(emigrants);
|
---|
187 | break;
|
---|
188 |
|
---|
189 | case MigrationStrategy.TakeBestReplaceRandom:
|
---|
190 | emigrants = scope.SubScopes[i];
|
---|
191 | emigrantsList.Add(emigrants);
|
---|
192 | break;
|
---|
193 |
|
---|
194 | case MigrationStrategy.TakeRandomReplaceBad:
|
---|
195 | replIdx = rand.Next(scope.SubScopes.Count);
|
---|
196 | emigrants = scope.SubScopes[replIdx];
|
---|
197 | emigrantsList.Add(emigrants);
|
---|
198 | break;
|
---|
199 |
|
---|
200 | case MigrationStrategy.TakeRandomReplaceRandom:
|
---|
201 | replIdx = rand.Next(scope.SubScopes.Count);
|
---|
202 | emigrants = scope.SubScopes[replIdx];
|
---|
203 | emigrantsList.Add(emigrants);
|
---|
204 | break;
|
---|
205 |
|
---|
206 | default:
|
---|
207 | break;
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | {
|
---|
212 | // send
|
---|
213 | for (int ei = 0; ei < emigrantsList.Count; ei++) {
|
---|
214 | using (var stream = new MemoryStream()) {
|
---|
215 | byte[] message;
|
---|
216 | var emigrantScope = emigrantsList[ei];
|
---|
217 |
|
---|
218 | var msgScope = new Scope();
|
---|
219 | var cloner = new Cloner();
|
---|
220 | foreach (var variable in emigrantScope.Variables) {
|
---|
221 | msgScope.Variables.Add((IVariable)variable.Clone(cloner));
|
---|
222 | }
|
---|
223 | HeuristicLab.Persistence.Default.Xml.XmlGenerator.Serialize(msgScope, stream);
|
---|
224 | message = stream.GetBuffer();
|
---|
225 | h.PublishDataToNetwork(message);
|
---|
226 |
|
---|
227 | }
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | {
|
---|
233 | // recieve
|
---|
234 | var message = h.GetDataFromNetwork();
|
---|
235 | //for (int ei = 0; ei < message.Length; ei++) {
|
---|
236 | foreach (var msg in message) {
|
---|
237 | using (var stream = new MemoryStream(msg.Value)) {
|
---|
238 | var immigrantScope = HeuristicLab.Persistence.Default.Xml.XmlParser.Deserialize<IScope>(stream);
|
---|
239 |
|
---|
240 | // replace individual in current population
|
---|
241 | switch (selectedMigStrat) {
|
---|
242 | case MigrationStrategy.TakeBestReplaceBad:
|
---|
243 | scope.SubScopes.RemoveAt(0);
|
---|
244 | break;
|
---|
245 |
|
---|
246 | case MigrationStrategy.TakeRandomReplaceBad:
|
---|
247 | scope.SubScopes.RemoveAt(0);
|
---|
248 | break;
|
---|
249 |
|
---|
250 | case MigrationStrategy.TakeBestReplaceRandom:
|
---|
251 | //replace random
|
---|
252 | replIdx = rand.Next(scope.SubScopes.Count);
|
---|
253 | scope.SubScopes.RemoveAt(replIdx);
|
---|
254 | break;
|
---|
255 |
|
---|
256 | case MigrationStrategy.TakeRandomReplaceRandom:
|
---|
257 | //replace random
|
---|
258 | replIdx = rand.Next(scope.SubScopes.Count);
|
---|
259 | scope.SubScopes.RemoveAt(replIdx);
|
---|
260 | break;
|
---|
261 |
|
---|
262 | default:
|
---|
263 | break;
|
---|
264 | }
|
---|
265 |
|
---|
266 | //insert individual sortet in population
|
---|
267 | var qualities = QualityParameter.ActualValue;
|
---|
268 | var qualityTranslatedName = QualityParameter.TranslatedName;
|
---|
269 | var qImmigrant = ((DoubleValue)immigrantScope.Variables[qualityTranslatedName].Value).Value;
|
---|
270 | var insertPos = scope.SubScopes.Count;
|
---|
271 | var maximization = Maximization.Value;
|
---|
272 | for (int i = 0; i < qualities.Length; i++) {
|
---|
273 | var qi = qualities[i].Value;
|
---|
274 | if ((maximization && qi < qImmigrant) || (!maximization && qi > qImmigrant)) {
|
---|
275 | insertPos = i;
|
---|
276 | break;
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | scope.SubScopes.Insert(insertPos, immigrantScope);
|
---|
281 |
|
---|
282 | var log = LogParameter.Value;
|
---|
283 | double quality = 0.0;
|
---|
284 | quality = qImmigrant;
|
---|
285 | log.LogMessage(string.Format("Recieved individual with quality {0} from peer {1}:{2} ; Job: {3}",
|
---|
286 | quality, msg.Key.IpAddress, msg.Key.Port, msg.Key.ProblemInstance));
|
---|
287 | }
|
---|
288 | }
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | MigrationIterations.Value++;
|
---|
293 | return base.Apply();
|
---|
294 | }
|
---|
295 |
|
---|
296 | private void ExceptionThrown(object sender, Exception e) {
|
---|
297 | var log = LogParameter.Value;
|
---|
298 | log.LogMessage(e.Message);
|
---|
299 | }
|
---|
300 |
|
---|
301 | }
|
---|
302 | }
|
---|