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