[12303] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17209] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12303] | 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 HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Operators;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
[16565] | 27 | using HEAL.Attic;
|
---|
[12303] | 28 |
|
---|
[12529] | 29 | namespace HeuristicLab.Optimization {
|
---|
[12307] | 30 | [Item("TerminationOperator", "An operator which either calls the terminate- or the continue branch.")]
|
---|
[16565] | 31 | [StorableType("FD4B77FF-4C9D-45D9-8CDF-F5F5EE2664F7")]
|
---|
[12444] | 32 | public sealed class TerminationOperator : InstrumentedOperator, ITerminationBasedOperator {
|
---|
[12447] | 33 | public ILookupParameter<ITerminator> TerminatorParameter {
|
---|
| 34 | get { return (ILookupParameter<ITerminator>)Parameters["Terminator"]; }
|
---|
[12303] | 35 | }
|
---|
| 36 | public ILookupParameter<BoolValue> TerminateParameter {
|
---|
| 37 | get { return (ILookupParameter<BoolValue>)Parameters["Terminate"]; }
|
---|
| 38 | }
|
---|
| 39 | private OperatorParameter ContinueBranchParameter {
|
---|
| 40 | get { return (OperatorParameter)Parameters["ContinueBranch"]; }
|
---|
| 41 | }
|
---|
| 42 | private OperatorParameter TerminateBranchParameter {
|
---|
| 43 | get { return (OperatorParameter)Parameters["TerminateBranch"]; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public IOperator ContinueBranch {
|
---|
| 47 | get { return ContinueBranchParameter.Value; }
|
---|
| 48 | set { ContinueBranchParameter.Value = value; }
|
---|
| 49 | }
|
---|
| 50 | public IOperator TerminateBranch {
|
---|
| 51 | get { return TerminateBranchParameter.Value; }
|
---|
| 52 | set { TerminateBranchParameter.Value = value; }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | [StorableConstructor]
|
---|
[16565] | 56 | private TerminationOperator(StorableConstructorFlag _) : base(_) { }
|
---|
[12303] | 57 | private TerminationOperator(TerminationOperator original, Cloner cloner)
|
---|
| 58 | : base(original, cloner) {
|
---|
| 59 | }
|
---|
| 60 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 61 | return new TerminationOperator(this, cloner);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | public TerminationOperator()
|
---|
| 65 | : base() {
|
---|
[12447] | 66 | Parameters.Add(new LookupParameter<ITerminator>("Terminator", "The termination criteria which sould be checked."));
|
---|
[12303] | 67 | Parameters.Add(new LookupParameter<BoolValue>("Terminate", "The parameter which will be set to determine if execution should be terminated or schould continue."));
|
---|
| 68 | Parameters.Add(new OperatorParameter("ContinueBranch", "The operator which is executed if no termination criteria has met."));
|
---|
| 69 | Parameters.Add(new OperatorParameter("TerminateBranch", "The operator which is executed if any termination criteria has met."));
|
---|
| 70 |
|
---|
| 71 | var assigner = new Assigner() { Name = "Terminate = false" };
|
---|
| 72 | assigner.LeftSideParameter.ActualName = TerminateParameter.Name;
|
---|
| 73 | assigner.RightSideParameter.Value = new BoolValue(false);
|
---|
| 74 |
|
---|
[12308] | 75 | var placeholder = new Placeholder() { Name = "Check termination criteria (Placeholder)" };
|
---|
[12447] | 76 | placeholder.OperatorParameter.ActualName = TerminatorParameter.Name;
|
---|
[12307] | 77 |
|
---|
[12308] | 78 | BeforeExecutionOperators.Add(assigner);
|
---|
| 79 | BeforeExecutionOperators.Add(placeholder);
|
---|
[12303] | 80 | }
|
---|
| 81 |
|
---|
[12308] | 82 | public override IOperation InstrumentedApply() {
|
---|
| 83 | var next = new OperationCollection(base.InstrumentedApply());
|
---|
[12410] | 84 | if (TerminateParameter.ActualValue.Value) {
|
---|
[12308] | 85 | if (TerminateBranch != null) next.Insert(0, ExecutionContext.CreateOperation(TerminateBranch));
|
---|
| 86 | } else {
|
---|
| 87 | if (ContinueBranch != null) next.Insert(0, ExecutionContext.CreateOperation(ContinueBranch));
|
---|
| 88 | }
|
---|
| 89 | return next;
|
---|
[12303] | 90 | }
|
---|
| 91 | }
|
---|
| 92 | } |
---|