Index: /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj
===================================================================
--- /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj	(revision 17049)
+++ /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj	(revision 17050)
@@ -391,4 +391,5 @@
     <Compile Include="Plugin.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="RandomForest\ModelCreation.cs" />
     <Compile Include="RandomForest\RandomForestClassificationSolution.cs" />
     <Compile Include="RandomForest\RandomForestClassification.cs" />
Index: /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/ModelCreation.cs
===================================================================
--- /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/ModelCreation.cs	(revision 17050)
+++ /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/ModelCreation.cs	(revision 17050)
@@ -0,0 +1,39 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ * and the BEACON Center for the Study of Evolution in Action.
+ * 
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using HEAL.Attic;
+
+namespace HeuristicLab.Algorithms.DataAnalysis.RandomForest {
+
+  /// <summary>
+  /// Defines what part of the Model should be stored.
+  /// QualityOnly - no solution will be created.
+  /// SurrogateModel - only the parameters will be stored, the model is calculated during deserialization
+  /// Model - the complete model will be stored (consider the amount of memory needed)
+  /// </summary>
+  [StorableType("3869899B-1848-4628-AF27-3B6FDE5840D6")]
+  public enum ModelCreation {
+    QualityOnly,
+    SurrogateModel,
+    Model
+  }
+}
Index: /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestClassification.cs
===================================================================
--- /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestClassification.cs	(revision 17049)
+++ /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestClassification.cs	(revision 17050)
@@ -20,6 +20,9 @@
 #endregion
 
+using System.Collections.Generic;
+using System.Linq;
 using System.Threading;
 using HEAL.Attic;
+using HeuristicLab.Algorithms.DataAnalysis.RandomForest;
 using HeuristicLab.Common;
 using HeuristicLab.Core;
@@ -43,5 +46,5 @@
     private const string SeedParameterName = "Seed";
     private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
-    private const string CreateSolutionParameterName = "CreateSolution";
+    private const string ModelCreationParameterName = "ModelCreation";
 
     #region parameter properties
@@ -61,6 +64,6 @@
       get { return (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
     }
-    public IFixedValueParameter<BoolValue> CreateSolutionParameter {
-      get { return (IFixedValueParameter<BoolValue>)Parameters[CreateSolutionParameterName]; }
+    private IFixedValueParameter<EnumValue<ModelCreation>> ModelCreationParameter {
+      get { return (IFixedValueParameter<EnumValue<ModelCreation>>)Parameters[ModelCreationParameterName]; }
     }
     #endregion
@@ -86,7 +89,7 @@
       set { SetSeedRandomlyParameter.Value.Value = value; }
     }
-    public bool CreateSolution {
-      get { return CreateSolutionParameter.Value.Value; }
-      set { CreateSolutionParameter.Value.Value = value; }
+    public ModelCreation ModelCreation {
+      get { return ModelCreationParameter.Value.Value; }
+      set { ModelCreationParameter.Value.Value = value; }
     }
     #endregion
@@ -105,6 +108,6 @@
       Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
       Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
-      Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));
-      Parameters[CreateSolutionParameterName].Hidden = true;
+      Parameters.Add(new FixedValueParameter<EnumValue<ModelCreation>>(ModelCreationParameterName, "Defines the results produced at the end of the run (Surrogate => Less disk space, lazy recalculation of model)", new EnumValue<ModelCreation>(ModelCreation.Model)));
+      Parameters[ModelCreationParameterName].Hidden = true;
 
       Problem = new ClassificationProblem();
@@ -121,7 +124,13 @@
       if (!Parameters.ContainsKey((SetSeedRandomlyParameterName)))
         Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
-      if (!Parameters.ContainsKey(CreateSolutionParameterName)) {
-        Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));
-        Parameters[CreateSolutionParameterName].Hidden = true;
+
+      // parameter type has been changed
+      if (Parameters.ContainsKey("CreateSolution")) {
+        var createSolutionParam = Parameters["CreateSolution"] as FixedValueParameter<BoolValue>;
+        Parameters.Remove(createSolutionParam);
+
+        ModelCreation value = createSolutionParam.Value.Value ? ModelCreation.Model : ModelCreation.QualityOnly;
+        Parameters.Add(new FixedValueParameter<EnumValue<ModelCreation>>(ModelCreationParameterName, "Defines the results produced at the end of the run (Surrogate => Less disk space, lazy recalculation of model)", new EnumValue<ModelCreation>(value)));
+        Parameters[ModelCreationParameterName].Hidden = true;
       }
       #endregion
