aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/SolutionStoreWithDiversityDescriptor.xtend
blob: a6e49d53501922ed12b77a5b2d5827a66378f7a1 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse

import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.PartialInterpretation2ImmutableTypeLattice
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.DiversityDescriptor
import java.util.LinkedList
import java.util.List
import org.eclipse.viatra.dse.base.ThreadContext
import java.util.HashSet
import java.util.Set

enum DiversityGranularity {
	Nodewise, Graphwise
}

class SolutionStoreWithDiversityDescriptor {
	val DiversityDescriptor descriptor
	DiversityGranularity granularity
	val PartialInterpretation2ImmutableTypeLattice solutionCoder = new PartialInterpretation2ImmutableTypeLattice
	val Set<Integer> solutionCodeList = new HashSet
	
	var long runtime
	var int allCheck
	var int successfulCheck
	
	new(DiversityDescriptor descriptor) {
		if(descriptor.ensureDiversity) {
			this.descriptor = descriptor
			this.granularity = DiversityGranularity::Nodewise
		} else {
			this.descriptor = null
			this.granularity = DiversityGranularity::Nodewise
		}
	}
	
	def public isActive() {
		descriptor!==null
	}
	
	def getSumRuntime() {
		return runtime
	}
	def getSuccessRate() {
		return successfulCheck as double / allCheck
	}
	
	def isDifferent(ThreadContext context) {
		if(active) {
			val start = System.nanoTime
			val model = context.model as PartialInterpretation
			var boolean isDifferent
			if(this.granularity == DiversityGranularity::Graphwise) {
				val code = solutionCoder.createRepresentation(model,
				descriptor.range,
				descriptor.parallels,
				descriptor.maxNumber,
				descriptor.relevantTypes,
				descriptor.relevantRelations).modelRepresentation.hashCode
			
				isDifferent = !solutionCodeList.contains(code)
			} else if(this.granularity == DiversityGranularity::Nodewise){
				val codes = solutionCoder.createRepresentation(model,
				descriptor.range,
				descriptor.parallels,
				descriptor.maxNumber,
				descriptor.relevantTypes,
				descriptor.relevantRelations).modelRepresentation.keySet.map[hashCode].toList
				val differentCodes = codes.filter[!solutionCodeList.contains(it)]
				//println(differentCodes.size)
				
				isDifferent = differentCodes.size>=3
				if(isDifferent)println(differentCodes.size)
			} else {
				throw new UnsupportedOperationException('''Unsupported diversity type: «this.granularity»''')
			}
			
			runtime += System.nanoTime - start
			allCheck++
			if(isDifferent) { successfulCheck++ }
			return isDifferent
		} else {
			allCheck++
			successfulCheck++
			return true
		}
	}
	
	def canBeDifferent(ThreadContext context) {
		return true
	}
	
	def newSolution(ThreadContext context) {
		if(active) {
			val start = System.nanoTime
			val model = context.model as PartialInterpretation
			if(this.granularity == DiversityGranularity::Graphwise) {
				val code = solutionCoder.createRepresentation(model,
				descriptor.range,
				descriptor.parallels,
				descriptor.maxNumber,
				descriptor.relevantTypes,
				descriptor.relevantRelations).modelRepresentation.hashCode
			
				solutionCodeList += code.hashCode
			} else if(this.granularity == DiversityGranularity::Nodewise){
				val codes = solutionCoder.createRepresentation(model,
				descriptor.range,
				descriptor.parallels,
				descriptor.maxNumber,
				descriptor.relevantTypes,
				descriptor.relevantRelations).modelRepresentation.keySet.map[hashCode].toList
				
				solutionCodeList += codes.map[it.hashCode]
			} else {
				throw new UnsupportedOperationException('''Unsupported diversity type: «this.granularity»''')
			}
			
			runtime += System.nanoTime - start
		}
	}
}