Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.SimOpt/SimOptSequentialSubOperatorProcessor.cs @ 2273

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

Put the GPL license in the files from the communication framework and simulation optimization project (branch 3.1)

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.SimOpt {
30  public class SimOptSequentialSubOperatorProcessor : OperatorBase {
31    public override string Description {
32      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      }
35    }
36
37    public SimOptSequentialSubOperatorProcessor()
38      : base() {
39      AddVariableInfo(new VariableInfo("Items", "The parameter vector", typeof(ConstrainedItemList), VariableKind.New | VariableKind.In | VariableKind.Out));
40      AddVariable(new Variable("ItemsBackup", new NullData()));
41      AddVariableInfo(new VariableInfo("MaximumTries", "", typeof(IntData), VariableKind.In));
42      GetVariableInfo("MaximumTries").Local = true;
43      AddVariable(new Variable("MaximumTries", new IntData(100)));
44      AddVariable(new Variable("Tries", new IntData(0)));
45    }
46
47    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      // 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;
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.