@@ -138,4 +147,5 @@
 
       var model = CreateRandomForestClassificationModel(Problem.ProblemData, NumberOfTrees, R, M, Seed, out rmsError, out relClassificationError, out outOfBagRmsError, out outOfBagRelClassificationError);
+
       Results.Add(new Result("Root mean square error", "The root of the mean of squared errors of the random forest regression solution on the training set.", new DoubleValue(rmsError)));
       Results.Add(new Result("Relative classification error", "Relative classification error of the random forest regression solution on the training set.", new PercentValue(relClassificationError)));
@@ -143,6 +153,16 @@
       Results.Add(new Result("Relative classification error (out-of-bag)", "The out-of-bag relative classification error  of the random forest regression solution.", new PercentValue(outOfBagRelClassificationError)));
 
-      if (CreateSolution) {
-        var solution = model.CreateClassificationSolution(Problem.ProblemData);
+
+      IClassificationSolution solution = null;
+      if (ModelCreation == ModelCreation.Model) {
+        solution = model.CreateClassificationSolution(Problem.ProblemData);
+      } else if (ModelCreation == ModelCreation.SurrogateModel) {
+        var problemData = Problem.ProblemData;
+        var surrogateModel = new RandomForestModelSurrogate(model, problemData.TargetVariable, problemData, Seed, NumberOfTrees, R, M, problemData.ClassValues.ToArray());
+
+        solution = surrogateModel.CreateClassificationSolution(problemData);
+      }
+
+      if (solution != null) {
         Results.Add(new Result(RandomForestClassificationModelResultName, "The random forest classification solution.", solution));
       }
@@ -157,8 +177,40 @@
     }
 
