Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Tests/HeuristicLab.Scripting-3.3/GAQAPScriptTest.cs @ 17213

Last change on this file since 17213 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 3.1 KB
RevLine 
[11514]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11514]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.IO;
23using System.Linq;
[17105]24using HEAL.Attic;
[11514]25using HeuristicLab.Problems.Instances.QAPLIB;
26using HeuristicLab.Problems.QuadraticAssignment;
27using HeuristicLab.Scripting;
28using Microsoft.VisualStudio.TestTools.UnitTesting;
29
30namespace HeuristicLab.Tests {
31  [TestClass]
32  public class GAQAPScriptTest {
33    private const string ScriptFileName = "GA_QAP_Script";
34    private const string ScriptItemName = "Genetic Algorithm Script - QAP";
35    private const string ScriptItemDescription = "A scripted genetic algorithm which solves the \"" + ProblemInstanceName + "\" quadratic assignment problem (imported from Drezner)";
36    private const string ProblemInstanceName = "dre56";
37    private const string BestQualityVariableName = "bestQuality";
38
[17105]39    private static readonly ProtoBufSerializer serializer = new ProtoBufSerializer();
40
[11514]41    [TestMethod]
42    [TestCategory("Scripts.Create")]
43    [TestProperty("Time", "short")]
44    public void CreateGAQAPScriptScriptTest() {
45      var script = CreateGAQAPScript();
46      string path = Path.Combine(ScriptingUtils.ScriptsDirectory, ScriptFileName + ScriptingUtils.ScriptFileExtension);
[17105]47      serializer.Serialize(script, path);
[11514]48    }
49
50    [TestMethod]
51    [TestCategory("Scripts.Execute")]
52    [TestProperty("Time", "long")]
53    public void RunGAQAPScriptTest() {
54      var script = CreateGAQAPScript();
55
56      script.Compile();
57      ScriptingUtils.RunScript(script);
58
59      var bestQuality = ScriptingUtils.GetVariable<double>(script, BestQualityVariableName);
60      Assert.AreEqual(2410.0, bestQuality, 1E-8);
61    }
62
63    private CSharpScript CreateGAQAPScript() {
64      var script = new CSharpScript {
65        Name = ScriptItemName,
66        Description = ScriptItemDescription
67      };
68      #region Variables
69      var provider = new DreznerQAPInstanceProvider();
70      var instance = provider.GetDataDescriptors().Single(x => x.Name == ProblemInstanceName);
71      var data = provider.LoadData(instance);
72      var problem = new QuadraticAssignmentProblem();
73      problem.Load(data);
74      script.VariableStore.Add(ProblemInstanceName, problem);
75      #endregion
76      #region Code
[11789]77      script.Code = ScriptSources.GAQAPScriptSource;
[11514]78      #endregion
79      return script;
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.