Index: branches/thasling/DistributedGA/DistributedGA.Hive/P2PMigrationAnalyzer.cs
===================================================================
--- branches/thasling/DistributedGA/DistributedGA.Hive/P2PMigrationAnalyzer.cs	(revision 13959)
+++ branches/thasling/DistributedGA/DistributedGA.Hive/P2PMigrationAnalyzer.cs	(revision 13960)
@@ -22,4 +22,5 @@
 using System;
 using System.Collections.Generic;
+using System.Data.SqlTypes;
 using System.IO;
 using System.ServiceModel.Configuration;
@@ -40,16 +41,23 @@
   [Item("P2PMigrationAnalyzer", "Migrates individuals using a P2P network.")]
   [StorableClass]
-  public class P2PMigrationAnalyzer : SingleSuccessorOperator, IAnalyzer {
+  public class P2PMigrationAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
     // state: messagehandler
     private IMessageHandler h;
 
+    public ILookupParameter<BoolValue> MaximizationParameter {
+      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
+    }
+    // for name translation
+    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
+      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
+    }
     public ILookupParameter<IntValue> MigrationIterationsParameter {
       get { return (ILookupParameter<IntValue>)Parameters["MigrationIterations"]; }
     }
-    public ILookupParameter<IntValue> MigrationRatesParameter {
-      get { return (ILookupParameter<IntValue>)Parameters["MigrationRate"]; }
-    }
-    public ILookupParameter<IntValue> CommunicationRatesParameter {
-      get { return (ILookupParameter<IntValue>)Parameters["CommunicationRate"]; }
+    public ILookupParameter<PercentValue> MigrationRatesParameter {
+      get { return (ILookupParameter<PercentValue>)Parameters["MigrationRate"]; }
+    }
+    public ILookupParameter<PercentValue> CommunicationRatesParameter {
+      get { return (ILookupParameter<PercentValue>)Parameters["CommunicationRate"]; }
     }
     public ILookupParameter<IntValue> MessageCacheCapacityParameter {
@@ -66,4 +74,11 @@
     }
 
+    public IConstrainedValueParameter<EnumValue<MigrationStrategy>> MigrationStrategyParameter {
+      get { return (IConstrainedValueParameter<EnumValue<MigrationStrategy>>)Parameters["MigrationStrategy"]; }
+    }
+
+    public BoolValue Maximization {
+      get { return MaximizationParameter.ActualValue; }
+    }
     public IntValue MigrationIterations {
       get { return MigrationIterationsParameter.ActualValue; }
@@ -85,7 +100,9 @@
       : base() {
       Parameters.Add(new LookupParameter<IntValue>("MigrationIterations"));
+      Parameters.Add(new LookupParameter<BoolValue>("Maximization"));
+      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", 1));
       Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "", new IntValue(1)));
-      Parameters.Add(new ValueParameter<IntValue>("MigrationRate", "", new IntValue(5)));
-      Parameters.Add(new ValueParameter<IntValue>("CommunicationRate", "", new IntValue(10)));
+      Parameters.Add(new ValueParameter<PercentValue>("MigrationRate", "", new PercentValue(0.05)));
+      Parameters.Add(new ValueParameter<PercentValue>("CommunicationRate", "", new PercentValue(0.10)));
       Parameters.Add(new ValueParameter<IntValue>("MessageCacheCapacity", "", new IntValue(100)));
       Parameters.Add(new ValueParameter<StringValue>("LanIpPrefix", "", new StringValue("10.")));
@@ -94,4 +111,12 @@
       Parameters.Add(new ValueParameter<StringValue>("JobGUID", "", new StringValue(Guid.NewGuid().ToString())));
       Parameters.Add(new ValueParameter<ILog>("Log", "The log", new Log(1000)));
+
+      var validValues = new ItemSet<EnumValue<MigrationStrategy>>();
+      validValues.Add(new EnumValue<MigrationStrategy>(MigrationStrategy.TakeBestReplaceBad));
+      validValues.Add(new EnumValue<MigrationStrategy>(MigrationStrategy.TakeBestReplaceRandom));
+      validValues.Add(new EnumValue<MigrationStrategy>(MigrationStrategy.TakeRandomReplaceBad));
+      validValues.Add(new EnumValue<MigrationStrategy>(MigrationStrategy.TakeRandomReplaceRandom));
+
+      Parameters.Add(new ConstrainedValueParameter<EnumValue<MigrationStrategy>>("MigrationStrategy", validValues));
     }
 
