#region License Information /* HeuristicLab * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using ProtoBuf; namespace WalkExporter { [ProtoContract] public class KeyValue { [ProtoMember(1, IsRequired = true)] public string Key; [ProtoMember(2)] public double? ContinuousValue; [ProtoMember(3)] public long? DiscreteValue; [ProtoMember(4)] public string StringValue; [ProtoMember(5, IsPacked = true)] public List ContinuousArrayValue; [ProtoMember(6, IsPacked = true)] public List DiscreteArrayValue; [ProtoMember(7)] public List StringArrayValue; [ProtoMember(8)] public byte[] RawValue; public double GetNumericValue() { return ContinuousValue.HasValue ? ContinuousValue.Value : DiscreteValue.Value; } public void SetNumericValue(long v) { ContinuousValue = null; DiscreteValue = v; } public void SetNumericValue(double v) { ContinuousValue = v; DiscreteValue = null; } public override string ToString() { var sb = new StringBuilder(); sb.Append(Key); sb.Append(": "); sb.Append(string.Join(" / ", new string[] { ContinuousValue?.ToString(), DiscreteValue?.ToString(), StringValue?.Substring(0, 10), ContinuousArrayValue != null ? string.Join(", ", ContinuousArrayValue.Take(10)) : null, DiscreteArrayValue != null ? string.Join(", ", DiscreteArrayValue.Take(10)) : null, StringArrayValue != null ? string.Join(", ", StringArrayValue.Take(10)) : null, RawValue != null ? Convert.ToBase64String(RawValue).Substring(0, 10) : null }.Where(x => !string.IsNullOrEmpty(x)))); return sb.ToString(); } } [ProtoContract] public class Solution { [ProtoMember(1)] public string Type; [ProtoMember(2)] public List Data; } [ProtoContract] public class Walk { [ProtoMember(1, IsRequired = true, IsPacked = true)] public List QualityTrail; [ProtoMember(2)] public List Solutions; } [ProtoContract] public class Exploration { [ProtoMember(1, IsRequired = true)] public string Problem; [ProtoMember(2, IsRequired = true)] public int Dimension; [ProtoMember(3, IsRequired = true)] public List Walks; public static Exploration Load(string file) => Serializer.Deserialize(File.OpenRead(file)); } [ProtoContract] public class Experiment { [ProtoMember(1, IsRequired = true)] public string Algorithm; [ProtoMember(2, IsRequired = true)] public List Trials; public static Experiment Load(string file) => Serializer.Deserialize(File.OpenRead(file)); public void Print(TextWriter writer) { writer.WriteLine("Experiment: " + Algorithm); foreach (var t in Trials) { writer.WriteLine("Trial: {0} ({1})", t.Problem, t.Dimension); foreach (var w in t.Walks) { writer.WriteLine("Walk: " + string.Join(";", w.QualityTrail.Take(10))); } } } } [ProtoContract] public class ProblemInstanceDescriptor { [ProtoMember(1)] public string Name; [ProtoMember(2)] public string Class; [ProtoMember(3)] public int Dimension; [ProtoMember(4)] public List Features; [ProtoMember(5)] public double DescriptionEffort; public IEnumerable GetNumericFeatures(IEnumerable keys) { var map = Features.ToDictionary(x => x.Key, x => x.GetNumericValue()); return keys.Select(x => map[x]); } } [ProtoContract] public class Knowledgebase { [ProtoMember(1)] public List Problems; public static Knowledgebase Load(string file) => Serializer.Deserialize(File.OpenRead(file)); public void Print(TextWriter writer) { writer.WriteLine("Knowledge base with {0} instances", Problems?.Count ?? 0); if (Problems == null) return; foreach (var p in Problems) { writer.WriteLine("Problem: {2} / {0} ({1}), [{2}]", p.Name, p.Dimension, p.Class, p.DescriptionEffort); writer.WriteLine("Features: {0}", p.Features?.Count ?? 0); if (p.Features != null) writer.WriteLine(" " + string.Join(Environment.NewLine + " ", p.Features.Select(f => f.ToString()))); } } } /// /// Just exists to have one type that includes all other types /// [ProtoContract] internal class AllProtos { [ProtoMember(1)] public Experiment Experiment; [ProtoMember(2)] public Knowledgebase Knowledgebase; } }