Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/29/21 09:54:58 (4 years ago)
Author:
mkommend
Message:

#3105: Merge trunk changes into branch.

Location:
branches/3105_PythonFormatter
Files:
10 edited
3 copied

Legend:

Unmodified
Added
Removed
  • branches/3105_PythonFormatter

  • branches/3105_PythonFormatter/HeuristicLab.Problems.DataAnalysis

  • branches/3105_PythonFormatter/HeuristicLab.Problems.DataAnalysis/3.4

  • branches/3105_PythonFormatter/HeuristicLab.Problems.DataAnalysis/3.4/DatasetExtensions.cs

    r17180 r17918  
    9696    }
    9797
     98    public static IntervalCollection GetIntervals(this IDataset dataset) {
     99      IntervalCollection intervalCollection = new IntervalCollection();
     100      foreach (var variable in dataset.DoubleVariables) { // intervals are only possible for double variables
     101        var variableInterval = Interval.GetInterval(dataset.GetDoubleValues(variable));
     102        intervalCollection.AddInterval(variable, variableInterval);
     103      }
     104
     105      return intervalCollection;
     106    }
     107
    98108    public static IEnumerable<KeyValuePair<string, IEnumerable<string>>> GetFactorVariableValues(
    99109      this IDataset ds, IEnumerable<string> factorVariables, IEnumerable<int> rows) {
  • branches/3105_PythonFormatter/HeuristicLab.Problems.DataAnalysis/3.4/HeuristicLab.Problems.DataAnalysis-3.4.csproj

    r17579 r17918  
    142142    <Compile Include="Implementation\Interval\Interval.cs" />
    143143    <Compile Include="Implementation\Interval\IntervalCollection.cs" />
     144    <Compile Include="Implementation\Interval\ShapeConstraint.cs" />
     145    <Compile Include="Implementation\Interval\ShapeConstraintsParser.cs" />
    144146    <Compile Include="Implementation\Regression\ConfidenceBoundRegressionSolution.cs" />
    145147    <Compile Include="Implementation\Regression\ConstantRegressionModel.cs" />
    146148    <Compile Include="Implementation\Regression\ConstantRegressionSolution.cs" />
     149    <Compile Include="Implementation\Regression\ShapeConstraints.cs" />
    147150    <Compile Include="Implementation\Regression\RegressionEnsembleProblemData.cs" />
    148151    <Compile Include="Implementation\Regression\RegressionEnsembleModel.cs">
  • branches/3105_PythonFormatter/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationProblemData.cs

    r17835 r17918  
    306306        classNamesCache.Add(ClassNamesParameter.Value[i, 0]);
    307307    }
     308
    308309    public override IDeepCloneable Clone(Cloner cloner) {
    309310      if (this == emptyProblemData) return emptyProblemData;
     
    314315
    315316    public ClassificationProblemData(IClassificationProblemData classificationProblemData)
    316       : this(classificationProblemData.Dataset, classificationProblemData.AllowedInputVariables, classificationProblemData.TargetVariable, classificationProblemData.ClassNames, classificationProblemData.PositiveClass) {
    317      
     317      : this(classificationProblemData, classificationProblemData.Dataset) {
     318    }
     319
     320    /// <summary>
     321    /// This method satisfies a common use case: making a copy of the problem but providing a different dataset.
     322    /// One must be careful here that the dataset passed is not modified, as that would invalidate the problem data internals.
     323    /// Passing a ModifiableDataset to this constructor is therefore discouraged.
     324    /// </summary>
     325    /// <param name="classificationProblemData">The original instance of classification problem data.</param>
     326    /// <param name="dataset">The new dataset.</param>
     327    public ClassificationProblemData(IClassificationProblemData classificationProblemData, IDataset dataset)
     328    : this(classificationProblemData.Dataset, classificationProblemData.AllowedInputVariables, classificationProblemData.TargetVariable, classificationProblemData.ClassNames, classificationProblemData.PositiveClass) {
     329
    318330      TrainingPartition.Start = classificationProblemData.TrainingPartition.Start;
    319331      TrainingPartition.End = classificationProblemData.TrainingPartition.End;
    320332      TestPartition.Start = classificationProblemData.TestPartition.Start;
    321333      TestPartition.End = classificationProblemData.TestPartition.End;
    322      
     334
    323335      for (int i = 0; i < Classes; i++) {
    324336        for (int j = 0; j < Classes; j++) {
     
    328340    }
    329341
    330     public ClassificationProblemData(IDataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable, IEnumerable<ITransformation> transformations = null)
    331       : this(dataset, allowedInputVariables, targetVariable, Enumerable.Empty<string>(), null, transformations) { }
    332 
    333342    public ClassificationProblemData(IDataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable,
    334       IEnumerable<string> classNames,
     343      IEnumerable<string> classNames = null,
    335344      string positiveClass = null, // can be null in which case it's set as the first class name
    336345      IEnumerable<ITransformation> transformations = null)
     
    348357
    349358      // set the class names
    350       if (classNames.Any()) {
     359      if (classNames != null && classNames.Any()) {
    351360        // better to allocate lists because we use these multiple times below
    352361        var names = classNames.ToList();
    353         var values = ClassValues.ToList();
     362        var values = ClassValuesCache;
    354363
    355364        if (names.Count != values.Count) {
  • branches/3105_PythonFormatter/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs

    r17583 r17918  
    3333    [Storable]
    3434    public double UpperBound { get; private set; }
     35
     36    public double Width => UpperBound - LowerBound;
    3537
    3638    [StorableConstructor]
     
    6769    }
    6870
     71    private Interval(double v) : this(v, v) { }
     72
    6973    public bool Contains(double value) {
    7074      return LowerBound <= value && value <= UpperBound;
     
    126130        return false;
    127131
    128       return (UpperBound.IsAlmost(other.UpperBound) || (double.IsNaN(UpperBound) && double.IsNaN(other.UpperBound)))
    129         && (LowerBound.IsAlmost(other.LowerBound) || (double.IsNaN(LowerBound) && double.IsNaN(other.LowerBound)));
     132      return (UpperBound==other.UpperBound || (double.IsNaN(UpperBound) && double.IsNaN(other.UpperBound)))
     133        && (LowerBound==other.LowerBound || (double.IsNaN(LowerBound) && double.IsNaN(other.LowerBound)));
    130134    }
    131135
     
    267271    }
    268272
    269     public static Interval AnalyticalQuotient(Interval a, Interval b) {
     273    public static Interval AnalyticQuotient(Interval a, Interval b) {
    270274      var dividend = a;
    271275      var divisor = Add(Square(b), new Interval(1.0, 1.0));
     
    276280    }
    277281    #endregion
     282
     283    #region arithmetic overloads
     284    public static Interval operator +(Interval a, Interval b) => Add(a, b);
     285    public static Interval operator +(Interval a, double b) => Add(a, new Interval(b));
     286    public static Interval operator +(double a, Interval b) => Add(new Interval(a), b);
     287    public static Interval operator -(Interval a, Interval b) => Subtract(a, b);
     288    public static Interval operator -(Interval a, double b) => Subtract(a, new Interval(b));
     289    public static Interval operator -(double a, Interval b) => Subtract(new Interval(a), b);
     290    public static Interval operator -(Interval a) => Subtract(new Interval(0), a);
     291    public static Interval operator *(Interval a, Interval b) => Multiply(a, b);
     292    public static Interval operator *(Interval a, double b) => Multiply(a, new Interval(b));
     293    public static Interval operator *(double a, Interval b) => Multiply(new Interval(a), b);
     294    public static Interval operator /(Interval a, Interval b) => Divide(a, b);
     295    public static Interval operator /(Interval a, double b) => Divide(a, new Interval(b));
     296    public static Interval operator /(double a, Interval b) => Divide(new Interval(a), b);
     297    public static Interval Exponential(double a) { return Exponential(new Interval(a)); }
     298    public static Interval Logarithm(double a) { return Logarithm(new Interval(a)); }
     299    public static Interval Sine(double a) { return Sine(new Interval(a)); }
     300    public static Interval Cosine(double a) { return Cosine(new Interval(a)); }
     301    public static Interval Tangens(double a) { return Tangens(new Interval(a)); }
     302    public static Interval HyperbolicTangent(double a) { return HyperbolicTangent(new Interval(a)); }
     303    public static Interval Square(double a) { return Square(new Interval(a)); }
     304    public static Interval Cube(double a) { return Cube(new Interval(a)); }
     305    public static Interval SquareRoot(double a) { return SquareRoot(new Interval(a)); }
     306    public static Interval CubicRoot(double a) { return CubicRoot(new Interval(a)); }
     307    public static Interval Absolute(double a) { return Absolute(new Interval(a)); }
     308    public static Interval AnalyticQuotient(Interval a, double b) { return AnalyticQuotient(a, new Interval(b)); }
     309    public static Interval AnalyticQuotient(double a, Interval b) { return AnalyticQuotient(new Interval(a), b); }
     310    public static Interval AnalyticQuotient(double a, double b) { return AnalyticQuotient(new Interval(a), new Interval(b)); }
     311    #endregion
    278312  }
    279313}
  • branches/3105_PythonFormatter/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/IntervalCollection.cs

    r17564 r17918  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    3434      get => HeuristicLab.Common.Resources.VSImageLibrary.Object;
    3535    }
    36     private IDictionary<string, Interval> intervals { get; } = new Dictionary<string, Interval>();
    3736
    38     [Storable(Name = "StorableIntervalInformation")]
     37    private IDictionary<string, Interval> intervals { get; set; } = new Dictionary<string, Interval>();
     38
     39    [Storable(OldName = "StorableIntervalInformation")]
    3940    private KeyValuePair<string, double[]>[] StorableIntervalInformation {
     41      set {
     42        foreach (var varInt in value)
     43          intervals.Add(varInt.Key, new Interval(varInt.Value[0], varInt.Value[1]));
     44      }
     45    }
     46
     47    [Storable]
     48    private object[] StorableIntervals {
    4049      get {
    41         var l = new List<KeyValuePair<string, double[]>>();
    42         foreach (var varInt in intervals)
     50        var names = intervals.Keys.ToArray();
     51        var lowerBounds = intervals.Values.Select(i => i.LowerBound).ToArray();
     52        var upperBounds = intervals.Values.Select(i => i.UpperBound).ToArray();
    4353
    44           l.Add(new KeyValuePair<string, double[]>(varInt.Key,
    45             new double[] { varInt.Value.LowerBound, varInt.Value.UpperBound }));
    46         return l.ToArray();
     54        return new object[] { names, lowerBounds, upperBounds };
    4755      }
    4856
    4957      set {
    50         foreach (var varInt in value)
    51           intervals.Add(varInt.Key, new Interval(varInt.Value[0], varInt.Value[1]));
     58        var names = (string[])value[0];
     59        var lowerBounds = (double[])value[1];
     60        var upperBounds = (double[])value[2];
     61
     62        for (int i = 0; i < names.Length; i++) {
     63          intervals.Add(names[i], new Interval(lowerBounds[i], upperBounds[i]));
     64        }
    5265      }
    5366    }
     
    8093    public void SetInterval(string identifier, Interval interval) {
    8194      intervals[identifier] = interval;
     95      RaiseChanged();
    8296    }
    8397
    8498    public void AddInterval(string identifier, Interval interval) {
    8599      intervals.Add(identifier, interval);
     100      RaiseChanged();
    86101    }
    87102
    88103    public void DeleteInterval(string identifier) {
    89104      intervals.Remove(identifier);
     105      RaiseChanged();
    90106    }
    91107
    92108    public IReadOnlyDictionary<string, Interval> GetReadonlyDictionary() {
     109      return intervals.ToDictionary(pair => pair.Key, pair => pair.Value);
     110    }
     111
     112    public IDictionary<string, Interval> GetDictionary() {
    93113      return intervals.ToDictionary(pair => pair.Key, pair => pair.Value);
    94114    }
     
    98118        yield return Tuple.Create(variableInterval.Key, variableInterval.Value);
    99119    }
     120
     121    public event EventHandler Changed;
     122    private void RaiseChanged() {
     123      var handler = Changed;
     124      if (handler != null)
     125        handler(this, EventArgs.Empty);
     126    }
     127
    100128  }
    101129}
  • branches/3105_PythonFormatter/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionProblemData.cs

    r17579 r17918  
    3535    protected const string TargetVariableParameterName = "TargetVariable";
    3636    protected const string VariableRangesParameterName = "VariableRanges";
    37     protected const string IntervalConstraintsParameterName = "IntervalConstraints";
     37    protected const string ShapeConstraintsParameterName = "ShapeConstraints";
    3838    public string Filename { get; set; }
    3939
     
    9494      problemData.Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>()));
    9595      problemData.Parameters.Add(new FixedValueParameter<IntervalCollection>(VariableRangesParameterName, "", new IntervalCollection()));
     96      problemData.Parameters.Add(new FixedValueParameter<ShapeConstraints>(ShapeConstraintsParameterName, "", new ShapeConstraints()));
    9697      emptyProblemData = problemData;
    9798    }
    9899    #endregion
    99100
    100     public IConstrainedValueParameter<StringValue> TargetVariableParameter {
    101       get { return (IConstrainedValueParameter<StringValue>)Parameters[TargetVariableParameterName]; }
    102     }
    103 
     101    #region parameter properties
     102    public IConstrainedValueParameter<StringValue> TargetVariableParameter => (IConstrainedValueParameter<StringValue>)Parameters[TargetVariableParameterName];
     103    public IFixedValueParameter<ShapeConstraints> ShapeConstraintsParameter => (IFixedValueParameter<ShapeConstraints>)Parameters[ShapeConstraintsParameterName];
    104104    public IFixedValueParameter<IntervalCollection> VariableRangesParameter => (IFixedValueParameter<IntervalCollection>)Parameters[VariableRangesParameterName];
    105 
     105    #endregion
     106
     107    #region properties
    106108    public IntervalCollection VariableRanges {
    107109      get => VariableRangesParameter.Value;
    108110    }
     111
     112
     113    public ShapeConstraints ShapeConstraints => ShapeConstraintsParameter.Value;
    109114
    110115
     
    120125      }
    121126    }
    122 
    123     public IEnumerable<double> TargetVariableValues {
    124       get { return Dataset.GetDoubleValues(TargetVariable); }
    125     }
    126     public IEnumerable<double> TargetVariableTrainingValues {
    127       get { return Dataset.GetDoubleValues(TargetVariable, TrainingIndices); }
    128     }
    129     public IEnumerable<double> TargetVariableTestValues {
    130       get { return Dataset.GetDoubleValues(TargetVariable, TestIndices); }
    131     }
     127    public IEnumerable<double> TargetVariableValues => Dataset.GetDoubleValues(TargetVariable);
     128    public IEnumerable<double> TargetVariableTrainingValues => Dataset.GetDoubleValues(TargetVariable, TrainingIndices);
     129    public IEnumerable<double> TargetVariableTestValues => Dataset.GetDoubleValues(TargetVariable, TestIndices);
     130    #endregion
     131
    132132
    133133
     
    137137    private void AfterDeserialization() {
    138138      if (!Parameters.ContainsKey(VariableRangesParameterName)) {
    139         var intervalCollection = CalculateDatasetIntervals(this.Dataset);
     139        var intervalCollection = Dataset.GetIntervals();
    140140        Parameters.Add(new FixedValueParameter<IntervalCollection>(VariableRangesParameterName, intervalCollection));
    141141      }
     142      if (Parameters.ContainsKey("IntervalConstraints")) {
     143        var param = (IFixedValueParameter<ShapeConstraints>)Parameters["IntervalConstraints"];
     144        Parameters.Remove(param);
     145        Parameters.Add(new FixedValueParameter<ShapeConstraints>(ShapeConstraintsParameterName, param.Value));
     146      }
     147      if (!Parameters.ContainsKey(ShapeConstraintsParameterName)) {
     148        Parameters.Add(new FixedValueParameter<ShapeConstraints>(ShapeConstraintsParameterName, new ShapeConstraints()));
     149      }
     150
    142151      RegisterParameterEvents();
    143152    }
     
    163172    }
    164173
    165     public RegressionProblemData(IDataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable, IEnumerable<ITransformation> transformations = null)
     174    public RegressionProblemData(IDataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable,
     175      IEnumerable<ITransformation> transformations = null,
     176      IntervalCollection variableRanges = null,
     177      ShapeConstraints shapeConstraints = null)
    166178      : base(dataset, allowedInputVariables, transformations ?? Enumerable.Empty<ITransformation>()) {
    167179      var variables = InputVariables.Select(x => x.AsReadOnly()).ToList();
    168180      Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>(variables), variables.Where(x => x.Value == targetVariable).First()));
    169       var intervalCollection = CalculateDatasetIntervals(this.Dataset);
    170       Parameters.Add(new FixedValueParameter<IntervalCollection>(VariableRangesParameterName, intervalCollection));
     181      if (variableRanges == null) {
     182        variableRanges = Dataset.GetIntervals();
     183      }
     184      Parameters.Add(new FixedValueParameter<IntervalCollection>(VariableRangesParameterName, variableRanges));
     185
     186      if (shapeConstraints == null) {
     187        shapeConstraints = new ShapeConstraints();
     188      }
     189      Parameters.Add(new FixedValueParameter<ShapeConstraints>(ShapeConstraintsParameterName, shapeConstraints));
    171190      RegisterParameterEvents();
    172191    }
    173 
    174     private static IntervalCollection CalculateDatasetIntervals(IDataset dataset) {
    175       IntervalCollection intervalCollection = new IntervalCollection();
    176       foreach (var variable in dataset.DoubleVariables) {// intervals are only possible for double variables
    177         var variableInterval = Interval.GetInterval(dataset.GetDoubleValues(variable));
    178         intervalCollection.AddInterval(variable, variableInterval);
    179       }
    180 
    181       return intervalCollection;
    182     }
    183 
    184192    private void RegisterParameterEvents() {
    185       TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged);
    186     }
    187     private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
     193      TargetVariableParameter.ValueChanged += new EventHandler(Parameter_ValueChanged);
     194      // VariableRanges and ShapeConstraints are fixed parameters
     195    }
     196    private void Parameter_ValueChanged(object sender, EventArgs e) {
    188197      OnChanged();
    189198    }
  • branches/3105_PythonFormatter/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Regression/IRegressionProblemData.cs

    r17579 r17918  
    11#region License Information
     2
    23/* HeuristicLab
    34 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     
    1819 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
    1920 */
     21
    2022#endregion
    2123
     
    2830    string TargetVariable { get; set; }
    2931
    30     IntervalCollection VariableRanges { get;}
     32    IntervalCollection VariableRanges { get; }
     33    ShapeConstraints ShapeConstraints { get; }
    3134
    3235    IEnumerable<double> TargetVariableValues { get; }
Note: See TracChangeset for help on using the changeset viewer.