#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 HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.IntegerVectorEncoding; using HeuristicLab.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.DataAnalysis; namespace HeuristicLab.Algorithms.EGO { [Item("DiscreteSampleCollector", "Collects IntegerVectors into a modifiablbe dataset")] [StorableClass] 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(bool 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"); for (var i = 0; i < vector.Length; i++) data.AddVariable("input" + i, new double[0]); data.AddVariable("output", new double[0]); } 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); } } }