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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Encodings.ConditionActionEncoding;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Encodings.VariableVector {
|
---|
33 | [StorableClass]
|
---|
34 | [Item("VariableVectorAction", "")]
|
---|
35 | public class VariableVectorAction : ItemDictionary<StringValue, IActionVariable>, IAction {
|
---|
36 |
|
---|
37 | public IOrderedEnumerable<StringValue> Order { get { return this.Keys.OrderBy(x => x.Value); } }
|
---|
38 |
|
---|
39 | public int VirtualLength { get { return this.Values.Sum(x => x.VirtualLength); } }
|
---|
40 |
|
---|
41 | [StorableConstructor]
|
---|
42 | protected VariableVectorAction(bool deserializing) : base(deserializing) { }
|
---|
43 | protected VariableVectorAction(VariableVectorAction original, Cloner cloner)
|
---|
44 | : base(original, cloner) {
|
---|
45 | }
|
---|
46 | public VariableVectorAction() : base() { }
|
---|
47 | public VariableVectorAction(int capacity) : base(capacity) { }
|
---|
48 | public VariableVectorAction(IDictionary<StringValue, IActionVariable> dictionary) : base(dictionary) { }
|
---|
49 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
50 | return new VariableVectorAction(this, cloner);
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected override void OnItemsAdded(IEnumerable<KeyValuePair<StringValue, IActionVariable>> items) {
|
---|
54 | foreach (var item in items) {
|
---|
55 | if (!(item.Value is StringVariable || item.Value is IntVariable)) {
|
---|
56 | throw new ArgumentException("Value has to be of type 'StringVariable' or 'IntVariable'");
|
---|
57 | }
|
---|
58 | }
|
---|
59 | base.OnItemsAdded(items);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public bool Match(IAction target) {
|
---|
63 | var targetCast = target as VariableVectorAction;
|
---|
64 | if (targetCast == null) { return false; }
|
---|
65 | if (this.Order.SequenceEqual(targetCast.Order)) { return false; }
|
---|
66 | foreach (var keyValuePair in targetCast) {
|
---|
67 | if (!this[keyValuePair.Key].MatchVariable(keyValuePair.Value)) {
|
---|
68 | return false;
|
---|
69 | }
|
---|
70 | }
|
---|
71 | return true;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public void Add(IEnumerable<IActionVariable> action) {
|
---|
75 | foreach (var variable in action) {
|
---|
76 | this.Add(new StringValue(variable.VariableName), variable);
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | public override string ToString() {
|
---|
81 | StringBuilder sb = new StringBuilder();
|
---|
82 | bool first = true;
|
---|
83 | foreach (var variable in this.Values) {
|
---|
84 | if (!first) {
|
---|
85 | first = false;
|
---|
86 | } else {
|
---|
87 | sb.Append(";");
|
---|
88 | }
|
---|
89 | sb.Append(variable.ToString());
|
---|
90 | }
|
---|
91 | return sb.ToString();
|
---|
92 | }
|
---|
93 |
|
---|
94 | public void Randomize(IRandom random) {
|
---|
95 | foreach (var variable in this.Values) {
|
---|
96 | variable.Randomize(random);
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | public bool Identical(VariableVectorCondition target) {
|
---|
101 | if (this.Order.SequenceEqual(target.Order)) { return false; }
|
---|
102 | foreach (var keyValuePair in target) {
|
---|
103 | if (!this[keyValuePair.Key].Identical(keyValuePair.Value)) {
|
---|
104 | return false;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | return true;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|