aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/CostObjectiveHint.xtend
blob: 2434073d6bcb8a31459a12761b801efad3f2a953 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization

import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.BoundSaturationListener
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.ExtendedLinearExpressionBuilder
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.LinearTypeConstraintHint
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.LinearTypeExpressionBuilderFactory
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.PolyhedronExtensionOperator
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns.PatternGenerator
import java.util.Map
import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PQuery
import org.eclipse.xtend.lib.annotations.Accessors

abstract class CostObjectiveHint implements LinearTypeConstraintHint, BoundSaturationListener {
	@Accessors ThreeValuedCostObjective objective
	@Accessors IObjectiveBoundsProvider boundsProvider
	
	Integer bestUpper = null

	override getAdditionalPatterns(PatternGenerator patternGenerator, Map<String, PQuery> fqnToPQuery) {
		''''''
	}

	override createConstraintUpdater(LinearTypeExpressionBuilderFactory builderFactory) {
		null
	}
	
	def isExact() {
		false
	}

	def PolyhedronExtensionOperator createPolyhedronExtensionOperator(
		Map<String, CostElementMatchers> costElementMatchers) {
		null
	}
	
	def setObjective(ThreeValuedCostObjective objective) {
		if (this.objective !== null) {
			throw new IllegalStateException("Objective was already set")
		}
		this.objective = objective
	}
	
	def setBoundsProvider(IObjectiveBoundsProvider boundsProvider) {
		if (this.boundsProvider !== null) {
			throw new IllegalStateException("Objective bounds provider was already set")
		}
		this.boundsProvider = boundsProvider
	}
	
	protected def buildWithBounds(ExtendedLinearExpressionBuilder builder) {
		val bounds = builder.build(this)
		if (objective !== null && boundsProvider !== null) {
			boundsProvider.computeRequiredBounds(objective, bounds)
		}
		if (exact && bestUpper !== null) {
			bounds.tightenLowerBound(bestUpper)
		}
		bounds
	}
	
	override boundsSaturated(Integer lower, Integer upper) {
		if (upper !== null && (bestUpper === null || bestUpper < upper)) {
			bestUpper = upper
		}
		objective?.boundsSaturated(lower, upper)
	}
	
}