Changeset 14013 for branches/thasling/DistributedGA
- Timestamp:
- 07/07/16 10:34:42 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/thasling/DistributedGA/DistributedGA.Hive/P2PMigrationAnalyzer.cs
r14010 r14013 23 23 using System.Collections.Generic; 24 24 using System.IO; 25 25 using System.Linq; 26 26 using DistributedGA.Core.Implementation; 27 27 using DistributedGA.Core.Interface; … … 127 127 } 128 128 129 public override void ClearState() { 130 base.ClearState(); 131 h.Dispose(); 132 h = null; 133 } 134 129 135 private void Init() { 130 136 h = new PeerNetworkMessageHandler(); … … 141 147 if (h == null) { 142 148 Init(); 143 //TODO: sort population144 145 149 } 150 146 151 if (MigrationIterationsParameter.ActualValue == null) { 147 152 MigrationIterationsParameter.ActualValue = new IntValue(0); … … 160 165 } 161 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 162 176 var selectedMigStratSelect = MigrationStrategySelectParameter.Value.Value; 163 177 var selectedMigStratReplace = MigrationStrategyReplaceParameter.Value.Value; … … 171 185 switch (selectedMigStratSelect) { 172 186 case MigrationStrategy.Best: 173 emigrants = s cope.SubScopes[i];187 emigrants = sortedPop[i]; 174 188 emigrantsList.Add(emigrants); 175 189 break; 176 190 177 191 case MigrationStrategy.Random: 178 replIdx = rand.Next(s cope.SubScopes.Count);179 emigrants = s cope.SubScopes[replIdx];192 replIdx = rand.Next(sortedPop.Count); 193 emigrants = sortedPop[replIdx]; 180 194 emigrantsList.Add(emigrants); 181 195 break; 182 196 183 197 case MigrationStrategy.Worst: 184 emigrants = s cope.SubScopes[scope.SubScopes.Count - i];198 emigrants = sortedPop[scope.SubScopes.Count - i - 1]; 185 199 emigrantsList.Add(emigrants); 186 break;187 188 189 default:190 200 break; 191 201 } … … 207 217 message = stream.GetBuffer(); 208 218 h.PublishDataToNetwork(message); 209 210 219 } 211 220 } … … 216 225 // recieve 217 226 var message = h.GetDataFromNetwork(); 218 //for (int ei = 0; ei < message.Length; ei++) { 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; 219 253 foreach (var msg in message) { 220 254 using (var stream = new MemoryStream(msg.Value)) { 221 255 var immigrantScope = HeuristicLab.Persistence.Default.Xml.XmlParser.Deserialize<IScope>(stream); 222 256 223 // replace individual in current population224 switch (selectedMigStratReplace) {225 case MigrationStrategy.Best:226 scope.SubScopes.RemoveAt(0);227 break;228 229 case MigrationStrategy.Random:230 replIdx = rand.Next(scope.SubScopes.Count);231 scope.SubScopes.RemoveAt(replIdx);232 break;233 234 case MigrationStrategy.Worst:235 //replace random236 scope.SubScopes.RemoveAt(scope.SubScopes.Count);237 break;238 239 default:240 break;241 }242 243 //insert individual sortet in population244 var qualities = QualityParameter.ActualValue;245 var qualityTranslatedName = QualityParameter.TranslatedName;246 257 var qImmigrant = ((DoubleValue)immigrantScope.Variables[qualityTranslatedName].Value).Value; 247 258 var insertPos = scope.SubScopes.Count; … … 258 269 259 270 var log = LogParameter.Value; 260 double quality = 0.0; 261 quality = qImmigrant; 271 double quality = qImmigrant; 262 272 log.LogMessage(string.Format("Recieved individual with quality {0} from peer {1}:{2} ; Job: {3}", 263 273 quality, msg.Key.IpAddress, msg.Key.Port, msg.Key.ProblemInstance));
Note: See TracChangeset
for help on using the changeset viewer.