#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.Encodings.ConditionActionEncoding;
using HeuristicLab.Encodings.VariableVector;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.PluginInfrastructure;
using HeuristicLab.Problems.ConditionActionClassification;
using HeuristicLab.Problems.DataAnalysis;
namespace HeuristicLab.Problems.VariableVectorClassification {
[StorableClass]
public class VariableVectorClassificationProblem : ConditionActionClassificationProblem, IVariableVectorClassificationProblem {
public override string ChildName {
get { return "VariableVector"; }
}
#region parameter properties
private IFixedValueParameter> PossibleActionsConcreteClassParameter {
get { return (IFixedValueParameter>)Parameters["PossibleActionsConcreteClass"]; }
}
public override IFixedValueParameter ClassifierComparerParameter {
get { return (IFixedValueParameter)Parameters["ClassifierComparer"]; }
}
public IVariableVectorCoveringCreator VariableVectorCoveringCreator {
get { return (IVariableVectorCoveringCreator)CoveringSolutionCreator; }
}
#endregion
#region properties
public new VariableVectorClassificationProblemData ProblemData {
get { return ProblemDataParameter.Value; }
protected set {
ProblemDataParameter.Value = value;
if (value != null) {
SetProblemDataSettings();
}
}
}
public ItemSet PossibleActionsConcreteClass {
get { return PossibleActionsConcreteClassParameter.Value; }
}
public IClassifierComparer ClassifierComparer {
get { return ClassifierComparerParameter.Value; }
}
#endregion
[StorableConstructor]
protected VariableVectorClassificationProblem(bool deserializing) : base(deserializing) { }
protected VariableVectorClassificationProblem(VariableVectorClassificationProblem original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new VariableVectorClassificationProblem(this, cloner);
}
public VariableVectorClassificationProblem() :
this(new VariableVectorClassificationProblemData(new Dataset(ConditionActionClassificationProblemData.defaultVariableNames, ConditionActionClassificationProblemData.defaultData),
ConditionActionClassificationProblemData.defaultVariableNames.Take(ConditionActionClassificationProblemData.defaultVariableNames.Length - 1), ConditionActionClassificationProblemData.defaultVariableNames.Last().ToEnumerable()),
new XCSEvaluator(), new UniformRandomVariableVectorCreator(), new VariableVectorCoveringCreator()) {
}
public VariableVectorClassificationProblem(VariableVectorClassificationProblemData problemData, XCSEvaluator evaluator, UniformRandomVariableVectorCreator solutionCreator, IVariableVectorCoveringCreator coveringSolutionCreator)
: base(problemData, evaluator, solutionCreator, coveringSolutionCreator) {
Parameters.Add(new FixedValueParameter("ClassifierComparer", problemData.ConcreteClassifierComparer));
Parameters.Add(new FixedValueParameter>("PossibleActions", new ItemSet(ClassifierComparer)));
Parameters.Add(new FixedValueParameter>("PossibleActionsConcreteClass", new ItemSet(ClassifierComparer)));
SolutionCreator.VariableVectorParameter.ActualName = ChildName;
SolutionCreator.ChangeSymbolProbabilityParameter.ActualName = ChangeSymbolProbabilityInCoveringParameter.Name;
SetProblemDataSettings();
InitializeOperators();
}
protected override void SetProblemDataSettings() {
SolutionCreator.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
VariableVectorCoveringCreator.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
SetPossibleActions();
}
private void InitializeOperators() {
Operators.AddRange(ApplicationManager.Manager.GetInstances());
Operators.AddRange(AddManipulators());
ParameterizeOperators();
}
protected override void ParameterizeOperators() {
base.ParameterizeOperators();
foreach (IVariableVectorCrossover op in Operators.OfType()) {
op.ParentsParameter.ActualName = SolutionCreator.VariableVectorParameter.ActualName;
op.ParentsParameter.Hidden = true;
op.ChildParameter.ActualName = SolutionCreator.VariableVectorParameter.ActualName;
op.ChildParameter.Hidden = true;
}
}
private IEnumerable AddManipulators() {
var manipulator = new UniformSomePositionManipulator();
manipulator.ChildParameter.ActualName = ChildName;
manipulator.FetchedInputParameter.ActualName = ClassifierFetcher.CurrentInputToMatchParameter.ActualName;
manipulator.PossibleActionsParameter.ActualName = PossibleActionsConcreteClassParameter.Name;
manipulator.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
return new List() { manipulator };
}
protected override void SetPossibleActions() {
var sampleAction = ProblemData.SampleVariableVector.Action;
var actionKeys = sampleAction.Order;
var actionsEnumeratorList = sampleAction.VariableDictionary.ToDictionary(x => x.Key, y => y.Value.GetAllPossibleActions().ToList());
int[] curPos = new int[actionsEnumeratorList.Count];
//initialize curPos
for (int i = 0; i < curPos.Length; i++) {
curPos[i] = 0;
}
bool done = false;
PossibleActions.Clear();
PossibleActionsConcreteClass.Clear();
VariableVectorAction curAction;
int cur;
while (!done) {
curAction = sampleAction.GetEmptyCopy();
cur = 0;
foreach (var key in actionKeys) {
curAction.VariableDictionary[key].SetTo(actionsEnumeratorList[key][curPos[cur]]);
cur++;
}
PossibleActions.Add(curAction);
PossibleActionsConcreteClass.Add(curAction);
done = MoveNext(actionsEnumeratorList, curPos, actionKeys);
}
ThetaMinimalNumberOfActions.Value = PossibleActions.Count;
}
private bool MoveNext(Dictionary> actionsEnumeratorList, int[] curPos, IEnumerable order) {
int cur = 0;
bool done = true;
foreach (var key in order) {
if (curPos[cur] + 1 < actionsEnumeratorList[key].Count) {
curPos[cur]++;
done = false;
break;
} else {
curPos[cur] = 0;
}
cur++;
}
return done;
}
}
}