#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.Optimization.Operators.LCS; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Encodings.DecisionList { [StorableClass] [Item("DecisionList", "")] public class DecisionList : Item, IGAssistIndividual { [Storable] private IList rules; public IList Rules { get { return rules; } } [Storable] private IAction defaultAction; public IAction DefaultAction { get { return defaultAction; } } public double Length { get { return 1 + rules.Sum(r => r.Length); } } // default rule (action) is part of the rule set size public int RuleSetSize { get { return defaultAction == null ? rules.Count : rules.Count + 1; } } [StorableConstructor] protected DecisionList(bool deserializing) : base(deserializing) { } protected DecisionList(DecisionList original, Cloner cloner) : base(original, cloner) { defaultAction = cloner.Clone(original.DefaultAction); rules = new List(original.rules.Count); foreach (var rule in original.Rules) { rules.Add(cloner.Clone(rule)); } } public DecisionList() : base() { rules = new List(); defaultAction = null; } public DecisionList(List rules) : base() { this.rules = rules; this.defaultAction = null; } public DecisionList(List rules, IAction defaultAction) : base() { if (rules.Any(x => x.Action.Match(defaultAction))) { throw new ArgumentException("If a default action is used. The default action is not allowed be used in any rule."); } this.rules = rules; this.defaultAction = defaultAction; } public override IDeepCloneable Clone(Cloner cloner) { return new DecisionList(this, cloner); } // for convenience public IEnumerable Evaluate(IEnumerable input) { ItemSet aliveRules; return Evaluate(input, out aliveRules); } // for convenience public IEnumerable Evaluate(IEnumerable input, out ItemSet aliveRules) { double theoryLengtgh; return Evaluate(input, out aliveRules, out theoryLengtgh); } public IEnumerable Evaluate(IEnumerable input, out ItemSet aliveRules, out double theoryLength) { var estimated = new List(); var activatedRules = new ItemSet(); int count = 0; foreach (var dli in input) { count++; foreach (var rule in rules) { if (rule.MatchInput(dli)) { estimated.Add(rule.Action); activatedRules.Add(rule); break; } } if (count > estimated.Count) { estimated.Add(defaultAction); } } aliveRules = activatedRules; theoryLength = activatedRules.Sum(x => x.ComputeTheoryLength()); return estimated; } public void RemoveRules(IEnumerable deadRules) { foreach (var deadRule in deadRules) { Rules.Remove(deadRule); } } #region IGAssistIndividual Members public IGAssistNiche Niche { get { return defaultAction; } } public void ApplySplit(IRandom random, double probability) { foreach (var rule in rules) { rule.ApplySplit(random, probability); } } public void ApplyMerge(IRandom random, double probability) { foreach (var rule in rules) { rule.ApplyMerge(random, probability); } } public void ApplyReinitialize(IRandom random, double probability, double oneProbability, IEnumerable discretizers) { foreach (var rule in rules) { rule.ApplyReinitialize(random, probability, oneProbability, discretizers); } } public IGAssistSolution CreateGAssistSolution(IGAssistProblemData problemData) { return new DecisionListSolution(this, problemData); } #endregion #region IGAssistModel Members IEnumerable IGAssistModel.Evaluate(IEnumerable input) { return Evaluate(input); } public IEnumerable Evaluate(IEnumerable input, out ItemSet aliveRules, out double theoryLength) { return Evaluate(input, out aliveRules, out theoryLength); } #endregion } }