#region License Information /* HeuristicLab * Copyright (C) 2002-2016 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.Collections.Generic; using System.Linq; using HEAL.Attic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.IntegerVectorEncoding; using HeuristicLab.Operators; using HeuristicLab.Parameters; using HeuristicLab.Problems.DataAnalysis; namespace HeuristicLab.Algorithms.EGO { [Item("DiscreteSampleCollector", "Collects IntegerVectors into a modifiablbe dataset")] [StorableType("b8638624-f79f-41e6-a837-d70824c00082")] public class DiscreteSampleCollector : InstrumentedOperator { public override bool CanChangeName => true; public ILookupParameter IntegerVectorParameter => (ILookupParameter)Parameters["IntegerVector"]; public ILookupParameter QualityParameter => (ILookupParameter)Parameters["Quality"]; public ILookupParameter DatasetParameter => (ILookupParameter)Parameters["Dataset"]; [StorableConstructor] protected DiscreteSampleCollector(StorableConstructorFlag deserializing) : base(deserializing) { } protected DiscreteSampleCollector(DiscreteSampleCollector original, Cloner cloner) : base(original, cloner) { } public DiscreteSampleCollector() { Parameters.Add(new LookupParameter("IntegerVector", "The vector which should be collected.")); Parameters.Add(new LookupParameter("Quality", "The quality associated which this vector")); Parameters.Add(new LookupParameter("Dataset", "The Dataset in wich new samples are stored.")); } public override IDeepCloneable Clone(Cloner cloner) { return new DiscreteSampleCollector(this, cloner); } public sealed override IOperation InstrumentedApply() { var vector = IntegerVectorParameter.ActualValue; var quality = QualityParameter.ActualValue.Value; var data = DatasetParameter.ActualValue; if (data.Columns != vector.Length + 1) { if (data.Columns != 0 || data.Rows != 0) throw new OperatorExecutionException(this, "dataset columns do not match samplesize+1"); var variableNames = vector.Select((x, i) => string.Format("input" + i)).Concat("output".ToEnumerable()); var variableValues = vector.Select(x => (double)x).Concat(quality.ToEnumerable()).Select(x => new List { x }); data = DatasetParameter.ActualValue = new ModifiableDataset(variableNames, variableValues); } else AddRow(data, vector, quality); return base.InstrumentedApply(); } private static void AddRow(ModifiableDataset data, IntegerVector vector, double quality) { var row = new object[vector.Length + 1]; for (var i = 0; i < vector.Length; i++) row[i] = (double)vector[i]; row[vector.Length] = quality; data.AddRow(row); } } }