1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.ConditionActionEncoding;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Encodings.VariableVector {
|
---|
32 | [StorableClass]
|
---|
33 | [Item("VariableVectorCondition", "")]
|
---|
34 | public class VariableVectorCondition : ItemDictionary<StringValue, IVariable>, ICondition {
|
---|
35 |
|
---|
36 | public int VirtualLength { get { return this.Values.Sum(x => x.VirtualLength); } }
|
---|
37 |
|
---|
38 | [StorableConstructor]
|
---|
39 | protected VariableVectorCondition(bool deserializing) : base(deserializing) { }
|
---|
40 | protected VariableVectorCondition(VariableVectorCondition original, Cloner cloner)
|
---|
41 | : base(original, cloner) {
|
---|
42 | }
|
---|
43 | public VariableVectorCondition() : base() { }
|
---|
44 | public VariableVectorCondition(int capacity) : base(capacity) { }
|
---|
45 | public VariableVectorCondition(IDictionary<StringValue, IVariable> dictionary) : base(dictionary) { }
|
---|
46 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
47 | return new VariableVectorCondition(this, cloner);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public bool Match(IInput target) {
|
---|
51 | var targetCast = target as VariableVectorInput;
|
---|
52 | if (targetCast == null) { return false; }
|
---|
53 | if (!(targetCast.Keys.Except(this.Keys).Count() == 0 && this.Keys.Except(targetCast.Keys).Count() == 0)) { return false; }
|
---|
54 | foreach (var keyValuePair in targetCast) {
|
---|
55 | if (!this[keyValuePair.Key].MatchInput(keyValuePair.Value.Value)) {
|
---|
56 | return false;
|
---|
57 | }
|
---|
58 | }
|
---|
59 | return true;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public void Add(IEnumerable<IVariable> condition) {
|
---|
63 | foreach (var variable in condition) {
|
---|
64 | this.Add(new StringValue(variable.VariableName), variable);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | public override string ToString() {
|
---|
69 | StringBuilder sb = new StringBuilder();
|
---|
70 | bool first = true;
|
---|
71 | foreach (var variable in this.Values) {
|
---|
72 | if (!first) {
|
---|
73 | first = false;
|
---|
74 | } else {
|
---|
75 | sb.Append(";");
|
---|
76 | }
|
---|
77 | sb.Append(variable.ToString());
|
---|
78 | }
|
---|
79 | return sb.ToString();
|
---|
80 | }
|
---|
81 |
|
---|
82 | public void Randomize(IRandom random, double spreadPercentage) {
|
---|
83 | foreach (var variable in this.Values) {
|
---|
84 | if (variable is DoubleVariable) {
|
---|
85 | ((DoubleVariable)variable).Randomize(random, spreadPercentage);
|
---|
86 | } else {
|
---|
87 | variable.Randomize(random);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | public bool Identical(VariableVectorCondition target) {
|
---|
93 | if (!(target.Keys.Except(this.Keys).Count() == 0 && this.Keys.Except(target.Keys).Count() == 0)) { return false; }
|
---|
94 | foreach (var keyValuePair in target) {
|
---|
95 | if (!this[keyValuePair.Key].Identical(keyValuePair.Value)) {
|
---|
96 | return false;
|
---|
97 | }
|
---|
98 | }
|
---|
99 | return true;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|