Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2925_AutoDiffForDynamicalModels/AutoDiffForDynamicalModelsTest/TestOdeIdentification.cs @ 17896

Last change on this file since 17896 was 16610, checked in by gkronber, 6 years ago

#2925: added support for multiple training episodes (and log, exp functions)

File size: 4.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Data;
4using HeuristicLab.Problems.DynamicalSystemsModelling;
5using HeuristicLab.Random;
6using Microsoft.VisualStudio.TestTools.UnitTesting;
7
8namespace AutoDiffForDynamicalModelsTest {
9  [TestClass]
10  public class TestOdeIdentification {
11    [TestMethod]
12    public void RunOdeIdentification() {
13      var alg = new OdeParameterIdentification();
14      var dynProb = new Problem();
15      var parser = new HeuristicLab.Problems.Instances.DataAnalysis.TableFileParser();
16      // var fileName = @"C:\reps\HEAL\EuroCAST - Kronberger\DataGeneration\test.csv";
17      var fileName = @"D:\heal\documents\trunk\Publications\2019\EuroCAST\Kronberger\DataGeneration\test.csv";
18      parser.Parse(fileName, true);
19      var prov = new HeuristicLab.Problems.Instances.DataAnalysis.RegressionCSVInstanceProvider();
20      var regressionProblemData = prov.ImportData(fileName);
21      regressionProblemData.TrainingPartition.Start = 0;
22      regressionProblemData.TrainingPartition.End = 40;
23      regressionProblemData.TestPartition.Start = 41;
24      var allowedInputs = new List<string>(); // empty
25      var targets = new List<string>(new[] { "x1", "x2", "v" });
26      foreach (var checkedItem in regressionProblemData.InputVariables) {
27        regressionProblemData.InputVariables.SetItemCheckedState(checkedItem, allowedInputs.Contains(checkedItem.Value));
28      }
29      dynProb.Load(regressionProblemData);
30
31      foreach (var checkedItem in dynProb.TargetVariables) {
32        dynProb.TargetVariables.SetItemCheckedState(checkedItem, targets.Contains(checkedItem.Value));
33      }
34
35      dynProb.TrainingEpisodesParameter.Value.Add(new IntRange(0, 40));
36      dynProb.NumericIntegrationStepsParameter.Value.Value = 1;
37
38      var rand = new FastRandom(1234);
39      var expressions = new string[] { "v", "-v", "1.0*(x2-x1)" };
40
41      alg.CreateSolution(dynProb, expressions, 200, rand);
42      Assert.AreEqual(6.8350792038369173E-20, ((DoubleValue)alg.Results["SNMSE"].Value).Value, 1E-8);
43    }
44    [TestMethod]
45    public void RunOdeIdentificationMultipleEpisodes() {
46      var alg = new OdeParameterIdentification();
47      var dynProb = new Problem();
48      var parser = new HeuristicLab.Problems.Instances.DataAnalysis.TableFileParser();
49      // var fileName = @"C:\reps\HEAL\EuroCAST - Kronberger\DataGeneration\test.csv";
50      var fileName = @"C:\reps\HEAL\EuroCAST - Kronberger\DataGeneration\s-system.csv";
51      parser.Parse(fileName, true);
52      var prov = new HeuristicLab.Problems.Instances.DataAnalysis.RegressionCSVInstanceProvider();
53      var regressionProblemData = prov.ImportData(fileName);
54      regressionProblemData.TrainingPartition.Start = 0;
55      regressionProblemData.TrainingPartition.End = 61;
56      regressionProblemData.TestPartition.Start = 61;
57      var allowedInputs = new List<string>(); // empty
58      var targets = new List<string>(new[] { "y1", "y2", "y3", "y4", "y5" });
59      foreach (var checkedItem in regressionProblemData.InputVariables) {
60        regressionProblemData.InputVariables.SetItemCheckedState(checkedItem, allowedInputs.Contains(checkedItem.Value));
61      }
62      dynProb.Load(regressionProblemData);
63
64      foreach (var checkedItem in dynProb.TargetVariables) {
65        dynProb.TargetVariables.SetItemCheckedState(checkedItem, targets.Contains(checkedItem.Value));
66      }
67
68      dynProb.TrainingEpisodesParameter.Value.Add(new IntRange(0, 30));
69      dynProb.TrainingEpisodesParameter.Value.Add(new IntRange(30, 61));
70      dynProb.NumericIntegrationStepsParameter.Value.Value = 1;
71
72      var rand = new FastRandom(1234);
73      var expressions = new string[] {
74"1.0*(y3*exp(log(y5)*0.1)) + 1.0*(y1*y1)",
75"1.0*(y1*y1) + 1.0*(y2*y2)",
76"1.0*exp(log(y2)*0.1) + 1.0*exp(log(y2)*0.1)*(y3*y3)",
77"1.0*(y1*y1)*exp(log(y5)*0.1) + 1.0*(y4*y4)",
78"1.0*(y4*y4)+1.0*(y5*y5)"
79      };
80
81      alg.CreateSolution(dynProb, expressions, 200, rand);
82      Assert.AreEqual(0.0, ((DoubleValue)alg.Results["SNMSE"].Value).Value, 1E-8);
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.