diff options
author | 2020-06-25 19:55:10 +0200 | |
---|---|---|
committer | 2020-06-25 19:55:10 +0200 | |
commit | c3a6d4b9cf3657070d180aa65ddbf0459e880329 (patch) | |
tree | 780c4fc61578dcb309af53fb0c164c7627e51676 /Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/cplex/CpsToLpTranslator.xtend | |
parent | New configuration language parser WIP (diff) | |
parent | Scope unsat benchmarks (diff) | |
download | VIATRA-Generator-c3a6d4b9cf3657070d180aa65ddbf0459e880329.tar.gz VIATRA-Generator-c3a6d4b9cf3657070d180aa65ddbf0459e880329.tar.zst VIATRA-Generator-c3a6d4b9cf3657070d180aa65ddbf0459e880329.zip |
Merge branch 'kris'
Diffstat (limited to 'Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/cplex/CpsToLpTranslator.xtend')
-rw-r--r-- | Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/cplex/CpsToLpTranslator.xtend | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/cplex/CpsToLpTranslator.xtend b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/cplex/CpsToLpTranslator.xtend new file mode 100644 index 00000000..c38af3a0 --- /dev/null +++ b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/cplex/CpsToLpTranslator.xtend | |||
@@ -0,0 +1,171 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.domains.cps.cplex | ||
2 | |||
3 | import com.google.common.collect.ImmutableList | ||
4 | import com.google.common.collect.ImmutableMap | ||
5 | import hu.bme.mit.inf.dslreasoner.domains.cps.ApplicationType | ||
6 | import hu.bme.mit.inf.dslreasoner.domains.cps.CyberPhysicalSystem | ||
7 | import hu.bme.mit.inf.dslreasoner.domains.cps.HostType | ||
8 | import hu.bme.mit.inf.dslreasoner.domains.cps.Requirement | ||
9 | import java.util.List | ||
10 | import java.util.Map | ||
11 | |||
12 | class CpsToLpTranslator { | ||
13 | static val MINIMUM_MEMORY_USAGE = 0.25 | ||
14 | static val MINIMUM_HDD_USAGE = 0.25 | ||
15 | |||
16 | val CyberPhysicalSystem cps | ||
17 | val Map<Requirement, ? extends List<String>> appInstances | ||
18 | val Map<HostType, ? extends List<String>> hostInstances | ||
19 | val boolean breakSymmetry | ||
20 | |||
21 | new(CyberPhysicalSystem cps, int hostInstanceCount, boolean breakSymmetry) { | ||
22 | this.cps = cps | ||
23 | appInstances = createAppInstances | ||
24 | hostInstances = createHostInstances(hostInstanceCount) | ||
25 | this.breakSymmetry = breakSymmetry | ||
26 | } | ||
27 | |||
28 | private def createAppInstances() { | ||
29 | val builder = ImmutableMap.builder | ||
30 | var int i = 0 | ||
31 | for (req : requirements) { | ||
32 | val listBuilder = ImmutableList.builder | ||
33 | for (var int j = 0; j < req.count; j++) { | ||
34 | listBuilder.add('''r«i»a«j»''') | ||
35 | } | ||
36 | builder.put(req, listBuilder.build) | ||
37 | i++ | ||
38 | } | ||
39 | builder.build | ||
40 | } | ||
41 | |||
42 | private def createHostInstances(int hostInstanceCount) { | ||
43 | val builder = ImmutableMap.builder | ||
44 | var int i = 0 | ||
45 | for (hostType : cps.hostTypes) { | ||
46 | val listBuilder = ImmutableList.builder | ||
47 | for (var int j = 0; j < hostInstanceCount; j++) { | ||
48 | listBuilder.add('''h«i»i«j»''') | ||
49 | } | ||
50 | builder.put(hostType, listBuilder.build) | ||
51 | i++ | ||
52 | } | ||
53 | builder.build | ||
54 | } | ||
55 | |||
56 | def getLpProblem() { | ||
57 | ''' | ||
58 | Minimize | ||
59 | total_cost: «objective» | ||
60 | Subject To | ||
61 | «constraints» | ||
62 | Bounds | ||
63 | «bounds» | ||
64 | Binary | ||
65 | «binaryVariables» | ||
66 | End | ||
67 | ''' | ||
68 | } | ||
69 | |||
70 | private def getObjective() { | ||
71 | '''«FOR pair : hostInstancesWithType SEPARATOR " + "»«pair.key.cost» «pair.value.existsVariable»«ENDFOR»''' | ||
72 | } | ||
73 | |||
74 | private def getConstraints() { | ||
75 | ''' | ||
76 | «FOR appPair : appInstancesWithType» | ||
77 | «appPair.value»_allocated: «FOR host : appPair.key.possibleHostInstances SEPARATOR " + "»«getAllocatedToVariable(appPair.value, host)»«ENDFOR» = 1 | ||
78 | «FOR host : appPair.key.possibleHostInstances» | ||
79 | «appPair.value»_to_«host»_exists: «host.existsVariable» - «getAllocatedToVariable(appPair.value, host)» >= 0 | ||
80 | «ENDFOR» | ||
81 | «ENDFOR» | ||
82 | «FOR hostPair : hostInstancesWithType» | ||
83 | «hostPair.value»_mem_use: «FOR appPair : hostPair.key.possibleAppInstancesWithRequirements SEPARATOR " + "»«appPair.key.requiredMemory» «getAllocatedToVariable(appPair.value, hostPair.value)»«ENDFOR» - «hostPair.key.defaultMemory» «hostPair.value.memoryUsageVariable» = 0 | ||
84 | «hostPair.value»_hdd_use: «FOR appPair : hostPair.key.possibleAppInstancesWithRequirements SEPARATOR " + "»«appPair.key.requiredHdd» «getAllocatedToVariable(appPair.value, hostPair.value)»«ENDFOR» - «hostPair.key.defaultHdd» «hostPair.value.hddUsageVariable» = 0 | ||
85 | «ENDFOR» | ||
86 | average_mem: «FOR host : allHostInstances SEPARATOR " + "»«host.memoryUsageVariable» - «MINIMUM_MEMORY_USAGE» «host.existsVariable»«ENDFOR» >= 0 | ||
87 | average_hdd: «FOR host : allHostInstances SEPARATOR " + "»«host.memoryUsageVariable» - «MINIMUM_HDD_USAGE» «host.existsVariable»«ENDFOR» >= 0 | ||
88 | «FOR reqPair : requirements.filter[count > 1].indexed» | ||
89 | «FOR host : reqPair.value.type.requirements.flatMap[hostInstances.get(hostType)]» | ||
90 | r«reqPair.key»_«host»_redundant: «FOR app : appInstances.get(reqPair.value) SEPARATOR " + "»«getAllocatedToVariable(app, host)»«ENDFOR» <= 1 | ||
91 | «ENDFOR» | ||
92 | «ENDFOR» | ||
93 | «IF breakSymmetry» | ||
94 | «FOR hosts : hostInstances.values» | ||
95 | «FOR i : 0 ..< (hosts.size - 1)» | ||
96 | «hosts.get(i + 1)»_after_«hosts.get(i)»: «hosts.get(i).existsVariable» - «hosts.get(i + 1).existsVariable» >= 0 | ||
97 | «ENDFOR» | ||
98 | «ENDFOR» | ||
99 | «ENDIF» | ||
100 | ''' | ||
101 | } | ||
102 | |||
103 | private def getBounds() { | ||
104 | ''' | ||
105 | «FOR host : allHostInstances» | ||
106 | 0 <= «host.memoryUsageVariable» <= 1 | ||
107 | 0 <= «host.hddUsageVariable» <= 1 | ||
108 | «ENDFOR» | ||
109 | ''' | ||
110 | } | ||
111 | |||
112 | private def getBinaryVariables() { | ||
113 | ''' | ||
114 | «FOR host : allHostInstances» | ||
115 | «host.existsVariable» | ||
116 | «ENDFOR» | ||
117 | «FOR appPair : appInstancesWithType» | ||
118 | «FOR host : appPair.key.possibleHostInstances» | ||
119 | «getAllocatedToVariable(appPair.value, host)» | ||
120 | «ENDFOR» | ||
121 | «ENDFOR» | ||
122 | ''' | ||
123 | } | ||
124 | |||
125 | private def getRequirements() { | ||
126 | cps.requests.flatMap[requirements] | ||
127 | } | ||
128 | |||
129 | private def getAllHostInstances() { | ||
130 | hostInstances.values.flatMap[it] | ||
131 | } | ||
132 | |||
133 | private def getHostInstancesWithType() { | ||
134 | hostInstances.entrySet.flatMap[pair|pair.value.map[pair.key -> it]] | ||
135 | } | ||
136 | |||
137 | private def getAppInstancesWithType() { | ||
138 | appInstances.entrySet.flatMap[pair|pair.value.map[pair.key.type -> it]] | ||
139 | } | ||
140 | |||
141 | private def getPossibleHostInstances(ApplicationType appType) { | ||
142 | appType.requirements.flatMap[req|hostInstances.get(req.hostType)] | ||
143 | } | ||
144 | |||
145 | private def getPossibleAppInstancesWithRequirements(HostType hostType) { | ||
146 | appInstances.entrySet.flatMap [ pair | | ||
147 | val resourceReq = pair.key.type.requirements.findFirst[it.hostType == hostType] | ||
148 | if (resourceReq === null) { | ||
149 | emptyList | ||
150 | } else { | ||
151 | pair.value.map[resourceReq -> it] | ||
152 | } | ||
153 | ] | ||
154 | } | ||
155 | |||
156 | private def getExistsVariable(String hostInstance) { | ||
157 | '''«hostInstance»_exists''' | ||
158 | } | ||
159 | |||
160 | private def getMemoryUsageVariable(String hostInstance) { | ||
161 | '''«hostInstance»_mem''' | ||
162 | } | ||
163 | |||
164 | private def getHddUsageVariable(String hostInstance) { | ||
165 | '''«hostInstance»_hdd''' | ||
166 | } | ||
167 | |||
168 | private def getAllocatedToVariable(String appInstance, String hostInstance) { | ||
169 | '''«appInstance»_to_«hostInstance»''' | ||
170 | } | ||
171 | } | ||