Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SimOpt/SimOptSequentialSubOperatorProcessor.cs @ 584

Last change on this file since 584 was 584, checked in by abeham, 16 years ago

merged communication framework to trunk (ticket #279)

File size: 3.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7
8namespace HeuristicLab.SimOpt {
9  public class SimOptSequentialSubOperatorProcessor : OperatorBase {
10    public override string Description {
11      get {
12        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.";
13      }
14    }
15
16    public SimOptSequentialSubOperatorProcessor()
17      : base() {
18      AddVariableInfo(new VariableInfo("Items", "The parameter vector", typeof(ConstrainedItemList), VariableKind.New | VariableKind.In | VariableKind.Out));
19      AddVariable(new Variable("ItemsBackup", new NullData()));
20      AddVariableInfo(new VariableInfo("MaximumTries", "", typeof(IntData), VariableKind.In));
21      GetVariableInfo("MaximumTries").Local = true;
22      AddVariable(new Variable("MaximumTries", new IntData(100)));
23      AddVariable(new Variable("Tries", new IntData(0)));
24    }
25
26    public override IOperation Apply(IScope scope) {
27      ConstrainedItemList parameterVector = GetVariableValue<ConstrainedItemList>("Items", scope, false, false);
28      // Mode: The parameter vector does not yet exist in the current scope
29      if (parameterVector == null) { // the parameter does not yet exist in the current scope
30        parameterVector = GetVariableValue<ConstrainedItemList>("Items", scope, true); // search for it
31        parameterVector = (ConstrainedItemList)parameterVector.Clone(); // clone it
32        scope.AddVariable(new Variable(scope.TranslateName("Items"), parameterVector)); // and add it to the current scope
33      }
34      // Mode: The parameter vector is marked for manipulation/initialization (constraint check is suspended)
35      if (parameterVector.ConstraintCheckSuspended) {
36        ICollection<IConstraint> violatedConstraints;
37        if (parameterVector.EndCombinedOperation(out violatedConstraints)) {
38          ((IntData)GetVariable("Tries").Value).Data = 0;
39          GetVariable("ItemsBackup").Value = new NullData();
40          return null; // manipulation/initialization was successful
41        } else { // restore old vector
42          int maximumTries = GetVariableValue<IntData>("MaximumTries", scope, true).Data;
43          IntData tries = (GetVariable("Tries").Value as IntData);
44          if (tries.Data >= maximumTries) throw new InvalidOperationException("ERROR: no valid solution in " + maximumTries.ToString() + " tries");
45          parameterVector = (ConstrainedItemList)GetVariable("ItemsBackup").Value;
46          scope.GetVariable(scope.TranslateName("Items")).Value = parameterVector;
47          ((IntData)GetVariable("Tries").Value).Data++;
48        }
49      }
50      // perform the sub operators in sequential order on the indices of the parameter vector
51      GetVariable("ItemsBackup").Value = (ConstrainedItemList)parameterVector.Clone();
52      CompositeOperation co = new CompositeOperation();
53      for (int i = 0; i < SubOperators.Count; i++) {
54        if (SubOperators[i].GetVariable("Index") != null) {
55          SubOperators[i].GetVariable("Index").Value = new IntData(i);
56        }
57        if (SubOperators[i].GetVariableInfo("Items") != null) {
58          SubOperators[i].GetVariableInfo("Items").ActualName = GetVariableInfo("Items").ActualName;
59        }
60      }
61      for (int i = 0; i < SubOperators.Count; i++)
62        co.AddOperation(new AtomicOperation(SubOperators[i], scope));
63      // add self to check if the manipulation/initialization did not violate any constraints
64      co.AddOperation(new AtomicOperation(this, scope));
65      parameterVector.BeginCombinedOperation();
66      return co;
67    }
68  }
69}
Note: See TracBrowser for help on using the repository browser.