Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/10/09 15:50:02 (15 years ago)
Author:
gkronber
Message:

Added a method to persist HL.Model.IModel instances. #712

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DatabaseService.cs

    r2270 r2271  
    6969    public void Persist(HeuristicLab.Modeling.IAlgorithm algorithm) {
    7070      GetOrCreateProblem(algorithm.Dataset);
     71      Persist(algorithm.Model, algorithm.Name, algorithm.Description);
     72    }
     73
     74    public void Persist(HeuristicLab.Modeling.IModel model, string algorithmName, string algorithmDescription) {
    7175      Dictionary<string, Variable> variables = GetAllVariables();
    72       Algorithm algo = GetOrCreateAlgorithm(algorithm.Name, algorithm.Description);
    73       Variable target = variables[algorithm.Model.TargetVariable];
    74       Model model;
    75 
    76       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    77         model = new Model(target, algo);
    78         model.TrainingSamplesStart = algorithm.Model.TrainingSamplesStart;
    79         model.TrainingSamplesEnd = algorithm.Model.TrainingSamplesEnd;
    80         model.ValidationSamplesStart = algorithm.Model.ValidationSamplesStart;
    81         model.ValidationSamplesEnd = algorithm.Model.ValidationSamplesEnd;
    82         model.TestSamplesStart = algorithm.Model.TestSamplesStart;
    83         model.TestSamplesEnd = algorithm.Model.TestSamplesEnd;
    84 
    85         ctx.Models.InsertOnSubmit(model);
    86 
    87         ctx.SubmitChanges();
    88       }
    89 
    90       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    91         ctx.ModelData.InsertOnSubmit(new ModelData(model, PersistenceManager.SaveToGZip(algorithm.Model.Data)));
    92       }
    93 
    94       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    95         foreach (string inputVariable in algorithm.Model.InputVariables) {
    96           ctx.InputVariables.InsertOnSubmit(new InputVariable(model, variables[inputVariable]));
     76      Algorithm algo = GetOrCreateAlgorithm(algorithmName, algorithmDescription);
     77      Variable target = variables[model.TargetVariable];
     78      Model m;
     79
     80      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     81        m = new Model(target, algo);
     82        m.TrainingSamplesStart = model.TrainingSamplesStart;
     83        m.TrainingSamplesEnd = model.TrainingSamplesEnd;
     84        m.ValidationSamplesStart = model.ValidationSamplesStart;
     85        m.ValidationSamplesEnd = model.ValidationSamplesEnd;
     86        m.TestSamplesStart = model.TestSamplesStart;
     87        m.TestSamplesEnd = model.TestSamplesEnd;
     88
     89        ctx.Models.InsertOnSubmit(m);
     90
     91        ctx.SubmitChanges();
     92      }
     93
     94      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     95        ctx.ModelData.InsertOnSubmit(new ModelData(m, PersistenceManager.SaveToGZip(model.Data)));
     96      }
     97
     98      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     99        foreach (string inputVariable in model.InputVariables) {
     100          ctx.InputVariables.InsertOnSubmit(new InputVariable(m, variables[inputVariable]));
    97101        }
    98102        ctx.SubmitChanges();
     
    101105      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    102106        //get all double properties to save as modelResult
    103         IEnumerable<PropertyInfo> modelResultInfos = algorithm.Model.GetType().GetProperties().Where(
     107        IEnumerable<PropertyInfo> modelResultInfos = model.GetType().GetProperties().Where(
    104108          info => info.PropertyType == typeof(double));
    105109        foreach (PropertyInfo modelResultInfo in modelResultInfos) {
    106110          Result result = GetOrCreateResult(modelResultInfo.Name);
    107           double value = (double)modelResultInfo.GetValue(algorithm.Model, null);
    108           ctx.ModelResults.InsertOnSubmit(new ModelResult(model, result, value));
    109         }
    110         ctx.SubmitChanges();
    111       }
    112 
    113       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    114         IEnumerable<MethodInfo> inputVariableResultInfos = algorithm.Model.GetType().GetMethods().Where(
     111          double value = (double)modelResultInfo.GetValue(model, null);
     112          ctx.ModelResults.InsertOnSubmit(new ModelResult(m, result, value));
     113        }
     114        ctx.SubmitChanges();
     115      }
     116
     117      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     118        IEnumerable<MethodInfo> inputVariableResultInfos = model.GetType().GetMethods().Where(
    115119          info => info.GetParameters().Count() == 1 &&
    116120             info.GetParameters()[0].ParameterType == typeof(string) &&
     
    121125          Result result = GetOrCreateResult(inputVariableResultInfo.Name.Substring(3));
    122126          foreach (InputVariable variable in ctx.InputVariables.Where(iv => iv.Model == model)) {
    123             double value = (double)inputVariableResultInfo.Invoke(algorithm.Model, new object[] { variable.Variable.Name });
     127            double value = (double)inputVariableResultInfo.Invoke(model, new object[] { variable.Variable.Name });
    124128            ctx.InputVariableResults.InsertOnSubmit(new InputVariableResult(variable, result, value));
    125129          }
     
    127131        ctx.SubmitChanges();
    128132      }
    129 
    130133    }
    131134
Note: See TracChangeset for help on using the changeset viewer.