Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.SimOpt/SimOptParameterPacker.cs @ 3900

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

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

File size: 4.2 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 System.Xml;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29
30namespace HeuristicLab.SimOpt {
31  public class SimOptParameterPacker : OperatorBase {
32    public override string Description {
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."; }
35    }
36
37    public SimOptParameterPacker()
38      : base() {
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;
43    }
44
45    public override IOperation Apply(IScope scope) {
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;
79        }
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));
83      }
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;
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.