Free cookie consent management tool by TermsFeed Policy Generator

Changeset 13969


Ignore:
Timestamp:
07/01/16 17:25:21 (8 years ago)
Author:
thasling
Message:

#2615:
implemented migration strategies
log in HL now also logs exceptions of peers

Location:
branches/thasling/DistributedGA
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/thasling/DistributedGA/DistributedGA.Core/Implementation/PeerNetworkMessageHandler.cs

    r13965 r13969  
    2828    private IMessageService host = null;
    2929
     30   public event EventHandler<Exception> ExceptionOccurend;   
    3031
    3132    public void Init(string lanIpPrefix, string contactServerUrl, string problemInstance, int messageCacheCapacity, double communicationRate) {
     
    120121    }
    121122
     123    private void PropagateException(Exception ex) {
     124      //if (CountdownCompleted != null)
     125      //  CountdownCompleted(this, e);
     126      if (ExceptionOccurend != null) {
     127        ExceptionOccurend(this, ex);
     128      }
     129    }
     130
    122131    private string GetInternalIpAddress(string ipPrefix) {
    123132      try {
     
    146155
    147156    private void AddError(string source, Exception ex) {
     157      PropagateException(ex);
    148158      if (peerListManager != null) {
    149159        try {
  • branches/thasling/DistributedGA/DistributedGA.Core/Interface/IMessageHandler.cs

    r13965 r13969  
    1 using System.Collections.Generic;
     1using System;
     2using System.Collections.Generic;
    23using DistributedGA.Core.Domain;
    34
     
    1718
    1819    void Dispose();
     20
     21    event EventHandler<Exception> ExceptionOccurend;
    1922  }
    2023}
  • branches/thasling/DistributedGA/DistributedGA.Hive/P2PMigrationAnalyzer.cs

    r13966 r13969  
    4141  [Item("P2PMigrationAnalyzer", "Migrates individuals using a P2P network.")]
    4242  [StorableClass]
    43   public class P2PMigrationAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator, IExecutable {
     43  public class P2PMigrationAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
    4444    // state: messagehandler
    4545    private IMessageHandler h;
     46
     47    public bool EnabledByDefault { get { return false; } }
    4648
    4749    public ILookupParameter<BoolValue> MaximizationParameter {
     
    134136      base.InitializeState();
    135137      // init P2P
     138      Init();
     139    }
     140
     141    private void Init() {
    136142      h = new PeerNetworkMessageHandler();
    137143      var lanIpPrefix = ((StringValue)(Parameters["LanIpPrefix"].ActualValue)).Value;
     
    141147      var messageCacheCapacity = ((IntValue)Parameters["MessageCacheCapacity"].ActualValue).Value;
    142148      h.Init(lanIpPrefix, contactServerUri, problemInstance, (int)(100 * messageCacheCapacity), (int)(100 * communicationRate));
     149      h.ExceptionOccurend += ExceptionThrown;
    143150    }
    144151
    145152    public override IOperation Apply() {
     153      if (h == null) {
     154        Init();
     155      }
    146156      if (MigrationIterationsParameter.ActualValue == null) {
    147157        MigrationIterationsParameter.ActualValue = new IntValue(0);
     
    155165        //define how many migrants to send
    156166        var migrationRate = ((PercentValue)Parameters["MigrationRate"].ActualValue).Value;
    157 
    158         //TODO: SELECT MIGRATION STRATEGY
    159         // TODO: select individuals based on quality
    160167        var popQualities = QualityParameter.ActualValue;
    161 
    162168        var selectedMigStrat = MigrationStrategyParameter.Value.Value;
    163169
    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);
    167212
    168213        {
     
    172217            using (var stream = new MemoryStream()) {
    173218              var emigrantScope = emigrantsList[ei];
    174               emigrantScope.ClearParentScopes();
     219
    175220              var msgScope = new Scope();
    176221              var cloner = new Cloner();
     
    178223                msgScope.Variables.Add((IVariable)variable.Clone(cloner));
    179224              }
     225              msgScope.ClearParentScopes();
    180226              HeuristicLab.Persistence.Default.Xml.XmlGenerator.Serialize(msgScope, stream);
    181227              message[ei] = stream.GetBuffer();
     
    193239              var immigrantScope = HeuristicLab.Persistence.Default.Xml.XmlParser.Deserialize<IScope>(stream);
    194240
    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
    200268              var qualities = QualityParameter.ActualValue;
    201 
    202269              var qualityTranslatedName = QualityParameter.TranslatedName;
    203270              var qImmigrant = ((DoubleValue)immigrantScope.Variables[qualityTranslatedName].Value).Value;
     
    227294    }
    228295
    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
    272301  }
    273302}
Note: See TracChangeset for help on using the changeset viewer.