Free cookie consent management tool by TermsFeed Policy Generator

source: branches/irace/HeuristicLab.Optimization/3.3/RunCollectionConstraints/RunCollectionEqualityConstraint.cs @ 11419

Last change on this file since 11419 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Optimization {
30  [StorableClass]
31  [Item("RunCollectionEqualityConstraint", "A constraint which checks the members of the contained runs for equality to the constraint data.")]
32  public class RunCollectionEqualityConstraint : EqualityConstraint, IRunCollectionColumnConstraint {
33    [StorableConstructor]
34    protected RunCollectionEqualityConstraint(bool deserializing) : base(deserializing) { }
35
36    protected RunCollectionEqualityConstraint(RunCollectionEqualityConstraint original, Cloner cloner)
37      : base(original, cloner) {
38      ConstraintData = original.ConstraintData;
39      ConstraintOperation = original.ConstraintOperation;
40      constraintColumn = original.constraintColumn;
41    }
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new RunCollectionEqualityConstraint(this, cloner);
44    }
45
46    public RunCollectionEqualityConstraint()
47      : base() {
48      this.ConstraintData = string.Empty;
49    }
50    public RunCollectionEqualityConstraint(RunCollection constrainedValue, ConstraintOperation constraintOperation, string constraintData)
51      : base(constrainedValue, constraintOperation, constraintData) {
52    }
53    public RunCollectionEqualityConstraint(RunCollection constrainedValue, ConstraintOperation constraintOperation, string constraintData, bool active)
54      : base(constrainedValue, constraintOperation, constraintData, active) {
55    }
56
57    public new RunCollection ConstrainedValue {
58      get { return (RunCollection)base.ConstrainedValue; }
59      set { base.ConstrainedValue = value; }
60    }
61
62    public new string ConstraintData {
63      get { return (string)base.ConstraintData; }
64      set { base.ConstraintData = value; }
65    }
66
67    [Storable]
68    private string constraintColumn;
69    public string ConstraintColumn {
70      get { return constraintColumn; }
71      set {
72        if (!((IStringConvertibleMatrix)ConstrainedValue).ColumnNames.Contains(value))
73          throw new ArgumentException("Could not set ConstraintData to not existing column index.");
74        if (constraintColumn != value) {
75          constraintColumn = value;
76          this.OnConstraintColumnChanged();
77          this.OnToStringChanged();
78        }
79      }
80    }
81
82    public event EventHandler ConstraintColumnChanged;
83    protected virtual void OnConstraintColumnChanged() {
84      EventHandler handler = ConstraintColumnChanged;
85      if (handler != null)
86        handler(this, EventArgs.Empty);
87    }
88
89    protected override void OnConstrainedValueChanged() {
90      base.OnConstrainedValueChanged();
91      IStringConvertibleMatrix matrix = (IStringConvertibleMatrix)ConstrainedValue;
92      if (constraintColumn == null && ConstrainedValue != null && matrix.Columns != 0)
93        constraintColumn = matrix.ColumnNames.ElementAt(0);
94    }
95
96    protected override bool Check(object constrainedMember) {
97      if (!Active)
98        return true;
99
100      foreach (IRun run in ConstrainedValue.Where(r => r.Visible)) {
101        IItem item = ConstrainedValue.GetValue(run, constraintColumn);
102        if (item != null && !base.Check(item.ToString()))
103          run.Visible = false;
104      }
105      return true;
106    }
107
108    protected override bool Check(object constrainedMember, out string errorMessage) {
109      errorMessage = string.Empty;
110      if (!Active)
111        return true;
112
113      foreach (IRun run in ConstrainedValue.Where(r => r.Visible)) {
114        IItem item = ConstrainedValue.GetValue(run, constraintColumn);
115        if (!base.Check(item))
116          run.Visible = false;
117      }
118      return true;
119    }
120
121    public override string ToString() {
122      string s = string.Empty;
123      IStringConvertibleMatrix matrix = ConstrainedValue;
124      if (matrix != null && matrix.ColumnNames.Count() != 0)
125        s += constraintColumn + " ";
126      else
127        return "EqualityConstraint";
128
129      if (ConstraintOperation != null)
130        s += ConstraintOperation.ToString() + " ";
131
132      if (!string.IsNullOrEmpty(ConstraintData))
133        s += ConstraintData;
134      else
135        s += "null";
136
137      return s;
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.