Free cookie consent management tool by TermsFeed Policy Generator

Changeset 637


Ignore:
Timestamp:
10/08/08 12:39:51 (16 years ago)
Author:
abeham
Message:

[trunk] Redesigned and simplified SimOpt according to ticket #291

Location:
trunk/sources/HeuristicLab.SimOpt
Files:
1 added
16 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.SimOpt/HeuristicLab.SimOpt.csproj

    r590 r637  
    5050  </ItemGroup>
    5151  <ItemGroup>
    52     <Compile Include="DefaultInitializer.cs" />
    53     <Compile Include="DefaultManipulator.cs" />
    54     <Compile Include="NormalDoubleManipulator.cs" />
    55     <Compile Include="NormalIntManipulator.cs" />
    56     <Compile Include="PermutationParameterCyclicCrossover.cs" />
    57     <Compile Include="PermutationParameterOrderCrossover.cs" />
    58     <Compile Include="PermutationParameterPartiallyMatchedCrossover.cs" />
    59     <Compile Include="SimOptCrossoverBase.cs" />
     52    <Compile Include="SimOptCrossoverPreparator.cs" />
    6053    <Compile Include="SimOptSequentialSubOperatorProcessor.cs" />
    6154    <Compile Include="SimOptSquentialSubOperatorCrossover.cs" />
    62     <Compile Include="Swap2PermutationManipulator.cs" />
    63     <Compile Include="RandomDoubleInitializer.cs" />
    64     <Compile Include="RandomPermutationInitializer.cs" />
    65     <Compile Include="SimOptInitializationOperatorBase.cs" />
    66     <Compile Include="SimOptManipulationOperatorBase.cs" />
    6755    <Compile Include="DoubleParameterBoundConstraint.cs" />
    6856    <Compile Include="DoubleParameterBoundConstraintView.cs">
     
    8775    </Compile>
    8876    <Compile Include="SimOptSinglePointCrossover.cs" />
    89     <Compile Include="TranslocationPermutationAdaptiveManipulator.cs" />
    90     <Compile Include="TranslocationPermutationManipulator.cs">
    91       <SubType>Code</SubType>
    92     </Compile>
    93     <Compile Include="UniformIntInitializer.cs" />
    9477  </ItemGroup>
    9578  <ItemGroup>
     
    127110      <Name>HeuristicLab.Operators</Name>
    128111    </ProjectReference>
    129     <ProjectReference Include="..\HeuristicLab.Permutation\HeuristicLab.Permutation.csproj">
    130       <Project>{55FDCFB3-3E8C-4FB0-837C-0CF06BEDED39}</Project>
    131       <Name>HeuristicLab.Permutation</Name>
    132     </ProjectReference>
    133112    <ProjectReference Include="..\HeuristicLab.PluginInfrastructure\HeuristicLab.PluginInfrastructure.csproj">
    134113      <Project>{94186A6A-5176-4402-AE83-886557B53CCA}</Project>
    135114      <Name>HeuristicLab.PluginInfrastructure</Name>
    136115    </ProjectReference>
    137     <ProjectReference Include="..\HeuristicLab.Random\HeuristicLab.Random.csproj">
    138       <Project>{47019A74-F7F7-482E-83AA-D3F4F777E879}</Project>
    139       <Name>HeuristicLab.Random</Name>
     116    <ProjectReference Include="..\HeuristicLab.Selection\HeuristicLab.Selection.csproj">
     117      <Project>{F7CF0571-25CB-43D5-8443-0843A1E2861A}</Project>
     118      <Name>HeuristicLab.Selection</Name>
    140119    </ProjectReference>
    141120  </ItemGroup>
  • trunk/sources/HeuristicLab.SimOpt/HeuristicLabSimOptPlugin.cs

    r591 r637  
    3232  [Dependency(Dependency = "HeuristicLab.Data-3.2")]
    3333  [Dependency(Dependency = "HeuristicLab.Operators-3.2")]
    34   [Dependency(Dependency = "HeuristicLab.Random-3.2")]
    3534  [Dependency(Dependency = "HeuristicLab.Constraints-3.2")]
    3635  [Dependency(Dependency = "HeuristicLab.Evolutionary-3.2")]
    37   [Dependency(Dependency = "HeuristicLab.Permutation-3.2")]
     36  [Dependency(Dependency = "HeuristicLab.Selection-3.2")]
    3837  public class HeuristicLabSimOptPlugin : PluginBase {
    3938  }
  • trunk/sources/HeuristicLab.SimOpt/SimOptDiscreteMultiCrossover.cs

    r591 r637  
    2626using HeuristicLab.Data;
    2727using HeuristicLab.Operators;
    28 using HeuristicLab.Random;
    2928using HeuristicLab.Evolutionary;
    3029
  • trunk/sources/HeuristicLab.SimOpt/SimOptParameterExtractor.cs

    r591 r637  
    3131  public class SimOptParameterExtractor : OperatorBase {
    3232    public override string Description {
    33       get { return @"Injects the contents of a ConstrainedItemList into the scope"; }
     33      get { return @"Appends each parameter as subscope to the current scope."; }
    3434    }
    3535
    3636    public SimOptParameterExtractor()
    3737      : base() {
    38       AddVariableInfo(new VariableInfo("Items", "The ConstrainedItemList to be extracted", typeof(ConstrainedItemList), VariableKind.In));
     38      AddVariableInfo(new VariableInfo("Items", "The ConstrainedItemList to be extracted", typeof(ConstrainedItemList), VariableKind.In | VariableKind.Deleted));
     39      AddVariableInfo(new VariableInfo("DeleteItems", "Whether or not to remove items from the current scope", typeof(BoolData), VariableKind.In));
     40      AddVariable(new Variable("DeleteItems", new BoolData(false)));
     41      GetVariableInfo("DeleteItems").Local = true;
    3942    }
    4043
    4144    public override IOperation Apply(IScope scope) {
    4245      ConstrainedItemList cil = GetVariableValue<ConstrainedItemList>("Items", scope, false);
     46      bool delete = GetVariableValue<BoolData>("DeleteItems", scope, true).Data;
    4347      for (int i = 0; i < cil.Count; i++) {
    44         IVariable var = scope.GetVariable(((Variable)cil[i]).Name);
    45         if (var == null) scope.AddVariable((IVariable)cil[i].Clone());
    46         else var.Value = (IItem)((Variable)cil[i]).Value.Clone();
     48        IScope tmp = new Scope(scope.Name + "_Param" + i.ToString());
     49        try {
     50          scope.AddVariable(cil[i].Clone() as Variable);
     51        } catch (InvalidCastException ice) {
     52          throw new InvalidCastException("Parameters in the constrained item list have to be encapsulated in a variable!\r\n\r\n" + ice.Message);
     53        }
     54        scope.AddSubScope(tmp);
     55      }
     56      if (delete) {
     57        IVariableInfo info = GetVariableInfo("Items");
     58        if (info.Local) RemoveVariable(info.ActualName);
     59        else scope.RemoveVariable(info.ActualName);
    4760      }
    4861      return null;
  • trunk/sources/HeuristicLab.SimOpt/SimOptParameterPacker.cs

    r591 r637  
    3131  public class SimOptParameterPacker : OperatorBase {
    3232    public override string Description {
    33       get { return @"Updates a ConstrainedItemList with the variables in the scope and removes them afterwards."; }
     33      get { return @"Takes the parameters in the subscope and creates or a updates a parameter vector. The order of the subscopes is assumed to be the same as the parameters appear in the vector.
     34If the parameter vector could not be updated due to a constraint violation, the first suboperator is returned as the next operation."; }
    3435    }
    3536
    3637    public SimOptParameterPacker()
    3738      : base() {
    38       AddVariableInfo(new VariableInfo("Items", "The ConstrainedItemList to be updated", typeof(ConstrainedItemList), VariableKind.In | VariableKind.Out));
     39      AddVariableInfo(new VariableInfo("Items", "The ConstrainedItemList to be updated or created", typeof(ConstrainedItemList), VariableKind.New | VariableKind.In | VariableKind.Out));
     40      AddVariableInfo(new VariableInfo("DeleteParameters", "Whether or not the subscopes containing the parameters should be removed afterwards", typeof(BoolData), VariableKind.In));
     41      AddVariable(new Variable("DeleteParameters", new BoolData(true)));
     42      GetVariableInfo("DeleteParameters").Local = true;
    3943    }
    4044
    4145    public override IOperation Apply(IScope scope) {
    42       ConstrainedItemList cil = GetVariableValue<ConstrainedItemList>("Items", scope, false);
    43       for (int i = 0; i < cil.Count; i++) {
    44         IVariable var = scope.GetVariable(((Variable)cil[i]).Name);
    45         if (var == null) throw new InvalidOperationException("ERROR in SimOptParameterPacker: Cannot find variable " + ((Variable)cil[i]).Name + " in scope");
    46         else {
    47           ((Variable)cil[i]).Value = (IItem)var.Value.Clone();
    48           scope.RemoveVariable(var.Name);
     46      // ----- FETCH THE PARAMETER VECTOR ----- //
     47      bool updateVector = true;
     48      ConstrainedItemList cil;
     49      try {
     50        cil = GetVariableValue<ConstrainedItemList>("Items", scope, false);
     51      } catch (ArgumentException) {
     52        updateVector = false;
     53        // the parameter vector is fetched from a higher scope and added locally
     54        cil = GetVariableValue<ConstrainedItemList>("Items", scope, true);
     55      }
     56      ConstrainedItemList tempcil = (ConstrainedItemList)cil.Clone();
     57      bool delete = GetVariableValue<BoolData>("DeleteParameters", scope, true).Data;
     58
     59      ICollection<IConstraint> violatedConstraints;
     60
     61      tempcil.BeginCombinedOperation();
     62      // ----- FETCH PARAMETERS AND UPDATE TEMPORARY VECTOR ----- //
     63      for (int i = 0; i < scope.SubScopes.Count; i++) {
     64        IVariable var = scope.SubScopes[i].GetVariable(((IVariable)tempcil[i]).Name);
     65        if (var == null) throw new ArgumentNullException(scope.SubScopes[i].Name, "Could not find parameter " + ((IVariable)tempcil[i]).Name + " in this scope");
     66        tempcil.TrySetAt(i, var, out violatedConstraints);
     67      }
     68
     69      // ----- CONSTRAINT HANDLING ----- //
     70      IVariableInfo info = GetVariableInfo("Items");
     71      bool error = tempcil.EndCombinedOperation(out violatedConstraints);
     72      if (!error) {
     73        if (!updateVector) {
     74          if (info.Local) AddVariable(new Variable(info.ActualName, tempcil));
     75          else scope.AddVariable(new Variable(scope.TranslateName("Items"), tempcil));
     76        } else {
     77          if (info.Local) GetVariable(info.ActualName).Value = tempcil;
     78          else scope.GetVariable(scope.TranslateName("Items")).Value = tempcil;
    4979        }
     80      } else if (!updateVector) { // in case there was an error and the parameter vector is not in the current scope, add it
     81        if (info.Local) AddVariable(new Variable(info.ActualName, cil));
     82        else scope.AddVariable(new Variable(scope.TranslateName("Items"), cil));
    5083      }
    51       return null;
     84
     85      // ----- DELETE SUBSCOPES ----- //
     86      if (delete) {
     87        scope.SubScopes.Clear();
     88      }
     89
     90      if (error) return new AtomicOperation(SubOperators[0], scope);
     91      else return null;
    5292    }
    5393  }
  • trunk/sources/HeuristicLab.SimOpt/SimOptSequentialSubOperatorProcessor.cs

    r591 r637  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.Data;
     28using HeuristicLab.Operators;
    2829
    2930namespace HeuristicLab.SimOpt {
     
    3132    public override string Description {
    3233      get {
    33         return @"Applies its suboperators on the parameter vector one for each index, afterwards it checks if the parameter vector satisfies all the constraints. If not it restores the old parameter vector and tries again until a maximum amount of tries (default: 100) has been reached.";
     34        return @"This operator encapsulates the functionality of processing the parameters of a simulation parameter vector. It works as follows:
     351. The parameters are extracted [SimOptParameterExtractor]
     362. The parameters are processed [SequentialSubScopeProcessor which applies the SubOperators of this operator on each parameter]
     373. The parameters are updated again [SimOptParameterPacker]
     38
     39Should the packing fail due to constraint violations, the operator will retry as often as specified in the variable MaximumTries.";
    3440      }
    3541    }
     
    4248      GetVariableInfo("MaximumTries").Local = true;
    4349      AddVariable(new Variable("MaximumTries", new IntData(100)));
    44       AddVariable(new Variable("Tries", new IntData(0)));
     50      AddVariable(new Variable("Tries", new IntData(1)));
    4551    }
    4652
    4753    public override IOperation Apply(IScope scope) {
    48       ConstrainedItemList parameterVector = GetVariableValue<ConstrainedItemList>("Items", scope, false, false);
    49       // Mode: The parameter vector does not yet exist in the current scope
    50       if (parameterVector == null) { // the parameter does not yet exist in the current scope
    51         parameterVector = GetVariableValue<ConstrainedItemList>("Items", scope, true); // search for it
    52         parameterVector = (ConstrainedItemList)parameterVector.Clone(); // clone it
    53         scope.AddVariable(new Variable(scope.TranslateName("Items"), parameterVector)); // and add it to the current scope
     54
     55      int tries = (GetVariable("Tries").Value as IntData).Data;
     56      int maxTries = GetVariableValue<IntData>("MaximumTries", scope, true).Data;
     57
     58      IVariableInfo itemsInfo = GetVariableInfo("Items");
     59      SimOptParameterExtractor extractor = new SimOptParameterExtractor();
     60      extractor.GetVariableInfo("Items").ActualName = itemsInfo.ActualName;
     61
     62      SequentialSubScopesProcessor sssp = new SequentialSubScopesProcessor();
     63      foreach (IOperator op in SubOperators)
     64        sssp.AddSubOperator(op);
     65
     66      SimOptParameterPacker packer = new SimOptParameterPacker();
     67      packer.GetVariableInfo("Items").ActualName = itemsInfo.ActualName;
     68      if (tries < maxTries) {
     69        SimOptSequentialSubOperatorProcessor sssop = (SimOptSequentialSubOperatorProcessor)this.Clone();
     70        (sssop.GetVariable("Tries").Value as IntData).Data = tries + 1;
     71        packer.AddSubOperator(this);
    5472      }
    55       // Mode: The parameter vector is marked for manipulation/initialization (constraint check is suspended)
    56       if (parameterVector.ConstraintCheckSuspended) {
    57         ICollection<IConstraint> violatedConstraints;
    58         if (parameterVector.EndCombinedOperation(out violatedConstraints)) {
    59           ((IntData)GetVariable("Tries").Value).Data = 0;
    60           GetVariable("ItemsBackup").Value = new NullData();
    61           return null; // manipulation/initialization was successful
    62         } else { // restore old vector
    63           int maximumTries = GetVariableValue<IntData>("MaximumTries", scope, true).Data;
    64           IntData tries = (GetVariable("Tries").Value as IntData);
    65           if (tries.Data >= maximumTries) throw new InvalidOperationException("ERROR: no valid solution in " + maximumTries.ToString() + " tries");
    66           parameterVector = (ConstrainedItemList)GetVariable("ItemsBackup").Value;
    67           scope.GetVariable(scope.TranslateName("Items")).Value = parameterVector;
    68           ((IntData)GetVariable("Tries").Value).Data++;
    69         }
    70       }
    71       // perform the sub operators in sequential order on the indices of the parameter vector
    72       GetVariable("ItemsBackup").Value = (ConstrainedItemList)parameterVector.Clone();
    73       CompositeOperation co = new CompositeOperation();
    74       for (int i = 0; i < SubOperators.Count; i++) {
    75         if (SubOperators[i].GetVariable("Index") != null) {
    76           SubOperators[i].GetVariable("Index").Value = new IntData(i);
    77         }
    78         if (SubOperators[i].GetVariableInfo("Items") != null) {
    79           SubOperators[i].GetVariableInfo("Items").ActualName = GetVariableInfo("Items").ActualName;
    80         }
    81       }
    82       for (int i = 0; i < SubOperators.Count; i++)
    83         co.AddOperation(new AtomicOperation(SubOperators[i], scope));
    84       // add self to check if the manipulation/initialization did not violate any constraints
    85       co.AddOperation(new AtomicOperation(this, scope));
    86       parameterVector.BeginCombinedOperation();
    87       return co;
     73
     74      CompositeOperation operation = new CompositeOperation();
     75      operation.AddOperation(new AtomicOperation(extractor, scope));
     76      operation.AddOperation(new AtomicOperation(sssp, scope));
     77      operation.AddOperation(new AtomicOperation(packer, scope));
     78
     79      return operation;
    8880    }
    8981  }
  • trunk/sources/HeuristicLab.SimOpt/SimOptSinglePointCrossover.cs

    r591 r637  
    2626using HeuristicLab.Data;
    2727using HeuristicLab.Operators;
    28 using HeuristicLab.Random;
    2928using HeuristicLab.Evolutionary;
    3029
  • trunk/sources/HeuristicLab.SimOpt/SimOptSquentialSubOperatorCrossover.cs

    r591 r637  
    2424using System.Linq;
    2525using System.Text;
     26using System.Xml;
    2627using HeuristicLab.Core;
    2728using HeuristicLab.Data;
    28 using HeuristicLab.Evolutionary;
     29using HeuristicLab.Operators;
     30using HeuristicLab.Selection;
    2931
    3032namespace HeuristicLab.SimOpt {
    3133  public class SimOptSquentialSubOperatorCrossover : OperatorBase {
     34    private UniformSequentialSubScopesProcessor usssp, usssp2, usssp3, usssp4;
     35    private SequentialSubScopesProcessor sssp;
     36    private SimOptParameterExtractor extractor;
     37    private SimOptParameterPacker packer;
     38    private SimOptCrossoverPreparator preparator;
     39    private MergingReducer merger;
     40    private bool uptodate;
     41
    3242    public override string Description {
    33       get { return @"Takes the parameter vector of two items and on each index applies the respectively indexed suboperator"; }
     43      get {
     44        return @"This operator encapsulates the functionality of crossing the parameters of a simulation parameter vector. It works as follows:
     451. The parameters of all parents are extracted [UniformSequentialSubScopeProcessor which applies a SimOptParameterExtractor]
     462. The parents are prepared for crossing by grouping the respective parameters [SimOptCrossoverPreparator]
     473. The parameters are crossed [UniformSequentialSubScopeProcessor which applies SequentialSubScopeProcessor which applies the SubOperators of this operator on each parameter group (except for the last one)]
     484. Assigning the crossed parameters to the respective children [MergingReducer]
     495. Update the parameters [UniformSequentialSubScopeProcessor which applies SimOptParameterPacker]
     50
     51Should the packing fail due to constraint violations, the operator will execute the last of its suboperators.";
     52      }
    3453    }
    3554
     
    3857      AddVariableInfo(new VariableInfo("Items", "The parameter vector", typeof(ConstrainedItemList), VariableKind.In | VariableKind.New));
    3958      AddVariableInfo(new VariableInfo("Parents", "The number of parents per child", typeof(IntData), VariableKind.In));
     59      uptodate = false;
    4060    }
    4161
    4262    public override IOperation Apply(IScope scope) {
    43       int parents = GetVariableValue<IntData>("Parents", scope, true).Data;
     63      string itemsActualName = GetVariableInfo("Items").ActualName;
     64      string parentsActualName = GetVariableInfo("Parents").ActualName;
     65      int parameters = SubOperators.Count - 1;
    4466
    45       int subScopesCount = scope.SubScopes.Count;
    46       if (subScopesCount < parents || (subScopesCount % parents) != 0)
    47         throw new InvalidOperationException("Size of mating pool is not a multiple (>1) of the number of parents per child");
    48       int children = subScopesCount / parents;
     67      if (!uptodate) {
     68        usssp = new UniformSequentialSubScopesProcessor();
     69        extractor = new SimOptParameterExtractor();
     70        usssp.AddSubOperator(extractor);
     71
     72        preparator = new SimOptCrossoverPreparator();
     73
     74        usssp2 = new UniformSequentialSubScopesProcessor();
     75        sssp = new SequentialSubScopesProcessor();
     76        for (int i = 0; i < parameters; i++) {
     77          sssp.AddSubOperator(SubOperators[i]);
     78        }
     79        usssp2.AddSubOperator(sssp);
     80
     81        usssp3 = new UniformSequentialSubScopesProcessor();
     82        merger = new MergingReducer();
     83        usssp3.AddSubOperator(merger);
     84
     85        usssp4 = new UniformSequentialSubScopesProcessor();
     86        packer = new SimOptParameterPacker();
     87        packer.AddSubOperator(SubOperators[SubOperators.Count - 1]);
     88        usssp4.AddSubOperator(packer);
     89        uptodate = true;
     90      }
     91      // Setting the actual names is necessary as the operator does not know if they've changed
     92      extractor.GetVariableInfo("Items").ActualName = itemsActualName;
     93      preparator.GetVariableInfo("Parents").ActualName = parentsActualName;
     94      packer.GetVariableInfo("Items").ActualName = itemsActualName;
    4995
    5096      CompositeOperation co = new CompositeOperation();
    51       for (int i = 0; i < SubOperators.Count; i++) {
    52         if (SubOperators[i].GetVariable("Index") != null) {
    53           SubOperators[i].GetVariable("Index").Value = new IntData(i);
    54         }
    55         if (SubOperators[i].GetVariableInfo("Items") != null) {
    56           SubOperators[i].GetVariableInfo("Items").ActualName = GetVariableInfo("Items").ActualName;
    57         }
    58       }
    59       for (int i = 0; i < children; i++) {
    60         IScope child = (IScope)scope.SubScopes[0].Clone();
    61         for (int j = 0; j < parents; j++) {
    62           IScope parent = scope.SubScopes[0];
    63           child.AddSubScope(parent);
    64           scope.RemoveSubScope(parent);
    65         }
    66         scope.AddSubScope(child);
    67         for (int n = 0 ; n < SubOperators.Count ; n++)
    68           co.AddOperation(new AtomicOperation(SubOperators[n], child));
    69         co.AddOperation(new AtomicOperation(new Operators.SubScopesRemover(), child));
    70       }
     97      co.AddOperation(new AtomicOperation(usssp, scope));
     98      co.AddOperation(new AtomicOperation(preparator, scope));
     99      co.AddOperation(new AtomicOperation(usssp2, scope));
     100      co.AddOperation(new AtomicOperation(usssp3, scope));
     101      co.AddOperation(new AtomicOperation(usssp4, scope));
    71102      return co;
     103    }
     104
     105    protected override void OnSubOperatorAdded(IOperator subOperator, int index) {
     106      base.OnSubOperatorAdded(subOperator, index);
     107      uptodate = false;
     108    }
     109
     110    protected override void OnSubOperatorRemoved(IOperator subOperator, int index) {
     111      base.OnSubOperatorRemoved(subOperator, index);
     112      uptodate = false;
     113    }
     114
     115    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
     116      base.Populate(node, restoredObjects);
     117      uptodate = false;
    72118    }
    73119  }
Note: See TracChangeset for help on using the changeset viewer.