@@ -113,8 +138,7 @@
       var contactServerUri = ((StringValue)(Parameters["ContactServerURL"].ActualValue)).Value;
       var problemInstance = ((StringValue)Parameters["JobGUID"].ActualValue).Value;
-      var communicationRate = ((IntValue)Parameters["CommunicationRate"].ActualValue).Value;
+      var communicationRate = ((PercentValue)Parameters["CommunicationRate"].ActualValue).Value;
       var messageCacheCapacity = ((IntValue)Parameters["MessageCacheCapacity"].ActualValue).Value;
-      h.Init(lanIpPrefix, contactServerUri, problemInstance, messageCacheCapacity, communicationRate);
-      var peer = h.GetPeerInfo();
+      h.Init(lanIpPrefix, contactServerUri, problemInstance, (int)(100 * messageCacheCapacity), (int)(100 * communicationRate));
     }
 
@@ -130,7 +154,11 @@
 
         //define how many migrants to send
-        var migrationRate = ((IntValue)Parameters["MigrationRate"].ActualValue).Value;
+        var migrationRate = ((PercentValue)Parameters["MigrationRate"].ActualValue).Value;
 
         //TODO: SELECT MIGRATION STRATEGY
+        // TODO: select individuals based on quality
+        var popQualities = QualityParameter.ActualValue;
+
+        var selectedMigStrat = MigrationStrategyParameter.Value.Value;
 
         // select best as emigrant
@@ -144,6 +172,11 @@
             using (var stream = new MemoryStream()) {
               var emigrantScope = emigrantsList[ei];
-              emigrantScope.ClearParentScopes();
-              HeuristicLab.Persistence.Default.Xml.XmlGenerator.Serialize(emigrantScope, stream);
+              var msgScope = new Scope();
+              var cloner = new Cloner();
+              foreach (var variable in emigrantScope.Variables) {
+                msgScope.Variables.Add((IVariable)variable.Clone(cloner));
+              }
+              // emigrantScope.ClearParentScopes();
+              HeuristicLab.Persistence.Default.Xml.XmlGenerator.Serialize(msgScope, stream);
               message[ei] = stream.GetBuffer();
             }
@@ -165,8 +198,23 @@
 
               scope.SubScopes.RemoveAt(replIdx);
-              scope.SubScopes.Insert(replIdx, immigrantScope);
+              var qualities = QualityParameter.ActualValue;
+
+              var qualityTranslatedName = QualityParameter.TranslatedName;
+              var qImmigrant = ((DoubleValue)immigrantScope.Variables[qualityTranslatedName].Value).Value;
+              var insertPos = scope.SubScopes.Count;
+              var maximization = Maximization.Value;
+              for (int i = 0; i < qualities.Length; i++) {
+                var qi = qualities[i].Value;
+                if ((maximization && qi < qImmigrant) || (!maximization && qi > qImmigrant)) {
+                  insertPos = i;
+                  break;
+                }
+              }
+
+              scope.SubScopes.Insert(insertPos, immigrantScope);
+
               var log = LogParameter.Value;
               double quality = 0.0;
-              if (immigrantScope.Variables.ContainsKey("Quality")) quality = ((DoubleValue)immigrantScope.Variables["Quality"].Value).Value;
+              quality = qImmigrant;
               log.LogMessage(string.Format("Recieved individual with quality {0}", quality));
             }
