- Timestamp:
- 07/01/16 17:25:21 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/thasling/DistributedGA/DistributedGA.Hive/P2PMigrationAnalyzer.cs
r13966 r13969 41 41 [Item("P2PMigrationAnalyzer", "Migrates individuals using a P2P network.")] 42 42 [StorableClass] 43 public class P2PMigrationAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator , IExecutable{43 public class P2PMigrationAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator { 44 44 // state: messagehandler 45 45 private IMessageHandler h; 46 47 public bool EnabledByDefault { get { return false; } } 46 48 47 49 public ILookupParameter<BoolValue> MaximizationParameter { … … 134 136 base.InitializeState(); 135 137 // init P2P 138 Init(); 139 } 140 141 private void Init() { 136 142 h = new PeerNetworkMessageHandler(); 137 143 var lanIpPrefix = ((StringValue)(Parameters["LanIpPrefix"].ActualValue)).Value; … … 141 147 var messageCacheCapacity = ((IntValue)Parameters["MessageCacheCapacity"].ActualValue).Value; 142 148 h.Init(lanIpPrefix, contactServerUri, problemInstance, (int)(100 * messageCacheCapacity), (int)(100 * communicationRate)); 149 h.ExceptionOccurend += ExceptionThrown; 143 150 } 144 151 145 152 public override IOperation Apply() { 153 if (h == null) { 154 Init(); 155 } 146 156 if (MigrationIterationsParameter.ActualValue == null) { 147 157 MigrationIterationsParameter.ActualValue = new IntValue(0); … … 155 165 //define how many migrants to send 156 166 var migrationRate = ((PercentValue)Parameters["MigrationRate"].ActualValue).Value; 157 158 //TODO: SELECT MIGRATION STRATEGY159 // TODO: select individuals based on quality160 167 var popQualities = QualityParameter.ActualValue; 161 162 168 var selectedMigStrat = MigrationStrategyParameter.Value.Value; 163 169 164 // select best as emigrant 165 IScope emigrants = scope.SubScopes[1]; 166 emigrantsList.Add(emigrants); 170 var rand = Random; 171 int replIdx = 0; 172 IScope emigrants = null; 173 174 //determine how many emigrants to send 175 int max = Convert.ToInt32(scope.SubScopes.Count * migrationRate); 176 if (max == 0 && scope.SubScopes.Count > 0) { 177 max = 0; 178 } 179 180 for (int i = 0; i < max; i++) { 181 //select emigrant depending on strategy 182 switch (selectedMigStrat) { 183 case MigrationStrategy.TakeBestReplaceBad: 184 emigrants = scope.SubScopes[i]; 185 emigrantsList.Add(emigrants); 186 break; 187 188 case MigrationStrategy.TakeBestReplaceRandom: 189 emigrants = scope.SubScopes[i]; 190 emigrantsList.Add(emigrants); 191 break; 192 193 case MigrationStrategy.TakeRandomReplaceBad: 194 replIdx = rand.Next(scope.SubScopes.Count); 195 emigrants = scope.SubScopes[replIdx]; 196 emigrantsList.Add(emigrants); 197 break; 198 199 case MigrationStrategy.TakeRandomReplaceRandom: 200 replIdx = rand.Next(scope.SubScopes.Count); 201 emigrants = scope.SubScopes[replIdx]; 202 emigrantsList.Add(emigrants); 203 break; 204 205 default: 206 break; 207 } 208 } 209 //each subscope is one emigrand 210 //IScope emigrants = scope.SubScopes[1]; 211 //emigrantsList.Add(emigrants); 167 212 168 213 { … … 172 217 using (var stream = new MemoryStream()) { 173 218 var emigrantScope = emigrantsList[ei]; 174 emigrantScope.ClearParentScopes(); 219 175 220 var msgScope = new Scope(); 176 221 var cloner = new Cloner(); … … 178 223 msgScope.Variables.Add((IVariable)variable.Clone(cloner)); 179 224 } 225 msgScope.ClearParentScopes(); 180 226 HeuristicLab.Persistence.Default.Xml.XmlGenerator.Serialize(msgScope, stream); 181 227 message[ei] = stream.GetBuffer(); … … 193 239 var immigrantScope = HeuristicLab.Persistence.Default.Xml.XmlParser.Deserialize<IScope>(stream); 194 240 195 // replace random individual in current population 196 var rand = Random; 197 var replIdx = rand.Next(scope.SubScopes.Count); 198 199 scope.SubScopes.RemoveAt(replIdx); 241 // replace individual in current population 242 switch (selectedMigStrat) { 243 case MigrationStrategy.TakeBestReplaceBad: 244 scope.SubScopes.RemoveAt(0); 245 break; 246 247 case MigrationStrategy.TakeRandomReplaceBad: 248 scope.SubScopes.RemoveAt(0); 249 break; 250 251 case MigrationStrategy.TakeBestReplaceRandom: 252 //replace random 253 replIdx = rand.Next(scope.SubScopes.Count); 254 scope.SubScopes.RemoveAt(replIdx); 255 break; 256 257 case MigrationStrategy.TakeRandomReplaceRandom: 258 //replace random 259 replIdx = rand.Next(scope.SubScopes.Count); 260 scope.SubScopes.RemoveAt(replIdx); 261 break; 262 263 default: 264 break; 265 } 266 267 //insert individual sortet in population 200 268 var qualities = QualityParameter.ActualValue; 201 202 269 var qualityTranslatedName = QualityParameter.TranslatedName; 203 270 var qImmigrant = ((DoubleValue)immigrantScope.Variables[qualityTranslatedName].Value).Value; … … 227 294 } 228 295 229 public bool EnabledByDefault { get { return false; } } 230 231 #region IExecutable Members 232 233 public event EventHandler<EventArgs<Exception>> ExceptionOccurred; 234 235 public ExecutionState ExecutionState { 236 get { return ExecutionState.Prepared; } 237 } 238 239 public event EventHandler ExecutionStateChanged; 240 241 public TimeSpan ExecutionTime { 242 get { return new TimeSpan(); } 243 } 244 245 public event EventHandler ExecutionTimeChanged; 246 247 public void Pause() { 248 int i = 0; 249 } 250 251 public event EventHandler Paused; 252 253 public void Prepare() { 254 int i = 0; 255 } 256 257 public event EventHandler Prepared; 258 259 public void Start() { 260 int i = 0; 261 } 262 263 public event EventHandler Started; 264 265 public void Stop() { 266 int i = 0; 267 } 268 269 public event EventHandler Stopped; 270 271 #endregion 296 private void ExceptionThrown(object sender, Exception e) { 297 var log = LogParameter.Value; 298 log.LogMessage(e.Message); 299 } 300 272 301 } 273 302 }
Note: See TracChangeset
for help on using the changeset viewer.