Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/WalkExporter/Data.cs @ 17280

Last change on this file since 17280 was 16955, checked in by abeham, 5 years ago

#2457: worked on thesis

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Text;
27using ProtoBuf;
28
29namespace WalkExporter {
30  [ProtoContract]
31  public class KeyValue {
32    [ProtoMember(1, IsRequired = true)]
33    public string Key;
34    [ProtoMember(2)]
35    public double? ContinuousValue;
36    [ProtoMember(3)]
37    public long? DiscreteValue;
38    [ProtoMember(4)]
39    public string StringValue;
40    [ProtoMember(5, IsPacked = true)]
41    public List<double> ContinuousArrayValue;
42    [ProtoMember(6, IsPacked = true)]
43    public List<long> DiscreteArrayValue;
44    [ProtoMember(7)]
45    public List<string> StringArrayValue;
46    [ProtoMember(8)]
47    public byte[] RawValue;
48
49    public double GetNumericValue() {
50      return ContinuousValue.HasValue ? ContinuousValue.Value : DiscreteValue.Value;
51    }
52    public void SetNumericValue(long v) {
53      ContinuousValue = null;
54      DiscreteValue = v;
55    }
56    public void SetNumericValue(double v) {
57      ContinuousValue = v;
58      DiscreteValue = null;
59    }
60
61    public override string ToString() {
62      var sb = new StringBuilder();
63      sb.Append(Key);
64      sb.Append(": ");
65      sb.Append(string.Join(" / ", new string[] {
66        ContinuousValue?.ToString(), DiscreteValue?.ToString(),
67        StringValue?.Substring(0, 10),
68        ContinuousArrayValue != null ? string.Join(", ", ContinuousArrayValue.Take(10)) : null,
69        DiscreteArrayValue != null ? string.Join(", ", DiscreteArrayValue.Take(10)) : null,
70        StringArrayValue != null ? string.Join(", ", StringArrayValue.Take(10)) : null,
71        RawValue != null ? Convert.ToBase64String(RawValue).Substring(0, 10) : null
72      }.Where(x => !string.IsNullOrEmpty(x))));
73      return sb.ToString();
74    }
75  }
76
77  [ProtoContract]
78  public class Solution {
79    [ProtoMember(1)]
80    public string Type;
81    [ProtoMember(2)]
82    public List<KeyValue> Data;
83  }
84
85  [ProtoContract]
86  public class Walk {
87    [ProtoMember(1, IsRequired = true, IsPacked = true)]
88    public List<double> QualityTrail;
89    [ProtoMember(2)]
90    public List<Solution> Solutions;
91  }
92
93  [ProtoContract]
94  public class Exploration {
95    [ProtoMember(1, IsRequired = true)]
96    public string Problem;
97    [ProtoMember(2, IsRequired = true)]
98    public int Dimension;
99    [ProtoMember(3, IsRequired = true)]
100    public List<Walk> Walks;
101
102    public static Exploration Load(string file) => Serializer.Deserialize<Exploration>(File.OpenRead(file));
103  }
104
105  [ProtoContract]
106  public class Experiment {
107    [ProtoMember(1, IsRequired = true)]
108    public string Algorithm;
109    [ProtoMember(2, IsRequired = true)]
110    public List<Exploration> Trials;
111
112    public static Experiment Load(string file) => Serializer.Deserialize<Experiment>(File.OpenRead(file));
113
114    public void Print(TextWriter writer) {
115      writer.WriteLine("Experiment: " + Algorithm);
116      foreach (var t in Trials) {
117        writer.WriteLine("Trial: {0} ({1})", t.Problem, t.Dimension);
118        foreach (var w in t.Walks) {
119          writer.WriteLine("Walk: " + string.Join(";", w.QualityTrail.Take(10)));
120        }
121      }
122    }
123  }
124
125  [ProtoContract]
126  public class ProblemInstanceDescriptor {
127    [ProtoMember(1)]
128    public string Name;
129    [ProtoMember(2)]
130    public string Class;
131    [ProtoMember(3)]
132    public int Dimension;
133    [ProtoMember(4)]
134    public List<KeyValue> Features;
135    [ProtoMember(5)]
136    public double DescriptionEffort;
137   
138    public IEnumerable<double> GetNumericFeatures(IEnumerable<string> keys) {
139      var map = Features.ToDictionary(x => x.Key, x => x.GetNumericValue());
140      return keys.Select(x => map[x]);
141    }
142  }
143
144  [ProtoContract]
145  public class Knowledgebase {
146    [ProtoMember(1)]
147    public List<ProblemInstanceDescriptor> Problems;
148
149    public static Knowledgebase Load(string file) => Serializer.Deserialize<Knowledgebase>(File.OpenRead(file));
150
151    public void Print(TextWriter writer) {
152      writer.WriteLine("Knowledge base with {0} instances", Problems?.Count ?? 0);
153      if (Problems == null) return;
154      foreach (var p in Problems) {
155        writer.WriteLine("Problem: {2} / {0} ({1}), [{2}]", p.Name, p.Dimension, p.Class, p.DescriptionEffort);
156        writer.WriteLine("Features: {0}", p.Features?.Count ?? 0);
157        if (p.Features != null)
158          writer.WriteLine(" " + string.Join(Environment.NewLine + " ", p.Features.Select(f => f.ToString())));
159
160      }
161    }
162  }
163
164  /// <summary>
165  /// Just exists to have one type that includes all other types
166  /// </summary>
167  [ProtoContract]
168  internal class AllProtos {
169    [ProtoMember(1)]
170    public Experiment Experiment;
171    [ProtoMember(2)]
172    public Knowledgebase Knowledgebase;
173  }
174}
Note: See TracBrowser for help on using the repository browser.