#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.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.PluginInfrastructure; using HeuristicLab.Problems.ConditionActionClassification; using HeuristicLab.Problems.DataAnalysis; namespace HeuristicLab.Problems.CombinedIntegerVectorClassification { [StorableClass] public class CombinedIntegerVectorClassificationProblem : ConditionActionClassificationProblem { public override string ChildName { get { return "CombinedIntegerVector"; } } #region parameter properties private IFixedValueParameter> PossibleActionsConcreteClassParameter { get { return (IFixedValueParameter>)Parameters["PossibleActionsConcreteClass"]; } } public override IFixedValueParameter ClassifierComparerParameter { get { return (IFixedValueParameter)Parameters["ClassifierComparer"]; } } #endregion #region properties public new CombinedIntegerVectorClassificationProblemData ProblemData { get { return ProblemDataParameter.Value; } protected set { ProblemDataParameter.Value = value; if (value != null) { SetProblemDataSpecificParameters(); } } } public ItemSet PossibleActionsConcreteClass { get { return PossibleActionsConcreteClassParameter.Value; } } public IClassifierComparer ClassifierComparer { get { return ClassifierComparerParameter.Value; } } #endregion [StorableConstructor] protected CombinedIntegerVectorClassificationProblem(bool deserializing) : base(deserializing) { } protected CombinedIntegerVectorClassificationProblem(CombinedIntegerVectorClassificationProblem original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new CombinedIntegerVectorClassificationProblem(this, cloner); } public CombinedIntegerVectorClassificationProblem() : this(new CombinedIntegerVectorClassificationProblemData(new Dataset(ConditionActionClassificationProblemData.defaultVariableNames, ConditionActionClassificationProblemData.defaultData), ConditionActionClassificationProblemData.defaultVariableNames.Take(ConditionActionClassificationProblemData.defaultVariableNames.Length - 1), ConditionActionClassificationProblemData.defaultVariableNames.Last().ToEnumerable()), new XCSEvaluator(), new UniformRandomCombinedIntegerVectorCreator(), new CombinedIntegerVectorCoveringCreator()) { } public CombinedIntegerVectorClassificationProblem(CombinedIntegerVectorClassificationProblemData problemData, XCSEvaluator evaluator, UniformRandomCombinedIntegerVectorCreator solutionCreator, ICoveringSolutionCreator coveringSolutionCreator) : base(problemData, evaluator, solutionCreator, coveringSolutionCreator) { Parameters.Add(new FixedValueParameter("ClassifierComparer", new CombinedIntegerVectorComparer())); Parameters.Add(new FixedValueParameter>("PossibleActions", new ItemSet(ClassifierComparer))); Parameters.Add(new FixedValueParameter>("PossibleActionsConcreteClass", new ItemSet(ClassifierComparer))); SetProblemDataSpecificParameters(); InitializeOperators(); problemData.Changed += new System.EventHandler(problemData_Changed); } private void problemData_Changed(object sender, System.EventArgs e) { SetProblemDataSpecificParameters(); } private void SetProblemDataSpecificParameters() { SolutionCreator.ActionPartLengthParameter.Value = ProblemData.ActionLengthParameter.Value; SolutionCreator.LengthParameter.Value = ProblemData.LengthParameter.Value; SolutionCreator.BoundsParameter.Value = ProblemData.BoundsParameter.Value; SetPossibleActions(); } private void InitializeOperators() { Operators.AddRange(ApplicationManager.Manager.GetInstances()); Operators.AddRange(AddManipulators()); ParameterizeOperators(); } private IEnumerable AddManipulators() { var manipulator = new UniformSomePositionManipulator(); manipulator.ChildParameter.ActualName = ChildName; manipulator.FetchedInputParameter.ActualName = ClassifierFetcher.CurrentInputToMatchParameter.ActualName; manipulator.PossibleActionsParameter.ActualName = PossibleActionsConcreteClassParameter.Name; return new List() { manipulator }; } protected override void SetPossibleActions() { //get bounds of action IntMatrix actionBounds = GetElementsOfBoundsForAction(ProblemData.Bounds, ProblemData.Length.Value, ProblemData.ActionLength.Value); int actionLength = ProblemData.ActionLength.Value; int start = ProblemData.Length.Value - actionLength; int[] elements = new int[actionLength]; int[] curPos = new int[actionLength]; bool done = false; //initialize curPos for (int i = 0; i < actionBounds.Rows; i++) { curPos[i] = actionBounds[i, 0]; } PossibleActions.Clear(); PossibleActionsConcreteClass.Clear(); while (!done) { PossibleActions.Add(new CombinedIntegerVector(curPos, actionLength, actionBounds)); PossibleActionsConcreteClass.Add(new CombinedIntegerVector(curPos, actionLength, actionBounds)); curPos = GetNextAction(curPos, actionBounds, out done); } ThetaMinimalNumberOfActions.Value = PossibleActions.Count; } private int[] GetNextAction(int[] curPos, IntMatrix actionBounds, out bool done) { int cur = 0; while (cur < curPos.Length) { curPos[cur] += actionBounds.Columns < 3 ? 1 : actionBounds[cur, 2]; if (curPos[cur] >= actionBounds[cur, 1]) { curPos[cur] = actionBounds[cur, 0]; cur++; } else { break; } } done = cur >= curPos.Length; return curPos; } private IntMatrix GetElementsOfBoundsForAction(IntMatrix bounds, int length, int actionPartLength) { IntMatrix actionBounds = new IntMatrix(actionPartLength, bounds.Columns); int start = length - actionPartLength; for (int i = start; i < length; i++) { int pos = i % bounds.Rows; for (int j = 0; j < bounds.Columns; j++) { actionBounds[i - start, j] = bounds[pos, j]; } } return actionBounds; } } }