Free cookie consent management tool by TermsFeed Policy Generator

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

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

Adding communication framework to branch 3.1 (ticket #278)

File size: 1.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Constraints;
8
9namespace HeuristicLab.SimOpt {
10  public abstract class SimOptInitializationOperatorBase : OperatorBase {
11
12    public SimOptInitializationOperatorBase()
13      : base() {
14      AddVariableInfo(new VariableInfo("Random", "A (uniform distributed) pseudo random number generator", typeof(IRandom), VariableKind.In));
15      AddVariableInfo(new VariableInfo("Items", "The parameter vector to initialize", typeof(ConstrainedItemList), VariableKind.In | VariableKind.Out));
16      AddVariableInfo(new VariableInfo("Index", "Which index in the parameter vector to initialize", typeof(IntData), VariableKind.In));
17      GetVariableInfo("Index").Local = true;
18      AddVariable(new Variable("Index", new IntData(-1)));
19    }
20
21    public override IOperation Apply(IScope scope) {
22      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
23      ConstrainedItemList parameterVector = GetVariableValue<ConstrainedItemList>("Items", scope, false);
24      IntData index = GetVariableValue<IntData>("Index", scope, false);
25      int i = index.Data;
26      if (i < 0 || i >= parameterVector.Count) throw new IndexOutOfRangeException("ERROR: Index is out of range of the parameter vector");
27      IItem item = parameterVector[i];
28      if (item is Variable) {
29        item = ((Variable)item).Value;
30      }
31      Apply(scope, random, item);
32      return null;
33    }
34
35    protected abstract void Apply(IScope scope, IRandom random, IItem item);
36
37    #region helper functions
38    protected bool IsIntegerConstrained(ConstrainedDoubleData data) {
39      foreach (IConstraint constraint in data.Constraints) {
40        if (constraint is IsIntegerConstraint) {
41          return true;
42        }
43      }
44      return false;
45    }
46    #endregion
47  }
48}
Note: See TracBrowser for help on using the repository browser.