Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1906


Ignore:
Timestamp:
05/27/09 13:22:41 (16 years ago)
Author:
gkronber
Message:

Added interface IModel and a standard implementation and changed SVM models to include the range transform in the model. #650 (IAlgorithm and derived interfaces should provide properties to retrieve results)

Location:
trunk/sources
Files:
2 added
9 edited

Legend:

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

    r1873 r1906  
    7070    }
    7171
     72    public virtual IModel Model {
     73      get {
     74        if (!engine.Terminated) throw new InvalidOperationException("The algorithm is still running. Wait until the algorithm is terminated to retrieve the result.");
     75        IScope bestModelScope = engine.GlobalScope.GetVariableValue<IScope>("BestValidationSolution", false);
     76        return CreateGPModel(bestModelScope);
     77      }
     78    }
     79
    7280    public virtual int Elites {
    7381      get { return GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data; }
     
    405413    }
    406414
     415    protected internal virtual Model CreateGPModel(IScope bestModelScope) {
     416      Model model = new Model();
     417      Dataset ds = bestModelScope.GetVariableValue<Dataset>("Dataset", true);
     418      model.Data = bestModelScope.GetVariableValue<IFunctionTree>("FunctionTree", false);
     419      model.Dataset = ds;
     420      model.TargetVariable = ds.GetVariableName(bestModelScope.GetVariableValue<IntData>("TargetVariable", true).Data);
     421      model.TrainingMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("Quality", false).Data;
     422      model.ValidationMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("ValidationQuality", false).Data;
     423      return model;
     424    }
     425
    407426    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    408427      AlgorithmBase clone = (AlgorithmBase)base.Clone(clonedObjects);
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/StandardGP.cs

    r1890 r1906  
    312312    }
    313313
     314    protected internal override Model CreateGPModel(IScope bestModelScope) {
     315      Model model = base.CreateGPModel(bestModelScope);
     316      model.TestMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("TestQuality", false).Data;
     317      return model;
     318    }
     319
    314320    public virtual IEditor CreateEditor() {
    315321      return new StandardGpEditor(this);
  • trunk/sources/HeuristicLab.Modeling/3.2/HeuristicLab.Modeling-3.2.csproj

    r1888 r1906  
    8383  <ItemGroup>
    8484    <Compile Include="ClassificationProblemInjector.cs" />
     85    <Compile Include="Model.cs" />
     86    <Compile Include="IModel.cs" />
    8587    <Compile Include="SimpleEvaluatorBase.cs" />
    8688    <Compile Include="ITimeSeriesAlgorithm.cs" />
  • trunk/sources/HeuristicLab.Modeling/3.2/IAlgorithm.cs

    r1857 r1906  
    3131    IOperator ProblemInjector { get; set; }
    3232    IEngine Engine { get; }
     33
     34    IModel Model { get; }
    3335  }
    3436}
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/HeuristicLab.SupportVectorMachines-3.2.csproj

    r1869 r1906  
    8282  <ItemGroup>
    8383    <Compile Include="SupportVectorRegression.cs" />
    84     <Compile Include="SVMRangeTransform.cs" />
    8584    <Compile Include="SVMModel.cs" />
    8685    <Compile Include="HeuristicLabSupportVectorMachinesPlugin.cs" />
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SVMModel.cs

    r1837 r1906  
    2929
    3030namespace HeuristicLab.Data {
    31   public class SVMModel : ObjectData {
     31  public class SVMModel : ItemBase {
     32    private SVM.Model model;
    3233    /// <summary>
    3334    /// Gets or sets the SVM model.
    3435    /// </summary>
    35     /// <remarks>Uses property <see cref="ObjectData.Data"/> of base class <see cref="ObjectData"></see>.
    36     /// No own data storage present.</remarks>
    37     public new SVM.Model Data {
    38       get { return (SVM.Model)base.Data; }
    39       set { base.Data = value; }
     36    public SVM.Model Model {
     37      get { return model; }
     38      set { model = value; }
     39    }
     40
     41
     42    /// <summary>
     43    /// Gets or sets the range transformation for the model.
     44    /// </summary>
     45    private SVM.RangeTransform rangeTransform;
     46    public SVM.RangeTransform RangeTransform {
     47      get { return rangeTransform; }
     48      set { rangeTransform = value; }
    4049    }
    4150
     
    4958      clonedObjects.Add(Guid, clone);
    5059      // beware we are only using a shallow copy here! (gkronber)
    51       clone.Data = Data;
     60      clone.Model = Model;
     61      clone.RangeTransform = RangeTransform;
    5262      return clone;
    5363    }
     
    6474    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    6575      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    66       XmlNode data = document.CreateElement("Data");
    67 
     76      XmlNode model = document.CreateElement("Model");
    6877      using (MemoryStream stream = new MemoryStream()) {
    69         SVM.Model.Write(stream, Data);
     78        SVM.Model.Write(stream, Model);
    7079        stream.Seek(0, System.IO.SeekOrigin.Begin);
    7180        StreamReader reader = new StreamReader(stream);
    72         data.InnerText = reader.ReadToEnd();
    73         node.AppendChild(data);
     81        model.InnerText = reader.ReadToEnd();
     82        node.AppendChild(model);
    7483      }
     84
     85      XmlNode rangeTransform = document.CreateElement("RangeTransform");
     86      using (MemoryStream stream = new MemoryStream()) {
     87        SVM.RangeTransform.Write(stream, RangeTransform);
     88        stream.Seek(0, System.IO.SeekOrigin.Begin);
     89        StreamReader reader = new StreamReader(stream);
     90        rangeTransform.InnerText = reader.ReadToEnd();
     91        node.AppendChild(rangeTransform);
     92      }
     93
    7594      return node;
    7695    }
     
    84103    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    85104      base.Populate(node, restoredObjects);
    86       XmlNode data = node.SelectSingleNode("Data");
    87       using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(data.InnerText))) {
    88         Data = SVM.Model.Read(stream);
     105      XmlNode model = node.SelectSingleNode("Model");
     106      using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(model.InnerText))) {
     107        Model = SVM.Model.Read(stream);
     108      }
     109      XmlNode rangeTransform = node.SelectSingleNode("RangeTransform");
     110      using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(rangeTransform.InnerText))) {
     111        RangeTransform = SVM.RangeTransform.Read(stream);
    89112      }
    90113    }
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorCreator.cs

    r1851 r1906  
    5353      AddVariableInfo(new VariableInfo("SVMGamma", "Gamma parameter in kernel function", typeof(DoubleData), VariableKind.In));
    5454      AddVariableInfo(new VariableInfo("SVMModel", "Represent the model learned by the SVM", typeof(SVMModel), VariableKind.New | VariableKind.Out));
    55       AddVariableInfo(new VariableInfo("SVMRangeTransform", "The applied transformation during the learning the model", typeof(SVMRangeTransform), VariableKind.New | VariableKind.Out));
     55//    AddVariableInfo(new VariableInfo("SVMRangeTransform", "The applied transformation during the learning the model", typeof(SVMRangeTransform), VariableKind.New | VariableKind.Out));
    5656    }
    5757
     
    9292        //persist variables in scope
    9393        SVMModel modelData = new SVMModel();
    94         modelData.Data = model;
     94        modelData.Model = model;
     95        modelData.RangeTransform = rangeTransform;
    9596        scope.AddVariable(new Variable(scope.TranslateName("SVMModel"), modelData));
    96         SVMRangeTransform rangeTransformData = new SVMRangeTransform();
    97         rangeTransformData.Data = rangeTransform;
    98         scope.AddVariable(new Variable(scope.TranslateName("SVMRangeTransform"), rangeTransformData));
     97        //SVMRangeTransform rangeTransformData = new SVMRangeTransform();
     98        //rangeTransformData.Data = rangeTransform;
     99        //scope.AddVariable(new Variable(scope.TranslateName("SVMRangeTransform"), rangeTransformData));
    99100        return null;
    100101      } else {
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorEvaluator.cs

    r1870 r1906  
    4141
    4242      AddVariableInfo(new VariableInfo("SVMModel", "Represent the model learned by the SVM", typeof(SVMModel), VariableKind.In));
    43       AddVariableInfo(new VariableInfo("SVMRangeTransform", "The applied transformation during the learning the model", typeof(SVMRangeTransform), VariableKind.In));
     43      // AddVariableInfo(new VariableInfo("SVMRangeTransform", "The applied transformation during the learning the model", typeof(SVMRangeTransform), VariableKind.In));
    4444
    4545      AddVariableInfo(new VariableInfo("Values", "Target vs predicted values", typeof(DoubleMatrixData), VariableKind.New | VariableKind.Out));
     
    5454      int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
    5555
    56       SVM.Model model = GetVariableValue<SVMModel>("SVMModel", scope, true).Data;
    57       SVM.RangeTransform rangeTransform = GetVariableValue<SVMRangeTransform>("SVMRangeTransform", scope, true).Data;
     56      SVMModel modelData = GetVariableValue<SVMModel>("SVMModel", scope, true);
     57      // SVM.RangeTransform rangeTransform = GetVariableValue<SVMRangeTransform>("SVMRangeTransform", scope, true).Data;
    5858
    5959      SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, allowedFeatures, targetVariable, start, end);
    60       SVM.Problem scaledProblem = SVM.Scaling.Scale(problem, rangeTransform);
     60      SVM.Problem scaledProblem = SVM.Scaling.Scale(problem, modelData.RangeTransform);
    6161
    6262      double[,] values = new double[end-start, 2];
    6363      for (int i = 0; i < end - start; i++) {
    64         values[i,0] = SVM.Prediction.Predict(model, scaledProblem.X[i]);
     64        values[i,0] = SVM.Prediction.Predict(modelData.Model, scaledProblem.X[i]);
    6565        values[i,1] = dataset.Samples[(start + i) * dataset.Columns + targetVariable];
    6666      }
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorRegression.cs

    r1889 r1906  
    5858    }
    5959
     60    public IModel Model {
     61      get {
     62        if (!engine.Terminated) throw new InvalidOperationException("The algorithm is still running. Wait until the algorithm is terminated to retrieve the result.");
     63        IScope bestModelScope = engine.GlobalScope.GetVariableValue<IScope>("BestValidationSolution", false);
     64        return CreateSVMModel(bestModelScope);
     65      }
     66    }
     67
    6068    public DoubleArrayData NuList {
    6169      get { return GetVariableInjector().GetVariable("NuList").GetValue<DoubleArrayData>(); }
     
    127135      modelCreator.GetVariableInfo("SVMModel").ActualName = "Model";
    128136      modelCreator.GetVariableInfo("SVMNu").ActualName = "Nu";
    129       modelCreator.GetVariableInfo("SVMRangeTransform").ActualName = "RangeTransform";
    130137      modelCreator.GetVariableInfo("SVMType").ActualName = "Type";
    131 
    132138
    133139      modelProcessor.AddSubOperator(modelCreator);
     
    196202      evaluator.Name = p + "SimpleEvaluator";
    197203      evaluator.GetVariableInfo("SVMModel").ActualName = "Model";
    198       evaluator.GetVariableInfo("SVMRangeTransform").ActualName = "RangeTransform";
    199204      evaluator.GetVariableInfo("SamplesStart").ActualName = p + "SamplesStart";
    200205      evaluator.GetVariableInfo("SamplesEnd").ActualName = p + "SamplesEnd";
     
    277282    }
    278283
     284    protected internal virtual Model CreateSVMModel(IScope bestModelScope) {
     285      Model model = new Model();
     286      model.Data = bestModelScope.GetVariableValue<SVMModel>("BestValidationModel", false);
     287      return model;
     288    }
     289
    279290    private IOperator GetVariableInjector() {
    280291      return GetMainOperator().SubOperators[0];
Note: See TracChangeset for help on using the changeset viewer.