[9334] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[9342] | 28 | using HeuristicLab.Optimization.Operators.LCS;
|
---|
[9334] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Encodings.DecisionList {
|
---|
| 32 | [StorableClass]
|
---|
| 33 | [Item("Rule", "")]
|
---|
| 34 | public class Rule : Item {
|
---|
| 35 | [Storable]
|
---|
| 36 | private IDictionary<string, IVariable> variables;
|
---|
| 37 | public IDictionary<string, IVariable> Variables {
|
---|
| 38 | get { return variables; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | [Storable]
|
---|
| 42 | private IAction action;
|
---|
| 43 | public IAction Action { get { return action; } }
|
---|
| 44 |
|
---|
[9605] | 45 | public double Length {
|
---|
| 46 | get { return variables.Values.Sum(x => x.Length); }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[9334] | 49 | [StorableConstructor]
|
---|
| 50 | protected Rule(bool deserializing) : base(deserializing) { }
|
---|
| 51 | protected Rule(Rule original, Cloner cloner)
|
---|
| 52 | : base(original, cloner) {
|
---|
| 53 | variables = new Dictionary<string, IVariable>(original.variables.Count);
|
---|
| 54 | foreach (var item in original.variables) {
|
---|
| 55 | variables.Add(item.Key, cloner.Clone(item.Value));
|
---|
| 56 | }
|
---|
| 57 | action = (IAction)original.action.Clone();
|
---|
| 58 | }
|
---|
| 59 | public Rule()
|
---|
| 60 | : base() {
|
---|
| 61 | variables = new Dictionary<string, IVariable>();
|
---|
| 62 | }
|
---|
| 63 | public Rule(IEnumerable<IVariable> condition, IAction action)
|
---|
| 64 | : this() {
|
---|
| 65 | foreach (var variable in condition) {
|
---|
| 66 | variables.Add(variable.VariableName, variable);
|
---|
| 67 | }
|
---|
| 68 | this.action = action;
|
---|
| 69 | }
|
---|
| 70 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 71 | return new Rule(this, cloner);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | public override string ToString() {
|
---|
| 75 | StringBuilder sb = new StringBuilder();
|
---|
| 76 | foreach (var variable in Variables) {
|
---|
| 77 | sb.Append(variable.Value + "|");
|
---|
| 78 | }
|
---|
[9352] | 79 | sb.Append("|" + Action);
|
---|
[9334] | 80 | return sb.ToString();
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[9605] | 83 | public void Randomize(IRandom random, double oneProbability, IEnumerable<IDiscretizer> discretizers, IEnumerable<IAction> exceptActions = null) {
|
---|
[9334] | 84 | foreach (var variable in variables.Values) {
|
---|
[9605] | 85 | variable.Randomize(random, oneProbability, discretizers);
|
---|
[9334] | 86 | }
|
---|
[9605] | 87 | if (exceptActions == null) {
|
---|
| 88 | action.Randomize(random);
|
---|
| 89 | } else {
|
---|
| 90 | action.Randomize(random, exceptActions);
|
---|
| 91 | }
|
---|
[9334] | 92 | }
|
---|
| 93 |
|
---|
[9605] | 94 | public void SetToMatchInput(IGAssistInput input) {
|
---|
| 95 | foreach (var variable in variables) {
|
---|
| 96 | if (!input.VariableNames.Contains(variable.Key)) {
|
---|
| 97 | throw new ArgumentException("input does not contain variable name of rule");
|
---|
| 98 | }
|
---|
| 99 | variable.Value.SetToMatch(input.GetVariableValue(variable.Key));
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[9334] | 103 | public double ComputeTheoryLength() {
|
---|
| 104 | return variables.Sum(x => x.Value.ComputeTheoryLength());
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[9392] | 107 | public bool MatchInput(IGAssistInput target) {
|
---|
[9334] | 108 | foreach (var variable in variables) {
|
---|
[9392] | 109 | if (!target.VariableNames.Contains(variable.Key)) { throw new ArgumentException("Input doesn't contain variable"); }
|
---|
| 110 | if (!variable.Value.Match(target.GetVariableValue(variable.Key))) { return false; }
|
---|
[9334] | 111 | }
|
---|
| 112 | return true;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | public Rule Crossover(Rule rule, IRandom random) {
|
---|
| 116 | //variables and rule.Variables have to be the same
|
---|
| 117 | int cutpoint = random.Next(0, Variables.Count);
|
---|
| 118 | var crossedVariables = new List<IVariable>(variables.Count);
|
---|
| 119 | var variableNames = variables.Keys.ToList();
|
---|
| 120 | for (int i = 0; i < cutpoint; i++) {
|
---|
| 121 | crossedVariables.Add(this.Variables[variableNames[i]]);
|
---|
| 122 | }
|
---|
| 123 | for (int i = cutpoint; i < variables.Count; i++) {
|
---|
| 124 | crossedVariables.Add(rule.Variables[variableNames[i]]);
|
---|
| 125 | }
|
---|
| 126 | IAction action = random.Next(0, 2) == 0 ? this.action : rule.action;
|
---|
| 127 | return new Rule(crossedVariables, action);
|
---|
| 128 | }
|
---|
[9342] | 129 |
|
---|
| 130 | public void ApplySplit(IRandom random, double probability) {
|
---|
| 131 | foreach (var variable in variables.Values) {
|
---|
| 132 | if (random.NextDouble() < probability)
|
---|
| 133 | variable.Split(random);
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | public void ApplyMerge(IRandom random, double probability) {
|
---|
| 138 | foreach (var variable in variables.Values) {
|
---|
| 139 | if (random.NextDouble() < probability)
|
---|
| 140 | variable.Merge(random);
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | public void ApplyReinitialize(IRandom random, double probability, double oneProbability, IEnumerable<IDiscretizer> discretizers) {
|
---|
| 145 | foreach (var variable in variables.Values) {
|
---|
| 146 | if (random.NextDouble() < probability)
|
---|
| 147 | variable.Reinitialize(random, oneProbability, discretizers);
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
[9334] | 150 | }
|
---|
| 151 | }
|
---|