Free cookie consent management tool by TermsFeed Policy Generator

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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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  }
Note: See TracChangeset for help on using the changeset viewer.