Changeset 16323


Ignore:
Timestamp:
11/23/18 15:19:39 (4 weeks ago)
Author:
chaider
Message:

#2966: Added Interval class and Interpreter class.

Location:
branches/2966_interval_calculation
Files:
2 edited
2 copied

Legend:

Unmodified
Added
Removed
  • branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r16322 r16323  
    199199    <Compile Include="Interfaces\IVariableSymbol.cs" />
    200200    <Compile Include="Interpreter\SymbolicDataAnalysisExpressionCompiledTreeInterpreter.cs" />
     201    <Compile Include="Interpreter\Intervalnterpreter.cs" />
    201202    <Compile Include="SymbolicDataAnalysisExpressionTreeSimplificationOperator.cs" />
    202203    <Compile Include="SymbolicDataAnalysisModelComplexityCalculator.cs" />
  • branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/Intervalnterpreter.cs

    r16313 r16323  
    1414  [StorableClass]
    1515  [Item("SymbolicDataAnalysisIntervalArithmeticInterpreter", "Interpreter for interval arithmetic within symbolic regression.")]
    16   public class SymbolicDataAnalysisIntervalArithmeticInterpreter : ParameterizedNamedItem{
     16  public class IntervalInterpreter : ParameterizedNamedItem{
    1717    private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
    1818    public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter {
     
    2727    public void ClearState() { }
    2828
    29     protected SymbolicDataAnalysisIntervalArithmeticInterpreter(bool deserializing) : base(deserializing) { }
    30 
    31     protected SymbolicDataAnalysisIntervalArithmeticInterpreter(SymbolicDataAnalysisIntervalArithmeticInterpreter original,
     29    protected IntervalInterpreter(bool deserializing) : base(deserializing) { }
     30
     31    protected IntervalInterpreter(IntervalInterpreter original,
    3232        Cloner cloner)
    3333        : base(original, cloner) { }
    3434
    35     public SymbolicDataAnalysisIntervalArithmeticInterpreter()
     35    public IntervalInterpreter()
    3636        : base("SymbolicDataAnalysisIntervalArithmeticInterpreter", "Interpreter for interval arithmetic within symbolic regression.") { }
    3737
    3838    public override IDeepCloneable Clone(Cloner cloner) {
    39       return new SymbolicDataAnalysisIntervalArithmeticInterpreter(this, cloner);
     39      return new IntervalInterpreter(this, cloner);
    4040    }
    4141
     
    7979    }
    8080
    81     //private static InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows, Dictionary<string, Interval> customIntervals = null) {
    82     //  Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
    83     //  int necessaryArgStackSize = 0;
    84     //  foreach (Instruction instr in code) {
    85     //    if (instr.opCode == OpCodes.Variable) {
    86     //      var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
    87     //      IList<double> values = new List<double>();
    88 
    89     //      if (customIntervals != null && customIntervals.ContainsKey(variableTreeNode.VariableName)) {
    90     //        instr.data = customIntervals[variableTreeNode.VariableName];
    91     //      } else {
    92     //        foreach (var rowEnum in rows) {
    93     //          values.Add(dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName)[rowEnum]);
    94     //        }
    95     //        instr.data = new Interval(values.Min(), values.Max());
    96     //      }
    97     //      //instr.data = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
    98     //    } else if (instr.opCode == OpCodes.FactorVariable) {
    99     //      var factorTreeNode = instr.dynamicNode as FactorVariableTreeNode;
    100     //      instr.data = dataset.GetReadOnlyStringValues(factorTreeNode.VariableName);
    101     //    } else if (instr.opCode == OpCodes.BinaryFactorVariable) {
    102     //      var factorTreeNode = instr.dynamicNode as BinaryFactorVariableTreeNode;
    103     //      instr.data = dataset.GetReadOnlyStringValues(factorTreeNode.VariableName);
    104     //    } else if (instr.opCode == OpCodes.LagVariable) {
    105     //      var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode;
    106     //      instr.data = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName);
    107     //    } else if (instr.opCode == OpCodes.VariableCondition) {
    108     //      var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode;
    109     //      instr.data = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName);
    110     //    } else if (instr.opCode == OpCodes.Call) {
    111     //      necessaryArgStackSize += instr.nArguments + 1;
    112     //    }
    113     //  }
    114     //  return new InterpreterState(code, necessaryArgStackSize);
    115     //}
    116 
    11781    private Interval Evaluate(InterpreterState state, Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
    11882      Instruction currentInstr = state.NextInstruction();
     
    186150            return s;
    187151          }
     152        case OpCodes.Square: {
     153            var s = Interval.Square(Evaluate(state, intervals));
     154            intervals.Add(currentInstr.dynamicNode, s);
     155            return s;
     156          }
    188157        case OpCodes.Root: {
    189158            var s = Evaluate(state, intervals);
     
    191160              s = Interval.Root(s, Evaluate(state, intervals));
    192161            }
     162            intervals.Add(currentInstr.dynamicNode, s);
     163            return s;
     164          }
     165        case OpCodes.SquareRoot: {
     166            var s = Interval.SquareRoot(Evaluate(state, intervals));
    193167            intervals.Add(currentInstr.dynamicNode, s);
    194168            return s;
     
    213187      EvaluatedSolutions = 0;
    214188    }
    215 
    216     //public InterpreterState GetInstructions(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows, Dictionary<string, Interval> customIntervals = null) {
    217     //  var state = PrepareInterpreterState(tree, dataset, rows, customIntervals);
    218     //  var x = Evaluate(dataset, state);
    219     //  state.Reset();
    220 
    221     //  return state;
    222     //}
    223 
    224     // public Interval GetSymbolicExressionTreeInterval(tree,variableIntervals)
    225     //public Interval GetSymbolicExressionTreeInterval(tree,Dataset,intervals = null)
    226189
    227190    public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IEnumerable<int> rows, out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
  • branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis/3.4/HeuristicLab.Problems.DataAnalysis-3.4.csproj

    r16322 r16323  
    191191    <Compile Include="Implementation\ConstantModel.cs" />
    192192    <Compile Include="Implementation\DataAnalysisModel.cs" />
     193    <Compile Include="Implementation\Interval.cs" />
    193194    <Compile Include="Implementation\Regression\ConfidenceBoundRegressionSolution.cs" />
    194195    <Compile Include="Implementation\Regression\ConstantRegressionModel.cs" />
  • branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval.cs

    r16303 r16323  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2018 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;
    223using System.Collections.Generic;
    324using System.Linq;
    4 using System.Text;
    5 using System.Threading.Tasks;
    625using HeuristicLab.Common;
    726
     
    1130    public double LowerBound {
    1231      get { return lowerBound; }
    13       set { lowerBound = value; }
     32      private set { lowerBound = value; }
    1433    }
    1534    private double upperBound;
    1635    public double UpperBound {
    1736      get { return upperBound; }
    18       set { lowerBound = value; }
    19     }
    20 
    21     public Interval() { }
     37      private set { lowerBound = value; }
     38    }
    2239
    2340    public Interval(double lowerBound, double upperBound) {
     
    165182    }
    166183
     184    public static Interval Square(Interval a) {
     185      return Power(a, new Interval(2, 2));
     186    }
     187
     188    public static Interval Cubic(Interval a) {
     189      return Power(a, new Interval(3, 3));
     190    }
    167191
    168192    public static Interval Root(Interval a, Interval b) {
     
    173197    }
    174198
    175     public static Interval Average(IEnumerable<Interval> intervals) {
    176       if (intervals == null || !intervals.Any()) throw new ArgumentException();
    177 
    178       int count = intervals.Count();
    179       Interval result = intervals.First();
    180       foreach (Interval i in intervals.Skip(1))
    181         result = Interval.Add(result, i);
    182 
    183       return new Interval(result.lowerBound / count, result.upperBound / count);
     199    public static Interval SquareRoot(Interval a) {
     200      return Root(a, new Interval(2, 2));
     201    }
     202
     203    public static Interval CubicRoot(Interval a) {
     204      return Root(a, new Interval(3, 3));
    184205    }
    185206    #endregion
Note: See TracChangeset for help on using the changeset viewer.