1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Constraints;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.SimOpt {
|
---|
10 | public abstract class SimOptManipulationOperatorBase : OperatorBase {
|
---|
11 |
|
---|
12 | public SimOptManipulationOperatorBase()
|
---|
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 manipulate", typeof(ConstrainedItemList), VariableKind.In | VariableKind.Out));
|
---|
16 | AddVariableInfo(new VariableInfo("Index", "Which index in the parameter vector to manipulate", 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 | }
|
---|