Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/13/09 17:28:07 (16 years ago)
Author:
gkronber
Message:

Worked on #722 (IModel should provide a Predict() method to get predicted values for an input vector).
At the same time removed parameter PunishmentFactor from GP algorithms (this parameter is internal to TreeEvaluators now).

Location:
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/BaseClasses/AlgorithmBase.cs

    r2270 r2285  
    7070    }
    7171
    72     private IModel model;
    73     public virtual IModel Model {
     72    private IAnalyzerModel model;
     73    public virtual IAnalyzerModel Model {
    7474      get {
    7575        if (!engine.Terminated) throw new InvalidOperationException("The algorithm is still running. Wait until the algorithm is terminated to retrieve the result.");
     
    100100      get { return GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data; }
    101101      set { GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data = value; }
    102     }
    103 
    104     public virtual double PunishmentFactor {
    105       get { return GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data; }
    106       set { GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data = value; }
    107102    }
    108103
     
    132127      MaxTreeHeight = 10;
    133128      Parents = 2000;
    134       PunishmentFactor = 10;
    135129      UseEstimatedTargetValue = false;
    136130    }
     
    212206      injector.AddVariable(new HeuristicLab.Core.Variable("TotalEvaluatedNodes", new DoubleData(0)));
    213207      injector.AddVariable(new HeuristicLab.Core.Variable("Parents", new IntData()));
    214       injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData()));
    215208      injector.AddVariable(new HeuristicLab.Core.Variable("UseEstimatedTargetValue", new BoolData()));
    216209      injector.AddVariable(new HeuristicLab.Core.Variable("TreeEvaluator", new HL2TreeEvaluator()));
     
    416409    }
    417410
    418     protected internal virtual Model CreateGPModel(IScope bestModelScope) {
     411    protected internal virtual IAnalyzerModel CreateGPModel(IScope bestModelScope) {
    419412      Engine.GlobalScope.AddSubScope(bestModelScope);
    420       Model model = new Model();
     413      IGeneticProgrammingModel tree = bestModelScope.GetVariableValue<IGeneticProgrammingModel>("FunctionTree", false);
     414      ITreeEvaluator evaluator = bestModelScope.GetVariableValue<ITreeEvaluator>("TreeEvaluator", false);
     415      IAnalyzerModel model = new AnalyzerModel();
     416      model.Predictor = new Predictor(evaluator, tree);
    421417      Dataset ds = bestModelScope.GetVariableValue<Dataset>("Dataset", true);
    422       model.Data = bestModelScope.GetVariableValue<IGeneticProgrammingModel>("FunctionTree", false);
    423418      model.Dataset = ds;
    424419      model.TargetVariable = ds.GetVariableName(bestModelScope.GetVariableValue<IntData>("TargetVariable", true).Data);
     
    450445        double impact = ((DoubleData)row[1]).Data;
    451446        model.SetVariableEvaluationImpact(variableName, impact);
    452         model.AddInputVariables(variableName);
     447        model.AddInputVariable(variableName);
    453448      }
    454449      foreach (ItemList row in qualityImpacts) {
     
    456451        double impact = ((DoubleData)row[1]).Data;
    457452        model.SetVariableQualityImpact(variableName, impact);
    458         model.AddInputVariables(variableName);
     453        model.AddInputVariable(variableName);
    459454      }
    460455      Engine.GlobalScope.RemoveSubScope(bestModelScope);
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/BaseClasses/TreeEvaluatorBase.cs

    r2235 r2285  
    2525using HeuristicLab.DataAnalysis;
    2626using HeuristicLab.GP.Interfaces;
     27using System.Xml;
    2728
    2829namespace HeuristicLab.GP.StructureIdentification {
     
    3233  public abstract class TreeEvaluatorBase : ItemBase, ITreeEvaluator {
    3334    protected const double EPSILON = 1.0e-7;
    34     protected double maxValue;
    35     protected double minValue;
     35    protected double maxValue = double.MaxValue;
     36    protected double minValue = double.MinValue;
     37    private double punishmentFactor = 10.0; // we should provide a view for treeevaluators that allows to change this value
    3638
    3739    protected class Instr {
     
    4850    protected int sampleIndex;
    4951
    50     public void PrepareForEvaluation(Dataset dataset, int targetVariable, int start, int end, double punishmentFactor, IFunctionTree functionTree) {
     52    public void PrepareForEvaluation(Dataset dataset, IFunctionTree functionTree) {
    5153      this.dataset = dataset;
     54      codeArr = new Instr[functionTree.GetSize()];
     55      int i = 0;
     56      foreach (IFunctionTree tree in IteratePrefix(functionTree)) {
     57        codeArr[i++] = TranslateToInstr(tree);
     58      }
     59    }
     60
     61    public void PrepareForEvaluation(Dataset dataset, int targetVariable, int start, int end, IFunctionTree functionTree) {
    5262      // calculate upper and lower bounds for the estimated value (mean +/- punishmentFactor * range)
    5363      double mean = dataset.GetMean(targetVariable, start, end);
     
    5565      maxValue = mean + punishmentFactor * range;
    5666      minValue = mean - punishmentFactor * range;
    57 
    58       codeArr = new Instr[functionTree.GetSize()];
    59       int i = 0;
    60       foreach (IFunctionTree tree in IteratePrefix(functionTree)) {
    61         codeArr[i++] = TranslateToInstr(tree);
    62       }
     67      PrepareForEvaluation(dataset, functionTree);
    6368    }
    6469
     
    118123
    119124    protected abstract double EvaluateBakedCode();
     125
     126    public override object Clone(IDictionary<Guid, object> clonedObjects) {
     127      TreeEvaluatorBase clone = (TreeEvaluatorBase)base.Clone(clonedObjects);
     128      clone.maxValue = maxValue;
     129      clone.minValue = minValue;
     130      clone.punishmentFactor = punishmentFactor;
     131      return clone;
     132    }
     133
     134    public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
     135      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
     136      XmlAttribute maxValueAttribute = document.CreateAttribute("MaxPredictionValue");
     137      XmlAttribute minValueAttribute = document.CreateAttribute("MinPredictionValue");
     138      XmlAttribute punishmentFactorAttribute = document.CreateAttribute("PunishmentFactor");
     139      maxValueAttribute.Value = XmlConvert.ToString(maxValue);
     140      minValueAttribute.Value = XmlConvert.ToString(minValue);
     141      punishmentFactorAttribute.Value = XmlConvert.ToString(punishmentFactor);
     142
     143      node.Attributes.Append(punishmentFactorAttribute);
     144      node.Attributes.Append(minValueAttribute);
     145      node.Attributes.Append(maxValueAttribute);
     146      return node;
     147    }
     148
     149    public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
     150      base.Populate(node, restoredObjects);
     151      minValue = XmlConvert.ToDouble(node.Attributes["MinPredictionValue"].Value);
     152      maxValue = XmlConvert.ToDouble(node.Attributes["MaxPredictionValue"].Value);
     153      punishmentFactor = XmlConvert.ToDouble(node.Attributes["PunishmentFactor"].Value);
     154    }
    120155  }
    121156}
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/GPEvaluatorBase.cs

    r2222 r2285  
    3333      AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In));
    3434      AddVariableInfo(new VariableInfo("TargetVariable", "Index of the column of the dataset that holds the target variable", typeof(IntData), VariableKind.In));
    35       AddVariableInfo(new VariableInfo("PunishmentFactor", "Punishment factor for invalid estimations", typeof(DoubleData), VariableKind.In));
    3635      AddVariableInfo(new VariableInfo("TotalEvaluatedNodes", "Number of evaluated nodes", typeof(DoubleData), VariableKind.In | VariableKind.Out));
    3736      AddVariableInfo(new VariableInfo("TrainingSamplesStart", "Start index of training samples in dataset", typeof(IntData), VariableKind.In));
     
    4746      Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
    4847      IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, true);
    49       double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data;
    5048      double totalEvaluatedNodes = scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data;
    5149      int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
     
    5553      bool useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValue", scope, true).Data;
    5654      ITreeEvaluator evaluator = GetVariableValue<ITreeEvaluator>("TreeEvaluator", scope, true);
    57       evaluator.PrepareForEvaluation(dataset, targetVariable, trainingStart, trainingEnd, punishmentFactor, gpModel.FunctionTree);
     55      evaluator.PrepareForEvaluation(dataset, targetVariable, trainingStart, trainingEnd, gpModel.FunctionTree);
    5856
    5957      double[] backupValues = null;
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/VariableEvaluationImpactCalculator.cs

    r2222 r2285  
    3333      AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be evaluated", typeof(IGeneticProgrammingModel), VariableKind.In));
    3434      AddVariableInfo(new VariableInfo("TreeSize", "Size (number of nodes) of the tree to evaluate", typeof(IntData), VariableKind.In));
    35       AddVariableInfo(new VariableInfo("PunishmentFactor", "Punishment factor for invalid estimations", typeof(DoubleData), VariableKind.In));
    3635    }
    3736
     
    4140      IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, true);
    4241      double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data;
    43       evaluator.PrepareForEvaluation(dataset, targetVariable, start, end, punishmentFactor, gpModel.FunctionTree);
     42      evaluator.PrepareForEvaluation(dataset, targetVariable, start, end, gpModel.FunctionTree);
    4443
    4544      double[] result = new double[end - start];
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/VariableQualityImpactCalculator.cs

    r2241 r2285  
    3333      AddVariableInfo(new VariableInfo("TreeEvaluator", "The evaluator that should be used to evaluate the expression tree", typeof(ITreeEvaluator), VariableKind.In));
    3434      AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be evaluated", typeof(IGeneticProgrammingModel), VariableKind.In));
    35       AddVariableInfo(new VariableInfo("PunishmentFactor", "Punishment factor for invalid estimations", typeof(DoubleData), VariableKind.In));
    3635    }
    3736
     
    4039      IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, true);
    4140      double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data;
    42       evaluator.PrepareForEvaluation(dataset, targetVariable, start, end, punishmentFactor, gpModel.FunctionTree);
     41      evaluator.PrepareForEvaluation(dataset, targetVariable, start, end, gpModel.FunctionTree);
    4342
    4443      double[,] result = new double[end - start, 2];
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/HeuristicLab.GP.StructureIdentification-3.3.csproj

    r2239 r2285  
    8585    <Compile Include="BaseClasses\FunctionTreeBase.cs" />
    8686    <Compile Include="BaseClasses\TreeEvaluatorBase.cs" />
     87    <Compile Include="Predictor.cs" />
    8788    <Compile Include="Evaluators\SimpleGPEvaluatorBase.cs" />
    8889    <Compile Include="HL3TreeEvaluator.cs" />
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/ITreeEvaluator.cs

    r2222 r2285  
    2626namespace HeuristicLab.GP.StructureIdentification {
    2727  public interface ITreeEvaluator : IItem {
    28     void PrepareForEvaluation(Dataset dataset, int targetVariable, int start, int end, double punishmentFactor, IFunctionTree functionTree);
     28    void PrepareForEvaluation(Dataset dataset, int targetVariable, int start, int end, IFunctionTree functionTree);
     29    void PrepareForEvaluation(Dataset dataset, IFunctionTree functionTree);
    2930    double Evaluate(int sampleIndex);
    3031  }
  • TabularUnified trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/StandardGP.cs

    r2222 r2285  
    110110      FullTreeShakingFactor = 0.1;
    111111      OnePointShakingFactor = 1.0;
    112       PunishmentFactor = 10.0;
    113112      UseEstimatedTargetValue = false;
    114113      SetSeedRandomly = true;
     
    337336    }
    338337
    339     protected internal override Model CreateGPModel(IScope bestModelScope) {
    340       Model model = base.CreateGPModel(bestModelScope);
     338    protected internal override IAnalyzerModel CreateGPModel(IScope bestModelScope) {
     339      IAnalyzerModel model = base.CreateGPModel(bestModelScope);
    341340      model.TestMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("TestQuality", false).Data;
    342341      model.TrainingCoefficientOfDetermination = bestModelScope.GetVariableValue<DoubleData>("TrainingR2", false).Data;
Note: See TracChangeset for help on using the changeset viewer.