Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/30/16 13:24:19 (8 years ago)
Author:
gkronber
Message:

#2615 debugging and changes preparing for different migration strategies

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/thasling/DistributedGA/DistributedGA.Hive/P2PMigrationAnalyzer.cs

    r13959 r13960  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Data.SqlTypes;
    2425using System.IO;
    2526using System.ServiceModel.Configuration;
     
    4041  [Item("P2PMigrationAnalyzer", "Migrates individuals using a P2P network.")]
    4142  [StorableClass]
    42   public class P2PMigrationAnalyzer : SingleSuccessorOperator, IAnalyzer {
     43  public class P2PMigrationAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
    4344    // state: messagehandler
    4445    private IMessageHandler h;
    4546
     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    }
    4654    public ILookupParameter<IntValue> MigrationIterationsParameter {
    4755      get { return (ILookupParameter<IntValue>)Parameters["MigrationIterations"]; }
    4856    }
    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"]; }
    5462    }
    5563    public ILookupParameter<IntValue> MessageCacheCapacityParameter {
     
    6674    }
    6775
     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    }
    6883    public IntValue MigrationIterations {
    6984      get { return MigrationIterationsParameter.ActualValue; }
     
    85100      : base() {
    86101      Parameters.Add(new LookupParameter<IntValue>("MigrationIterations"));
     102      Parameters.Add(new LookupParameter<BoolValue>("Maximization"));
     103      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", 1));
    87104      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)));
    90107      Parameters.Add(new ValueParameter<IntValue>("MessageCacheCapacity", "", new IntValue(100)));
    91108      Parameters.Add(new ValueParameter<StringValue>("LanIpPrefix", "", new StringValue("10.")));
     
    94111      Parameters.Add(new ValueParameter<StringValue>("JobGUID", "", new StringValue(Guid.NewGuid().ToString())));
    95112      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));
    96121    }
    97122
     
    113138      var contactServerUri = ((StringValue)(Parameters["ContactServerURL"].ActualValue)).Value;
    114139      var problemInstance = ((StringValue)Parameters["JobGUID"].ActualValue).Value;
    115       var communicationRate = ((IntValue)Parameters["CommunicationRate"].ActualValue).Value;
     140      var communicationRate = ((PercentValue)Parameters["CommunicationRate"].ActualValue).Value;
    116141      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));
    119143    }
    120144
     
    130154
    131155        //define how many migrants to send
    132         var migrationRate = ((IntValue)Parameters["MigrationRate"].ActualValue).Value;
     156        var migrationRate = ((PercentValue)Parameters["MigrationRate"].ActualValue).Value;
    133157
    134158        //TODO: SELECT MIGRATION STRATEGY
     159        // TODO: select individuals based on quality
     160        var popQualities = QualityParameter.ActualValue;
     161
     162        var selectedMigStrat = MigrationStrategyParameter.Value.Value;
    135163
    136164        // select best as emigrant
     
    144172            using (var stream = new MemoryStream()) {
    145173              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);
    148181              message[ei] = stream.GetBuffer();
    149182            }
     
    165198
    166199              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
    168216              var log = LogParameter.Value;
    169217              double quality = 0.0;
    170               if (immigrantScope.Variables.ContainsKey("Quality")) quality = ((DoubleValue)immigrantScope.Variables["Quality"].Value).Value;
     218              quality = qImmigrant;
    171219              log.LogMessage(string.Format("Recieved individual with quality {0}", quality));
    172220            }
Note: See TracChangeset for help on using the changeset viewer.