#region License Information /* HeuristicLab * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using HeuristicLab.Common; using HeuristicLab.Core; using HEAL.Attic; namespace HeuristicLab.Problems.DataAnalysis { [StorableType("8109BE58-CCFB-4462-A2F4-EEE5DFADAFF7")] [Item("Interval Constraint", "Constraint on intervals.")] public sealed class IntervalConstraint : Item { private string expression; [Storable] public string Expression { get => expression; private set { if (expression != value) { expression = value; OnChanged(); OnToStringChanged(); } } } public string Definition { get { return GetDefinitionString(); } } private string variable; [Storable] public string Variable { get => variable; private set { if (variable != value) { variable = value; UpdateExpression(); OnChanged(); } } } private string target; [Storable] public string Target { get => target; set { if (target != value) { target = value; UpdateExpression(); OnChanged(); } } } public bool IsDerivation { get => numberOfDerivation > 0; } private int numberOfDerivation; [Storable] public int NumberOfDerivation { get => numberOfDerivation; set { if (value < 0 || value > 3) throw new ArgumentException("Number of derivation has to be between 0 - 3."); if (numberOfDerivation != value) { numberOfDerivation = value; UpdateExpression(); OnChanged(); } } } private Interval interval; [Storable] public Interval Interval { get => interval; set { if (interval != value) { interval = value; UpdateExpression(); OnChanged(); } } } private bool inclusiveLowerBound; [Storable] public bool InclusiveLowerBound { get => inclusiveLowerBound; set { if (inclusiveLowerBound != value) { inclusiveLowerBound = value; UpdateExpression(); OnChanged(); } } } private bool inclusiveUpperBound; [Storable] public bool InclusiveUpperBound { get => inclusiveUpperBound; set { if (inclusiveUpperBound != value) { inclusiveUpperBound = value; UpdateExpression(); OnChanged(); } } } private bool enabled; [Storable] public bool Enabled { get => enabled; set { if (value != enabled) { enabled = value; OnChanged(); } } } [StorableConstructor] private IntervalConstraint(StorableConstructorFlag _) : base(_) { } public IntervalConstraint(string expression, string variable, string target, int numberOfDerivation, Interval interval, bool inclusiveLowerBound, bool inclusiveUpperBound, bool enabled) : base(){ this.expression = expression; this.variable = variable; this.target = target; this.numberOfDerivation = numberOfDerivation; this.interval = interval; this.inclusiveLowerBound = inclusiveLowerBound; this.inclusiveUpperBound = inclusiveUpperBound; this.enabled = enabled; } public override IDeepCloneable Clone(Cloner cloner) { return new IntervalConstraint(this, cloner); } private IntervalConstraint(IntervalConstraint original, Cloner cloner) : base(original, cloner) { this.Expression = original.Expression; this.Variable = original.Variable; this.Target = original.Target; this.NumberOfDerivation = original.NumberOfDerivation; this.Interval = original.Interval; this.InclusiveLowerBound = original.InclusiveLowerBound; this.InclusiveUpperBound = original.InclusiveUpperBound; this.Enabled = original.Enabled; } public event EventHandler Changed; private void OnChanged() { EventHandler handlers = Changed; if (handlers != null) handlers(this, EventArgs.Empty); } private static string GetDerivationString(int derivation) { switch (derivation) { case 1: return ""; case 2: return "²"; case 3: return "³"; default: return ""; } } private void UpdateExpression() { var expression = ""; if (!IsDerivation) { expression = string.Format("Target:{0} in {1}{2} .. {3}{4}", Variable, (InclusiveLowerBound) ? "[" : "]", Interval?.LowerBound, Interval?.UpperBound, (InclusiveUpperBound) ? "]" : "["); Expression = expression; return; } expression = string.Format("∂{6}{1}/∂{0}{6} in {2}{3} .. {4}{5}", Variable, Target, (InclusiveLowerBound) ? "[" : "]", Interval?.LowerBound, Interval?.UpperBound, (InclusiveUpperBound) ? "]" : "[", GetDerivationString(numberOfDerivation)); Expression = expression; } private string GetDefinitionString() { if (!IsDerivation) { return "Target " + Variable; } var definition = $"∂{GetDerivationString(numberOfDerivation)}Target/∂{Variable}{GetDerivationString(numberOfDerivation)}"; return definition; } public override string ToString() { return Expression; } } }