Changeset 13960
- Timestamp:
- 06/30/16 13:24:19 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/thasling/DistributedGA/DistributedGA.Hive/P2PMigrationAnalyzer.cs
r13959 r13960 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Data.SqlTypes; 24 25 using System.IO; 25 26 using System.ServiceModel.Configuration; … … 40 41 [Item("P2PMigrationAnalyzer", "Migrates individuals using a P2P network.")] 41 42 [StorableClass] 42 public class P2PMigrationAnalyzer : SingleSuccessorOperator, IAnalyzer {43 public class P2PMigrationAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator { 43 44 // state: messagehandler 44 45 private IMessageHandler h; 45 46 47 public ILookupParameter<BoolValue> MaximizationParameter { 48 get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; } 49 } 50 // for name translation 51 public ScopeTreeLookupParameter<DoubleValue> QualityParameter { 52 get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; } 53 } 46 54 public ILookupParameter<IntValue> MigrationIterationsParameter { 47 55 get { return (ILookupParameter<IntValue>)Parameters["MigrationIterations"]; } 48 56 } 49 public ILookupParameter< IntValue> MigrationRatesParameter {50 get { return (ILookupParameter< IntValue>)Parameters["MigrationRate"]; }51 } 52 public ILookupParameter< IntValue> CommunicationRatesParameter {53 get { return (ILookupParameter< IntValue>)Parameters["CommunicationRate"]; }57 public ILookupParameter<PercentValue> MigrationRatesParameter { 58 get { return (ILookupParameter<PercentValue>)Parameters["MigrationRate"]; } 59 } 60 public ILookupParameter<PercentValue> CommunicationRatesParameter { 61 get { return (ILookupParameter<PercentValue>)Parameters["CommunicationRate"]; } 54 62 } 55 63 public ILookupParameter<IntValue> MessageCacheCapacityParameter { … … 66 74 } 67 75 76 public IConstrainedValueParameter<EnumValue<MigrationStrategy>> MigrationStrategyParameter { 77 get { return (IConstrainedValueParameter<EnumValue<MigrationStrategy>>)Parameters["MigrationStrategy"]; } 78 } 79 80 public BoolValue Maximization { 81 get { return MaximizationParameter.ActualValue; } 82 } 68 83 public IntValue MigrationIterations { 69 84 get { return MigrationIterationsParameter.ActualValue; } … … 85 100 : base() { 86 101 Parameters.Add(new LookupParameter<IntValue>("MigrationIterations")); 102 Parameters.Add(new LookupParameter<BoolValue>("Maximization")); 103 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", 1)); 87 104 Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "", new IntValue(1))); 88 Parameters.Add(new ValueParameter< IntValue>("MigrationRate", "", new IntValue(5)));89 Parameters.Add(new ValueParameter< IntValue>("CommunicationRate", "", new IntValue(10)));105 Parameters.Add(new ValueParameter<PercentValue>("MigrationRate", "", new PercentValue(0.05))); 106 Parameters.Add(new ValueParameter<PercentValue>("CommunicationRate", "", new PercentValue(0.10))); 90 107 Parameters.Add(new ValueParameter<IntValue>("MessageCacheCapacity", "", new IntValue(100))); 91 108 Parameters.Add(new ValueParameter<StringValue>("LanIpPrefix", "", new StringValue("10."))); … … 94 111 Parameters.Add(new ValueParameter<StringValue>("JobGUID", "", new StringValue(Guid.NewGuid().ToString()))); 95 112 Parameters.Add(new ValueParameter<ILog>("Log", "The log", new Log(1000))); 113 114 var validValues = new ItemSet<EnumValue<MigrationStrategy>>(); 115 validValues.Add(new EnumValue<MigrationStrategy>(MigrationStrategy.TakeBestReplaceBad)); 116 validValues.Add(new EnumValue<MigrationStrategy>(MigrationStrategy.TakeBestReplaceRandom)); 117 validValues.Add(new EnumValue<MigrationStrategy>(MigrationStrategy.TakeRandomReplaceBad)); 118 validValues.Add(new EnumValue<MigrationStrategy>(MigrationStrategy.TakeRandomReplaceRandom)); 119 120 Parameters.Add(new ConstrainedValueParameter<EnumValue<MigrationStrategy>>("MigrationStrategy", validValues)); 96 121 } 97 122 … … 113 138 var contactServerUri = ((StringValue)(Parameters["ContactServerURL"].ActualValue)).Value; 114 139 var problemInstance = ((StringValue)Parameters["JobGUID"].ActualValue).Value; 115 var communicationRate = (( IntValue)Parameters["CommunicationRate"].ActualValue).Value;140 var communicationRate = ((PercentValue)Parameters["CommunicationRate"].ActualValue).Value; 116 141 var messageCacheCapacity = ((IntValue)Parameters["MessageCacheCapacity"].ActualValue).Value; 117 h.Init(lanIpPrefix, contactServerUri, problemInstance, messageCacheCapacity, communicationRate); 118 var peer = h.GetPeerInfo(); 142 h.Init(lanIpPrefix, contactServerUri, problemInstance, (int)(100 * messageCacheCapacity), (int)(100 * communicationRate)); 119 143 } 120 144 … … 130 154 131 155 //define how many migrants to send 132 var migrationRate = (( IntValue)Parameters["MigrationRate"].ActualValue).Value;156 var migrationRate = ((PercentValue)Parameters["MigrationRate"].ActualValue).Value; 133 157 134 158 //TODO: SELECT MIGRATION STRATEGY 159 // TODO: select individuals based on quality 160 var popQualities = QualityParameter.ActualValue; 161 162 var selectedMigStrat = MigrationStrategyParameter.Value.Value; 135 163 136 164 // select best as emigrant … … 144 172 using (var stream = new MemoryStream()) { 145 173 var emigrantScope = emigrantsList[ei]; 146 emigrantScope.ClearParentScopes(); 147 HeuristicLab.Persistence.Default.Xml.XmlGenerator.Serialize(emigrantScope, stream); 174 var msgScope = new Scope(); 175 var cloner = new Cloner(); 176 foreach (var variable in emigrantScope.Variables) { 177 msgScope.Variables.Add((IVariable)variable.Clone(cloner)); 178 } 179 // emigrantScope.ClearParentScopes(); 180 HeuristicLab.Persistence.Default.Xml.XmlGenerator.Serialize(msgScope, stream); 148 181 message[ei] = stream.GetBuffer(); 149 182 } … … 165 198 166 199 scope.SubScopes.RemoveAt(replIdx); 167 scope.SubScopes.Insert(replIdx, immigrantScope); 200 var qualities = QualityParameter.ActualValue; 201 202 var qualityTranslatedName = QualityParameter.TranslatedName; 203 var qImmigrant = ((DoubleValue)immigrantScope.Variables[qualityTranslatedName].Value).Value; 204 var insertPos = scope.SubScopes.Count; 205 var maximization = Maximization.Value; 206 for (int i = 0; i < qualities.Length; i++) { 207 var qi = qualities[i].Value; 208 if ((maximization && qi < qImmigrant) || (!maximization && qi > qImmigrant)) { 209 insertPos = i; 210 break; 211 } 212 } 213 214 scope.SubScopes.Insert(insertPos, immigrantScope); 215 168 216 var log = LogParameter.Value; 169 217 double quality = 0.0; 170 if (immigrantScope.Variables.ContainsKey("Quality")) quality = ((DoubleValue)immigrantScope.Variables["Quality"].Value).Value;218 quality = qImmigrant; 171 219 log.LogMessage(string.Format("Recieved individual with quality {0}", quality)); 172 220 }
Note: See TracChangeset
for help on using the changeset viewer.