-    public static RandomForestModel CreateRandomForestClassificationModel(IClassificationProblemData problemData, int nTrees, double r, double m, int seed,
+    public static RandomForestModelFull CreateRandomForestClassificationModel(IClassificationProblemData problemData, int nTrees, double r, double m, int seed,
+ out double rmsError, out double avgRelError, out double outOfBagRmsError, out double outOfBagAvgRelError) {
+      var model = CreateRandomForestClassificationModel(problemData, problemData.TrainingIndices, nTrees, r, m, seed, out rmsError, out avgRelError, out outOfBagRmsError, out outOfBagAvgRelError);
+      return model;
+    }
+
+    public static RandomForestModelFull CreateRandomForestClassificationModel(IClassificationProblemData problemData, IEnumerable<int> trainingIndices, int nTrees, double r, double m, int seed,
       out double rmsError, out double relClassificationError, out double outOfBagRmsError, out double outOfBagRelClassificationError) {
-      return RandomForestModel.CreateClassificationModel(problemData, nTrees, r, m, seed,
-       rmsError: out rmsError, relClassificationError: out relClassificationError, outOfBagRmsError: out outOfBagRmsError, outOfBagRelClassificationError: out outOfBagRelClassificationError);
+
+      var variables = problemData.AllowedInputVariables.Concat(new string[] { problemData.TargetVariable });
+      double[,] inputMatrix = problemData.Dataset.ToArray(variables, trainingIndices);
+
+      var classValues = problemData.ClassValues.ToArray();
+      int nClasses = classValues.Length;
+
+      // map original class values to values [0..nClasses-1]
+      var classIndices = new Dictionary<double, double>();
+      for (int i = 0; i < nClasses; i++) {
+        classIndices[classValues[i]] = i;
+      }
+
+      int nRows = inputMatrix.GetLength(0);
+      int nColumns = inputMatrix.GetLength(1);
+      for (int row = 0; row < nRows; row++) {
+        inputMatrix[row, nColumns - 1] = classIndices[inputMatrix[row, nColumns - 1]];
+      }
+
+      alglib.dfreport rep;
+      var dForest = RandomForestUtil.CreateRandomForestModel(seed, inputMatrix, nTrees, r, m, nClasses, out rep);
+
+      rmsError = rep.rmserror;
+      outOfBagRmsError = rep.oobrmserror;
+      relClassificationError = rep.relclserror;
+      outOfBagRelClassificationError = rep.oobrelclserror;
+
+      return new RandomForestModelFull(dForest, problemData.TargetVariable, problemData.AllowedInputVariables, classValues);
     }
     #endregion
Index: /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModel.cs
===================================================================
--- /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModel.cs	(revision 17049)
+++ /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModel.cs	(revision 17050)
@@ -34,5 +34,5 @@
   /// Represents a random forest model for regression and classification
   /// </summary>
-  [Obsolete("This class only exists for backwards compatibility reasons. Use RFModelSurrogate or RFModelFull instead.")]
+  [Obsolete("This class only exists for backwards compatibility reasons for stored models with the XML Persistence. Use RFModelSurrogate or RFModelFull instead.")]
   [StorableType("9AA4CCC2-CD75-4471-8DF6-949E5B783642")]
   [Item("RandomForestModel", "Represents a random forest for regression and classification.")]
Index: /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModelFull.cs
===================================================================
--- /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModelFull.cs	(revision 17049)
+++ /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModelFull.cs	(revision 17050)
@@ -100,4 +100,7 @@
 
     public RandomForestModelFull(alglib.decisionforest decisionForest, string targetVariable, IEnumerable<string> inputVariables, IEnumerable<double> classValues = null) : base(targetVariable) {
+      this.name = ItemName;
+      this.description = ItemDescription;
+
       randomForest = decisionForest;
 
Index: /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModelSurrogate.cs
===================================================================
--- /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModelSurrogate.cs	(revision 17049)
+++ /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModelSurrogate.cs	(revision 17050)
@@ -115,14 +115,13 @@
       IRandomForestModel randomForestModel = null;
 
-      //TODO Refactor to new methods
       double rmsError, oobRmsError, relClassError, oobRelClassError;
       var classificationProblemData = originalTrainingData as IClassificationProblemData;
 
       if (originalTrainingData is IRegressionProblemData regressionProblemData) {
-        randomForestModel = RandomForestModel.CreateRegressionModel(regressionProblemData,
+        randomForestModel = RandomForestRegression.CreateRandomForestRegressionModel(regressionProblemData,
                                               nTrees, r, m, seed, out rmsError, out oobRmsError,
                                               out relClassError, out oobRelClassError);
       } else if (classificationProblemData != null) {
-        randomForestModel = RandomForestModel.CreateClassificationModel(classificationProblemData,
+        randomForestModel = RandomForestClassification.CreateRandomForestClassificationModel(classificationProblemData,
                                               nTrees, r, m, seed, out rmsError, out oobRmsError,
                                               out relClassError, out oobRelClassError);
Index: /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestRegression.cs
===================================================================
--- /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestRegression.cs	(revision 17049)
+++ /branches/2952_RF-ModelStorage/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestRegression.cs	(revision 17050)
@@ -24,4 +24,5 @@
 using System.Threading;
 using HEAL.Attic;
+using HeuristicLab.Algorithms.DataAnalysis.RandomForest;
 using HeuristicLab.Common;
 using HeuristicLab.Core;
@@ -45,5 +46,5 @@
     private const string SeedParameterName = "Seed";
     private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
-    private const string CreateSolutionParameterName = "CreateSolution";
+    private const string ModelCreationParameterName = "ModelCreation";
 
     #region parameter properties
@@ -63,6 +64,6 @@
       get { return (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
     }
-    public IFixedValueParameter<BoolValue> CreateSolutionParameter {
-      get { return (IFixedValueParameter<BoolValue>)Parameters[CreateSolutionParameterName]; }
+    private IFixedValueParameter<EnumValue<ModelCreation>> ModelCreationParameter {
+      get { return (IFixedValueParameter<EnumValue<ModelCreation>>)Parameters[ModelCreationParameterName]; }
     }
     #endregion
@@ -88,7 +89,7 @@
       set { SetSeedRandomlyParameter.Value.Value = value; }
     }
-    public bool CreateSolution {
-      get { return CreateSolutionParameter.Value.Value; }
-      set { CreateSolutionParameter.Value.Value = value; }
+    public ModelCreation ModelCreation {
+      get { return ModelCreationParameter.Value.Value; }
+      set { ModelCreationParameter.Value.Value = value; }
     }
     #endregion
@@ -106,6 +107,6 @@
       Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
       Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
-      Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));
-      Parameters[CreateSolutionParameterName].Hidden = true;
+      Parameters.Add(new FixedValueParameter<EnumValue<ModelCreation>>(ModelCreationParameterName, "Defines the results produced at the end of the run (Surrogate => Less disk space, lazy recalculation of model)", new EnumValue<ModelCreation>(ModelCreation.Model)));
+      Parameters[ModelCreationParameterName].Hidden = true;
 
       Problem = new RegressionProblem();
@@ -122,7 +123,13 @@
       if (!Parameters.ContainsKey((SetSeedRandomlyParameterName)))
         Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
-      if (!Parameters.ContainsKey(CreateSolutionParameterName)) {
-        Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));
-        Parameters[CreateSolutionParameterName].Hidden = true;
+
+      // parameter type has been changed
+      if (Parameters.ContainsKey("CreateSolution")) {
+        var createSolutionParam = Parameters["CreateSolution"] as FixedValueParameter<BoolValue>;
+        Parameters.Remove(createSolutionParam);
+
+        ModelCreation value = createSolutionParam.Value.Value ? ModelCreation.Model : ModelCreation.QualityOnly;
+        Parameters.Add(new FixedValueParameter<EnumValue<ModelCreation>>(ModelCreationParameterName, "Defines the results produced at the end of the run (Surrogate => Less disk space, lazy recalculation of model)", new EnumValue<ModelCreation>(value)));
+        Parameters[ModelCreationParameterName].Hidden = true;
       }
       #endregion
@@ -145,6 +152,14 @@
       Results.Add(new Result("Average relative error (out-of-bag)", "The out-of-bag average of relative errors of the random forest regression solution.", new PercentValue(outOfBagAvgRelError)));
 
-      if (CreateSolution) {
-        var solution = model.CreateRegressionSolution(Problem.ProblemData);
+      IRegressionSolution solution = null;
+      if (ModelCreation == ModelCreation.Model) {
+        solution = model.CreateRegressionSolution(Problem.ProblemData);
+      } else if (ModelCreation == ModelCreation.SurrogateModel) {
+        var problemData = Problem.ProblemData;
+        var surrogateModel = new RandomForestModelSurrogate(model, problemData.TargetVariable, problemData, Seed, NumberOfTrees, R, M);
+        solution = surrogateModel.CreateRegressionSolution(problemData);
+      }
+
+      if (solution != null) {
         Results.Add(new Result(RandomForestRegressionModelResultName, "The random forest regression solution.", solution));
       }
@@ -163,5 +178,6 @@
      double r, double m, int seed,
      out double rmsError, out double avgRelError, out double outOfBagRmsError, out double outOfBagAvgRelError) {
-      return CreateRandomForestRegressionModel(problemData, problemData.TrainingIndices, nTrees, r, m, seed, out rmsError, out avgRelError, out outOfBagRmsError, out outOfBagAvgRelError);
+      var model = CreateRandomForestRegressionModel(problemData, problemData.TrainingIndices, nTrees, r, m, seed, out rmsError, out avgRelError, out outOfBagRmsError, out outOfBagAvgRelError);
+      return model;
     }
 
@@ -181,7 +197,4 @@
 
       return new RandomForestModelFull(dForest, problemData.TargetVariable, problemData.AllowedInputVariables);
-
-      //return RandomForestModel.CreateRegressionModel(problemData, nTrees, r, m, seed,
-      //rmsError: out rmsError, avgRelError: out avgRelError, outOfBagRmsError: out outOfBagRmsError, outOfBagAvgRelError: out outOfBagAvgRelError);
     }
 
