Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/30/14 11:49:08 (9 years ago)
Author:
ascheibe
Message:

#2267 added crossover selection operators

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/ProbabilitiesGenerator.cs

    r11492 r11511  
    3434  [Item("ProbabilitiesGenerator", "An operator the generates the probability vector for Multi Crossovers/Mutators")]
    3535  [StorableClass]
    36   public class ProbabilitiesGenerator : SingleSuccessorOperator {
     36  public class ProbabilitiesGenerator : SingleSuccessorOperator, IAnalyzer {
     37    public bool EnabledByDefault {
     38      get { return false; }
     39    }
    3740    public ValueParameter<StringValue> SuccessfulOffspringFlagParameter {
    3841      get { return (ValueParameter<StringValue>)Parameters["SuccessfulOffspringFlag"]; }
    3942    }
    40 
    4143    public ValueParameter<StringValue> OperatorNameVariableParameter {
    4244      get { return (ValueParameter<StringValue>)Parameters["OperatorNameVariable"]; }
    4345    }
    44     public LookupParameter<ResultCollection> SuccessfulOffspringAnalysisParameter {
    45       get { return (LookupParameter<ResultCollection>)Parameters["SuccessfulOffspringAnalysis"]; }
    46     }
    4746    public LookupParameter<IOperator> CrossoverParameter {
    4847      get { return (LookupParameter<IOperator>)Parameters["Crossover"]; }
    49     }
    50     public ILookupParameter<IntValue> GenerationsParameter {
    51       get { return (LookupParameter<IntValue>)Parameters["Generations"]; }
    5248    }
    5349    public LookupParameter<DoubleArray> ProbablilitiesParameter {
     
    5753    public ValueParameter<DoubleValue> MinimumOperatorUsageParameter {
    5854      get { return (ValueParameter<DoubleValue>)Parameters["MinimumOperatorUsage"]; }
     55    }
     56    public LookupParameter<ItemList<IScope>> GeneratedOffspringParameter {
     57      get { return (LookupParameter<ItemList<IScope>>)Parameters["GeneratedOffspring"]; }
    5958    }
    6059
     
    6968      Parameters.Add(new ValueParameter<StringValue>("SuccessfulOffspringFlag", "The name of the flag which indicates if the individual was successful.", new StringValue("SuccessfulOffspring")));
    7069      Parameters.Add(new ValueParameter<StringValue>("OperatorNameVariable", "The properties of the successful offspring that should be collected.", new StringValue("SelectedCrossoverOperator")));
    71       Parameters.Add(new LookupParameter<IntValue>("Generations", "The current number of generations."));
    72       Parameters.Add(new LookupParameter<ResultCollection>("SuccessfulOffspringAnalysis", "The successful offspring analysis which is created."));
    7370      Parameters.Add(new LookupParameter<DoubleArray>("Probabilities"));
    7471      Parameters.Add(new LookupParameter<IOperator>("Crossover"));
    7572      Parameters.Add(new ValueParameter<DoubleValue>("MinimumOperatorUsage", "Minimum percentage of operator usage. ", new DoubleValue(0.05)));
     73      Parameters.Add(new LookupParameter<ItemList<IScope>>("GeneratedOffspring", "Temporary store of the offspring population."));
    7674    }
    7775
    7876    public override IOperation Apply() {
    79       IScope scope = ExecutionContext.Scope;
    80       IScope offspring = scope.SubScopes[1];
     77      if (GeneratedOffspringParameter.ActualValue == null) {
     78        GeneratedOffspringParameter.ActualValue = new ItemList<IScope>();
     79      }
     80
     81      var offspring = GeneratedOffspringParameter.ActualValue;
    8182      string operatorName = OperatorNameVariableParameter.Value.Value;
    8283      string succssFlag = SuccessfulOffspringFlagParameter.Value.Value;
     
    8889        x.GetGenericTypeDefinition() == typeof(ICheckedMultiOperator<>))) {
    8990
    90         for (int i = 0; i < offspring.SubScopes.Count; i++) {
     91        for (int i = 0; i < offspring.Count; i++) {
    9192          // fetch values from scopes
    9293          IVariable tmpVar;
    93           if (!offspring.SubScopes[i].Variables.TryGetValue(succssFlag, out tmpVar))
     94          if (!offspring[i].Variables.TryGetValue(succssFlag, out tmpVar))
    9495            throw new InvalidOperationException(Name + ": Could not determine if an offspring was successful or not.");
    9596          BoolValue success = (tmpVar.Value as BoolValue);
    9697          if (success == null) throw new InvalidOperationException(Name + ": The variable that indicates whether an offspring is successful or not must contain a BoolValue.");
    9798
    98           if (!offspring.SubScopes[i].Variables.TryGetValue(operatorName, out tmpVar))
     99          if (!offspring[i].Variables.TryGetValue(operatorName, out tmpVar))
    99100            throw new InvalidOperationException(Name + ": Could not determine operator an offspring was created with.");
    100101          StringValue op = (tmpVar.Value as StringValue);
     
    126127        }
    127128
     129        //initialize probabilities vector
     130        if (ProbablilitiesParameter.ActualValue == null) {
     131          ProbablilitiesParameter.ActualValue = new DoubleArray(crossoverCount);
     132          for (int i = 0; i < crossoverCount; i++) {
     133            ProbablilitiesParameter.ActualValue[i] = 1.0;
     134          }
     135          return base.Apply();
     136        }
     137
    128138        //fill probabilities vector
    129139        for (int i = 0; i < crossoverCount; i++) {
     
    152162          }
    153163        }
     164
     165        GeneratedOffspringParameter.ActualValue.Clear();
    154166      }
    155167
Note: See TracChangeset for help on using the changeset viewer.