Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/RunCollectionComparisonConstraint.cs @ 3614

Last change on this file since 3614 was 3614, checked in by mkommend, 14 years ago

implemented first version of RunConstraints (ticket #970)

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