Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/tools/CedmaDatabaseMerger/MergerForm.cs @ 8840

Last change on this file since 8840 was 2285, checked in by gkronber, 15 years ago

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).

File size: 4.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using HeuristicLab.Modeling.Database.SQLServerCompact;
10using HeuristicLab.Modeling;
11using HeuristicLab.GP;
12using HeuristicLab.Core;
13using HeuristicLab.Modeling.Database;
14using System.Reflection;
15
16namespace CedmaDatabaseMerger {
17  public partial class MergerForm : Form {
18    string destinationFile;
19    public MergerForm() {
20      InitializeComponent();
21    }
22
23    private void setOutputButton_Click(object sender, EventArgs e) {
24      using (OpenFileDialog dialog = new OpenFileDialog()) {
25        DialogResult result = dialog.ShowDialog();
26        if (result == DialogResult.OK) {
27          outputTextBox.Text = dialog.FileName;
28          destinationFile = dialog.FileName;
29          importButton.Enabled = true;
30        } else {
31          outputTextBox.Text = string.Empty;
32          destinationFile = string.Empty;
33          importButton.Enabled = false;
34        }
35      }
36    }
37
38    private void importButton_Click(object sender, EventArgs e) {
39      string importFileName;
40      using (OpenFileDialog dialog = new OpenFileDialog()) {
41        DialogResult result = dialog.ShowDialog();
42        if (result == DialogResult.OK) {
43          importFileName = dialog.FileName;
44        } else {
45          importFileName = string.Empty;
46        }
47      }
48      if (!string.IsNullOrEmpty(importFileName)) {
49        importButton.Enabled = false;
50        BackgroundWorker worker = new BackgroundWorker();
51        worker.WorkerReportsProgress = true;
52        worker.DoWork += CreateDoWorkDelegate(worker, importFileName);
53        worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
54        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
55        worker.RunWorkerAsync();
56      }
57    }
58
59    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
60      importButton.Enabled = true;
61      importProgressBar.Value = 0;
62    }
63
64    void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
65      importProgressBar.Value = e.ProgressPercentage;
66    }
67
68    private DoWorkEventHandler CreateDoWorkDelegate(BackgroundWorker worker, string importFileName) {
69      return (sender, args) => {
70        DatabaseService destiationDatabase = new DatabaseService("Data Source=" + destinationFile);
71        DatabaseService sourceDatabase = new DatabaseService("Data Source=" + importFileName);
72
73        sourceDatabase.Connect();
74        var models = sourceDatabase.GetAllModels();
75        var sourceDataset = sourceDatabase.GetDataset();
76        int importCount = 0;
77        foreach (HeuristicLab.Modeling.Database.IModel m in models) {
78
79          HeuristicLab.Modeling.IAnalyzerModel model = new AnalyzerModel();
80          model.Predictor = (HeuristicLab.Modeling.IPredictor)PersistenceManager.RestoreFromGZip(sourceDatabase.GetModelData(m));
81          model.TargetVariable = m.TargetVariable.Name;
82          model.Dataset = sourceDataset;
83          model.TrainingSamplesStart = m.TrainingSamplesStart;
84          model.TrainingSamplesEnd = m.TrainingSamplesEnd;
85          model.ValidationSamplesStart = m.ValidationSamplesStart;
86          model.ValidationSamplesEnd = m.ValidationSamplesEnd;
87          model.TestSamplesStart = m.TestSamplesStart;
88          model.TestSamplesEnd = m.TestSamplesEnd;
89          model.Predictor.Predict(sourceDataset, 10, 20);
90          //get all double properties to save as modelResult
91          IEnumerable<PropertyInfo> modelResultInfos = model.GetType().GetProperties().Where(
92            info => info.PropertyType == typeof(double));
93          var modelResults = sourceDatabase.GetModelResults(m);
94          foreach (IModelResult result in modelResults) {
95            PropertyInfo matchingPropInfo = modelResultInfos.First(x => x.Name == result.Result.Name);
96            if (matchingPropInfo != null) matchingPropInfo.SetValue(model, result.Value, null);
97          }
98          var inputVariableResults = sourceDatabase.GetInputVariableResults(m);
99          foreach (IInputVariableResult result in inputVariableResults) {
100            model.AddInputVariable(result.Variable.Name);
101            if (result.Result.Name == "VariableEvaluationImpact") {
102              model.SetVariableEvaluationImpact(result.Variable.Name, result.Value);
103            } else if (result.Result.Name == "VariableQualityImpact") {
104              model.SetVariableQualityImpact(result.Variable.Name, result.Value);
105            } else throw new FormatException();
106          }
107
108          destiationDatabase.Persist(model, m.Algorithm.Name, m.Algorithm.Description);
109          worker.ReportProgress((++importCount * 100) / models.Count());
110        }
111        sourceDatabase.Disconnect();
112      };
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.