diff options
Diffstat (limited to 'subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/planner/cost/impl/VariableBindingBasedCostFunction.java')
-rw-r--r-- | subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/planner/cost/impl/VariableBindingBasedCostFunction.java | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/planner/cost/impl/VariableBindingBasedCostFunction.java b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/planner/cost/impl/VariableBindingBasedCostFunction.java new file mode 100644 index 00000000..a517af25 --- /dev/null +++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/planner/cost/impl/VariableBindingBasedCostFunction.java | |||
@@ -0,0 +1,95 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2014, Marton Bur, Balazs Grill, Akos Horvath, Zoltan Ujhelyi, Istvan Rath and Daniel Varro | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.localsearch.planner.cost.impl; | ||
10 | |||
11 | import java.util.Set; | ||
12 | |||
13 | import tools.refinery.viatra.runtime.localsearch.planner.cost.IConstraintEvaluationContext; | ||
14 | import tools.refinery.viatra.runtime.localsearch.planner.cost.ICostFunction; | ||
15 | import tools.refinery.viatra.runtime.matchers.psystem.PConstraint; | ||
16 | import tools.refinery.viatra.runtime.matchers.psystem.PVariable; | ||
17 | import tools.refinery.viatra.runtime.matchers.psystem.basicdeferred.AggregatorConstraint; | ||
18 | import tools.refinery.viatra.runtime.matchers.psystem.basicdeferred.ExportedParameter; | ||
19 | import tools.refinery.viatra.runtime.matchers.psystem.basicdeferred.NegativePatternCall; | ||
20 | import tools.refinery.viatra.runtime.matchers.psystem.basicenumerables.BinaryTransitiveClosure; | ||
21 | import tools.refinery.viatra.runtime.matchers.psystem.basicenumerables.ConstantValue; | ||
22 | |||
23 | /** | ||
24 | * This class can be used to calculate cost of application of a constraint with a given adornment. | ||
25 | * | ||
26 | * For now the logic is based on the following principles: | ||
27 | * | ||
28 | * <li>The transitive closures, NACs and count finds are the most expensive operations | ||
29 | * | ||
30 | * <li>The number of free variables increase the cost | ||
31 | * | ||
32 | * <li>If all the variables of a constraint are free, then its cost equals to twice the number of its parameter | ||
33 | * variables. This solves the problem of unnecessary iteration over instances at the beginning of a plan (thus causing | ||
34 | * very long run times when executing the plan) by applying constraints based on structural features as soon as | ||
35 | * possible. | ||
36 | * | ||
37 | * <br> | ||
38 | * | ||
39 | * @author Marton Bur | ||
40 | * @since 1.4 | ||
41 | * | ||
42 | */ | ||
43 | public class VariableBindingBasedCostFunction implements ICostFunction { | ||
44 | |||
45 | // Static cost definitions | ||
46 | private static int MAX = 1000; | ||
47 | private static int exportedParameterCost = MAX - 20; | ||
48 | private static int binaryTransitiveClosureCost = MAX - 50; | ||
49 | private static int nacCost = MAX - 100; | ||
50 | private static int aggregatorCost = MAX - 200; | ||
51 | private static int constantCost = 0; | ||
52 | |||
53 | @Override | ||
54 | public double apply(IConstraintEvaluationContext input) { | ||
55 | PConstraint constraint = input.getConstraint(); | ||
56 | Set<PVariable> affectedVariables = constraint.getAffectedVariables(); | ||
57 | |||
58 | int cost = 0; | ||
59 | |||
60 | // For constants the cost is determined to be 0.0 | ||
61 | // The following constraints should be checks: | ||
62 | // * Binary transitive closure | ||
63 | // * NAC | ||
64 | // * count | ||
65 | // * exported parameter - only a metadata | ||
66 | if (constraint instanceof ConstantValue) { | ||
67 | cost = constantCost; | ||
68 | } else if (constraint instanceof BinaryTransitiveClosure) { | ||
69 | cost = binaryTransitiveClosureCost; | ||
70 | } else if (constraint instanceof NegativePatternCall) { | ||
71 | cost = nacCost; | ||
72 | } else if (constraint instanceof AggregatorConstraint) { | ||
73 | cost = aggregatorCost; | ||
74 | } else if (constraint instanceof ExportedParameter) { | ||
75 | cost = exportedParameterCost; | ||
76 | } else { | ||
77 | // In case of other constraints count the number of unbound variables | ||
78 | for (PVariable pVariable : affectedVariables) { | ||
79 | if (input.getFreeVariables().contains(pVariable)) { | ||
80 | // For each free variable ('without-value-variable') increase cost | ||
81 | cost += 1; | ||
82 | } | ||
83 | } | ||
84 | if (cost == affectedVariables.size()) { | ||
85 | // If all the variables are free, double the cost. | ||
86 | // This ensures that iteration costs more | ||
87 | cost *= 2; | ||
88 | } | ||
89 | |||
90 | } | ||
91 | |||
92 | return Float.valueOf(cost); | ||
93 | } | ||
94 | |||
95 | } | ||