Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/13/09 17:28:07 (15 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/BaseClasses
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • 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);
  • 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}
Note: See TracChangeset for help on using the changeset viewer.