#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.CombinedIntegerVectorEncoding; using HeuristicLab.Encodings.ConditionActionEncoding; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.ConditionActionClassification; using HeuristicLab.Problems.DataAnalysis; namespace HeuristicLab.Problems.CombinedIntegerVectorClassification { [StorableClass] [Item("CombinedIntegerVectorClassificationProblemData", "A problem data for LCS.")] public class CombinedIntegerVectorClassificationProblemData : ConditionActionClassificationProblemData, ICombinedIntegerVectorClassificationProblemData { #region parameter properites public IFixedValueParameter LengthParameter { get { return (IFixedValueParameter)Parameters["Length"]; } } public IFixedValueParameter ActionLengthParameter { get { return (IFixedValueParameter)Parameters["ActionLength"]; } } public IValueParameter BoundsParameter { get { return (IValueParameter)Parameters["Bounds"]; } } #endregion #region properties public IntValue Length { get { return LengthParameter.Value; } } public IntValue ActionLength { get { return ActionLengthParameter.Value; } } public IntMatrix Bounds { get { return BoundsParameter.Value; } } public CombinedIntegerVectorComparer ConcreteClassifierComparer { get { return new CombinedIntegerVectorComparer(); } } public override IClassifierComparer ClassifierComparer { get { return ConcreteClassifierComparer; } } #endregion [StorableConstructor] protected CombinedIntegerVectorClassificationProblemData(bool deserializing) : base(deserializing) { } protected CombinedIntegerVectorClassificationProblemData(CombinedIntegerVectorClassificationProblemData original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new CombinedIntegerVectorClassificationProblemData(this, cloner); } public CombinedIntegerVectorClassificationProblemData(Dataset dataset, IEnumerable allowedConditionVariables, IEnumerable allowedActionVariables) : base(dataset, allowedConditionVariables, allowedActionVariables) { Parameters.Add(new FixedValueParameter("Length", "", new IntValue(allowedConditionVariables.Count() + allowedActionVariables.Count()))); Parameters.Add(new FixedValueParameter("ActionLength", "", new IntValue(allowedActionVariables.Count()))); Parameters.Add(new ValueParameter("Bounds", "", GetBoundsMatrix(dataset, allowedConditionVariables, allowedActionVariables))); } private IntMatrix GetBoundsMatrix(Dataset dataset, IEnumerable conditionVariables, IEnumerable actionVariables) { IntMatrix bounds = new IntMatrix(conditionVariables.Count() + actionVariables.Count(), 2); int index = 0; foreach (var variable in conditionVariables) { var values = dataset.GetDoubleValues(variable); bounds[index, 0] = (int)values.Min(); bounds[index, 1] = (int)values.Max() + 2; index++; } foreach (var variable in actionVariables) { var values = dataset.GetDoubleValues(variable); bounds[index, 0] = (int)values.Min(); bounds[index, 1] = (int)values.Max() + 1; index++; } return bounds; } public override IInput FetchInput(int rowNumber) { if (!fetchInputCache.ContainsKey(rowNumber)) { int[] elements = new int[Length.Value]; var variableNamesList = Dataset.VariableNames.ToList(); int elementIndex = 0; for (int i = 0; i < variableNamesList.Count; i++) { if (AllowedConditionVariables.Contains(variableNamesList[i])) { elements[elementIndex] = int.Parse(Dataset.GetValue(rowNumber, i)); elementIndex++; } } for (int i = 0; i < variableNamesList.Count; i++) { if (AllowedActionVariables.Contains(variableNamesList[i])) { elements[elementIndex] = int.Parse(Dataset.GetValue(rowNumber, i)); elementIndex++; } } if (elementIndex != Length.Value) { throw new ArgumentException("Length of classifier is not equal to the number of allowed condition + action variables."); } fetchInputCache.Add(rowNumber, new CombinedIntegerVector(elements, ActionLengthParameter.Value.Value, BoundsParameter.Value)); } return fetchInputCache[rowNumber]; } public override IAction FetchAction(int rowNumber) { return (FetchInput(rowNumber) as IClassifier).Action; } protected override void ActionConditionVariablesChanged() { ActionLength.Value = AllowedActionVariables.Count(); Length.Value = AllowedConditionVariables.Count() + ActionLength.Value; BoundsParameter.Value = GetBoundsMatrix(Dataset, AllowedConditionVariables, AllowedActionVariables); } } }