Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.SimOpt/SimOptInitializationOperatorBase.cs @ 8250

Last change on this file since 8250 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: 2.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;
28using HeuristicLab.Constraints;
29
30namespace HeuristicLab.SimOpt {
31  public abstract class SimOptInitializationOperatorBase : OperatorBase {
32
33    public SimOptInitializationOperatorBase()
34      : base() {
35      AddVariableInfo(new VariableInfo("Random", "A (uniform distributed) pseudo random number generator", typeof(IRandom), VariableKind.In));
36      AddVariableInfo(new VariableInfo("Items", "The parameter vector to initialize", typeof(ConstrainedItemList), VariableKind.In | VariableKind.Out));
37      AddVariableInfo(new VariableInfo("Index", "Which index in the parameter vector to initialize", typeof(IntData), VariableKind.In));
38      GetVariableInfo("Index").Local = true;
39      AddVariable(new Variable("Index", new IntData(-1)));
40    }
41
42    public override IOperation Apply(IScope scope) {
43      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
44      ConstrainedItemList parameterVector = GetVariableValue<ConstrainedItemList>("Items", scope, false);
45      IntData index = GetVariableValue<IntData>("Index", scope, false);
46      int i = index.Data;
47      if (i < 0 || i >= parameterVector.Count) throw new IndexOutOfRangeException("ERROR: Index is out of range of the parameter vector");
48      IItem item = parameterVector[i];
49      if (item is Variable) {
50        item = ((Variable)item).Value;
51      }
52      Apply(scope, random, item);
53      return null;
54    }
55
56    protected abstract void Apply(IScope scope, IRandom random, IItem item);
57
58    #region helper functions
59    protected bool IsIntegerConstrained(ConstrainedDoubleData data) {
60      foreach (IConstraint constraint in data.Constraints) {
61        if (constraint is IsIntegerConstraint) {
62          return true;
63        }
64      }
65      return false;
66    }
67    #endregion
68  }
69}
Note: See TracBrowser for help on using the repository browser.