diff options
Diffstat (limited to 'Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains')
18 files changed, 1154 insertions, 0 deletions
diff --git a/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/cplex/CbcCpsMain.xtend b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/cplex/CbcCpsMain.xtend new file mode 100644 index 00000000..1a07e26e --- /dev/null +++ b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/cplex/CbcCpsMain.xtend | |||
@@ -0,0 +1,67 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.domains.cps.cplex | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.domains.cps.CpsPackage | ||
4 | import hu.bme.mit.inf.dslreasoner.domains.cps.generator.CpsGenerator | ||
5 | import java.io.BufferedReader | ||
6 | import java.io.BufferedWriter | ||
7 | import java.io.FileReader | ||
8 | import java.io.FileWriter | ||
9 | import java.util.concurrent.TimeUnit | ||
10 | import java.util.regex.Pattern | ||
11 | import org.eclipse.emf.ecore.EPackage | ||
12 | import org.eclipse.emf.ecore.resource.Resource | ||
13 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl | ||
14 | |||
15 | class CbcCpsMain { | ||
16 | static val PROBLEM_FILE = "problem.lp" | ||
17 | static val SOLUTION_FILE = "solution.txt" | ||
18 | static val VALUE_REGEX = Pattern.compile("Optimal - objective value\\s*([0-9]+(\\.[0-9]+)?)") | ||
19 | |||
20 | private new() { | ||
21 | new IllegalStateException("This is a static utility class and should not be instantiated directly.") | ||
22 | } | ||
23 | |||
24 | public static def void main(String[] args) { | ||
25 | Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put(Resource.Factory.Registry.DEFAULT_EXTENSION, | ||
26 | new XMIResourceFactoryImpl) | ||
27 | EPackage.Registry.INSTANCE.put(CpsPackage.eNS_URI, CpsPackage.eINSTANCE) | ||
28 | val generator = new CpsGenerator(1, 4, 1) | ||
29 | val problem = generator.generateCpsProblem | ||
30 | val toLp = new CpsToLpTranslator(problem, 10, true) | ||
31 | val lp = toLp.lpProblem | ||
32 | val writer = new BufferedWriter(new FileWriter(PROBLEM_FILE)) | ||
33 | try { | ||
34 | writer.append(lp) | ||
35 | } finally { | ||
36 | writer.close | ||
37 | } | ||
38 | val process = new ProcessBuilder().inheritIO.command("cbc", PROBLEM_FILE, "solve", "solu", SOLUTION_FILE).start | ||
39 | if (!process.waitFor(120, TimeUnit.SECONDS)) { | ||
40 | System.err.println("Timeout reached") | ||
41 | process.destroyForcibly | ||
42 | System.exit(-1) | ||
43 | } | ||
44 | if (process.exitValue != 0) { | ||
45 | System.err.println("Unexpected exit value " + process.exitValue) | ||
46 | System.exit(-1) | ||
47 | } | ||
48 | val reader = new BufferedReader(new FileReader(SOLUTION_FILE)) | ||
49 | var double value = Double.NaN | ||
50 | try { | ||
51 | var String line | ||
52 | while ((line = reader.readLine) !== null) { | ||
53 | println(line) | ||
54 | val matcher = VALUE_REGEX.matcher(line) | ||
55 | if (matcher.matches) { | ||
56 | value = Double.parseDouble(matcher.group(1)) | ||
57 | } | ||
58 | } | ||
59 | } finally { | ||
60 | reader.close | ||
61 | } | ||
62 | val applicationCost = problem.requests.flatMap[requirements.map[count]].reduce[p1, p2|p1 + p2] * 5 | ||
63 | val cost = applicationCost + value | ||
64 | println | ||
65 | println("Cost: " + cost) | ||
66 | } | ||
67 | } | ||
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 | } | ||
diff --git a/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/dse/CpsStateCoder.xtend b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/dse/CpsStateCoder.xtend new file mode 100644 index 00000000..223cee03 --- /dev/null +++ b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/dse/CpsStateCoder.xtend | |||
@@ -0,0 +1,134 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.domains.cps.dse | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.domains.cps.ApplicationInstance | ||
4 | import hu.bme.mit.inf.dslreasoner.domains.cps.CyberPhysicalSystem | ||
5 | import hu.bme.mit.inf.dslreasoner.domains.cps.HostInstance | ||
6 | import hu.bme.mit.inf.dslreasoner.domains.cps.HostType | ||
7 | import hu.bme.mit.inf.dslreasoner.domains.cps.Request | ||
8 | import hu.bme.mit.inf.dslreasoner.domains.cps.Requirement | ||
9 | import org.eclipse.emf.common.notify.Notifier | ||
10 | import org.eclipse.emf.ecore.resource.Resource | ||
11 | import org.eclipse.emf.ecore.resource.ResourceSet | ||
12 | import org.eclipse.viatra.dse.statecode.IStateCoder | ||
13 | import org.eclipse.viatra.dse.statecode.IStateCoderFactory | ||
14 | import org.eclipse.viatra.query.runtime.api.IPatternMatch | ||
15 | import org.eclipse.xtend2.lib.StringConcatenationClient | ||
16 | |||
17 | class CpsStateCoder implements IStateCoder { | ||
18 | CyberPhysicalSystem cps | ||
19 | |||
20 | protected new() { | ||
21 | } | ||
22 | |||
23 | override init(Notifier notifier) { | ||
24 | cps = switch (notifier) { | ||
25 | ResourceSet: getCpsFromResourceSet(notifier) | ||
26 | Resource: getCpsFromResource(notifier) | ||
27 | CyberPhysicalSystem: notifier | ||
28 | default: throw new IllegalArgumentException("notifier is not a CyberPhysicalSystem") | ||
29 | } | ||
30 | } | ||
31 | |||
32 | private def getCpsFromResourceSet(ResourceSet resourceSet) { | ||
33 | if (resourceSet.resources.empty) { | ||
34 | throw new IllegalArgumentException("No Resource in ResourceSet") | ||
35 | } | ||
36 | val resource = resourceSet.resources.head | ||
37 | getCpsFromResource(resource) | ||
38 | } | ||
39 | |||
40 | private def getCpsFromResource(Resource resource) { | ||
41 | if (resource.contents.empty) { | ||
42 | throw new IllegalArgumentException("No EObject in Resource") | ||
43 | } | ||
44 | val cps = resource.contents.head | ||
45 | if (cps instanceof CyberPhysicalSystem) { | ||
46 | cps | ||
47 | } else { | ||
48 | throw new IllegalArgumentException("EObject in Resource is not a CyberPhysicalSystem") | ||
49 | } | ||
50 | } | ||
51 | |||
52 | override String createStateCode() { | ||
53 | '''«createRequestsCode»«createHostTypesCode»''' | ||
54 | } | ||
55 | |||
56 | private def StringConcatenationClient createRequestsCode() { | ||
57 | '''«FOR request : cps.requests»«createRequestCode(request)»«ENDFOR»''' | ||
58 | } | ||
59 | |||
60 | private def StringConcatenationClient createRequestCode(Request request) { | ||
61 | '''[«FOR requirement : request.requirements»«createRequirementCode(requirement)»«ENDFOR»]''' | ||
62 | } | ||
63 | |||
64 | private def StringConcatenationClient createRequirementCode(Requirement requirement) { | ||
65 | '''[«FOR app : requirement.instances SEPARATOR ","»«createAppCode(app)»«ENDFOR»]''' | ||
66 | } | ||
67 | |||
68 | private def createAppCode(ApplicationInstance app) { | ||
69 | if (app.allocatedTo === null) { | ||
70 | "-" | ||
71 | } else { | ||
72 | createMatchArgumentCode(app.allocatedTo) | ||
73 | } | ||
74 | } | ||
75 | |||
76 | private def createHostTypesCode() { | ||
77 | '''(«FOR hostType : cps.hostTypes SEPARATOR ","»«hostType.instances.size»«ENDFOR»)''' | ||
78 | } | ||
79 | |||
80 | override String createActivationCode(IPatternMatch match) { | ||
81 | '''«match.specification.simpleName»(«FOR arg : match.toArray SEPARATOR ","»«createMatchArgumentCode(arg)»«ENDFOR»)''' | ||
82 | } | ||
83 | |||
84 | protected dispatch def String createMatchArgumentCode(Requirement requirement) { | ||
85 | val request = requirement.eContainer | ||
86 | if (request instanceof Request) { | ||
87 | if (request.eContainer != cps) { | ||
88 | throw new IllegalArgumentException("Request is not contained in the CPS") | ||
89 | } | ||
90 | val requestIndex = cps.requests.indexOf(request) | ||
91 | val requirementIndex = request.requirements.indexOf(requirement) | ||
92 | requestIndex + "." + requirementIndex | ||
93 | } else { | ||
94 | throw new IllegalArgumentException("Requirement is not contained in a request") | ||
95 | } | ||
96 | } | ||
97 | |||
98 | protected dispatch def String createMatchArgumentCode(ApplicationInstance app) { | ||
99 | val requirement = app.requirement | ||
100 | if (requirement === null) { | ||
101 | throw new IllegalArgumentException("Application instance is not associated with a requirement") | ||
102 | } | ||
103 | val instanceIndex = requirement.instances.indexOf(app) | ||
104 | createMatchArgumentCode(requirement) + "." + instanceIndex | ||
105 | } | ||
106 | |||
107 | protected dispatch def String createMatchArgumentCode(HostInstance host) { | ||
108 | val hostType = host.eContainer | ||
109 | if (hostType instanceof HostType) { | ||
110 | val hostIndex = hostType.instances.indexOf(host) | ||
111 | createMatchArgumentCode(hostType) + "." + hostIndex | ||
112 | } else { | ||
113 | throw new IllegalArgumentException("Host is not contained in a host type") | ||
114 | } | ||
115 | } | ||
116 | |||
117 | protected dispatch def String createMatchArgumentCode(HostType hostType) { | ||
118 | if (hostType.eContainer != cps) { | ||
119 | throw new IllegalArgumentException("Host type is not contained in the CPS") | ||
120 | } | ||
121 | val hostTypeIndex = cps.hostTypes.indexOf(hostType) | ||
122 | hostTypeIndex.toString | ||
123 | } | ||
124 | |||
125 | protected dispatch def createMatchArgumentCode(Object object) { | ||
126 | throw new IllegalArgumentException("Unknown match argument: ") | ||
127 | } | ||
128 | |||
129 | static class Factory implements IStateCoderFactory { | ||
130 | override createStateCoder() { | ||
131 | new CpsStateCoder | ||
132 | } | ||
133 | } | ||
134 | } | ||
diff --git a/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/dse/RuleBasedCpsMain.xtend b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/dse/RuleBasedCpsMain.xtend new file mode 100644 index 00000000..35b3b1df --- /dev/null +++ b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/dse/RuleBasedCpsMain.xtend | |||
@@ -0,0 +1,39 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.domains.cps.dse | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.domains.cps.CpsPackage | ||
4 | import hu.bme.mit.inf.dslreasoner.domains.cps.generator.CpsGenerator | ||
5 | import hu.bme.mit.inf.dslreasoner.domains.cps.queries.CpsQueries | ||
6 | import org.eclipse.emf.ecore.EPackage | ||
7 | import org.eclipse.emf.ecore.EStructuralFeature | ||
8 | import org.eclipse.emf.ecore.resource.Resource | ||
9 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl | ||
10 | import org.eclipse.viatra.addon.querybasedfeatures.runtime.QueryBasedFeatureSettingDelegateFactory | ||
11 | import org.eclipse.viatra.addon.querybasedfeatures.runtime.handler.QueryBasedFeatures | ||
12 | import org.eclipse.viatra.dse.api.DesignSpaceExplorer | ||
13 | import org.eclipse.viatra.dse.api.DesignSpaceExplorer.DseLoggingLevel | ||
14 | import org.eclipse.viatra.query.runtime.extensibility.SingletonQueryGroupProvider | ||
15 | import org.eclipse.viatra.query.runtime.registry.QuerySpecificationRegistry | ||
16 | import org.eclipse.viatra.query.runtime.registry.connector.QueryGroupProviderSourceConnector | ||
17 | |||
18 | class RuleBasedCpsMain { | ||
19 | private new() { | ||
20 | new IllegalStateException("This is a static utility class and should not be instantiated directly.") | ||
21 | } | ||
22 | |||
23 | public static def void main(String[] args) { | ||
24 | DesignSpaceExplorer.turnOnLogging(DseLoggingLevel.VERBOSE_FULL) | ||
25 | Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put(Resource.Factory.Registry.DEFAULT_EXTENSION, | ||
26 | new XMIResourceFactoryImpl) | ||
27 | EStructuralFeature.Internal.SettingDelegate.Factory.Registry.INSTANCE.put(QueryBasedFeatures.ANNOTATION_SOURCE, | ||
28 | new QueryBasedFeatureSettingDelegateFactory) | ||
29 | EPackage.Registry.INSTANCE.put(CpsPackage.eNS_URI, CpsPackage.eINSTANCE) | ||
30 | QuerySpecificationRegistry.instance.addSource( | ||
31 | new QueryGroupProviderSourceConnector("CpsQueries", new SingletonQueryGroupProvider(CpsQueries.instance), | ||
32 | true)) | ||
33 | val generator = new CpsGenerator(1, 4, 1) | ||
34 | val problem = generator.generateCpsProblem | ||
35 | // problem.eResource.save(emptyMap) | ||
36 | val solver = new RuleBasedCpsSolver | ||
37 | solver.solve(problem) | ||
38 | } | ||
39 | } | ||
diff --git a/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/dse/RuleBasedCpsSolver.xtend b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/dse/RuleBasedCpsSolver.xtend new file mode 100644 index 00000000..503c06ea --- /dev/null +++ b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/dse/RuleBasedCpsSolver.xtend | |||
@@ -0,0 +1,74 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.domains.cps.dse | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.domains.cps.CpsFactory | ||
4 | import hu.bme.mit.inf.dslreasoner.domains.cps.CpsPackage | ||
5 | import hu.bme.mit.inf.dslreasoner.domains.cps.CyberPhysicalSystem | ||
6 | import hu.bme.mit.inf.dslreasoner.domains.cps.queries.Allocate | ||
7 | import hu.bme.mit.inf.dslreasoner.domains.cps.queries.AverageFreeHddMetric | ||
8 | import hu.bme.mit.inf.dslreasoner.domains.cps.queries.AverageFreeMemoryMetric | ||
9 | import hu.bme.mit.inf.dslreasoner.domains.cps.queries.CostMetric | ||
10 | import hu.bme.mit.inf.dslreasoner.domains.cps.queries.CreateHostInstance | ||
11 | import hu.bme.mit.inf.dslreasoner.domains.cps.queries.GuidanceObjective | ||
12 | import hu.bme.mit.inf.dslreasoner.domains.cps.queries.RequirementNotSatisfied | ||
13 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.CompositeDirectionalThresholdObjective | ||
14 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.ObjectiveKind | ||
15 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.ObjectiveThreshold | ||
16 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.QueryBasedObjective | ||
17 | import org.eclipse.viatra.dse.api.DesignSpaceExplorer | ||
18 | import org.eclipse.viatra.dse.evolutionary.EvolutionaryStrategyBuilder | ||
19 | import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRuleFactory | ||
20 | |||
21 | class RuleBasedCpsSolver { | ||
22 | extension val BatchTransformationRuleFactory = new BatchTransformationRuleFactory | ||
23 | extension val CpsFactory = CpsFactory.eINSTANCE | ||
24 | |||
25 | def solve(CyberPhysicalSystem problem) { | ||
26 | // for (request : problem.requests) { | ||
27 | // for (req : request.requirements) { | ||
28 | // for (i : 0 ..< req.count) { | ||
29 | // val app = createApplicationInstance | ||
30 | // req.type.instances += app | ||
31 | // req.instances += app | ||
32 | // } | ||
33 | // } | ||
34 | // } | ||
35 | val dse = new DesignSpaceExplorer | ||
36 | dse.addMetaModelPackage(CpsPackage.eINSTANCE) | ||
37 | dse.initialModel = problem.eResource.resourceSet | ||
38 | dse.addTransformationRule(createRule.precondition(RequirementNotSatisfied.instance).action [ | ||
39 | val app = createApplicationInstance | ||
40 | req.type.instances += app | ||
41 | req.instances += app | ||
42 | ].build) | ||
43 | dse.addTransformationRule(createRule.precondition(Allocate.instance).action [ | ||
44 | app.allocatedTo = host | ||
45 | ].build) | ||
46 | // dse.addTransformationRule(createRule(UnallocateAppInstance.instance).action [ | ||
47 | // app.allocatedTo = null | ||
48 | // ].build) | ||
49 | dse.addTransformationRule(createRule.precondition(CreateHostInstance.instance).action [ | ||
50 | hostType.instances += createHostInstance | ||
51 | ].build) | ||
52 | // dse.addTransformationRule(createRule(RemoveHostInstance.instance).action [ | ||
53 | // hostInstance.type.instances -= hostInstance | ||
54 | // ].build) | ||
55 | dse.addObjective( | ||
56 | new CompositeDirectionalThresholdObjective("Composite", | ||
57 | new QueryBasedObjective(GuidanceObjective.instance, ObjectiveKind.LOWER_IS_BETTER, | ||
58 | new ObjectiveThreshold.Inclusive(0), 0), | ||
59 | new QueryBasedObjective(AverageFreeMemoryMetric.instance, ObjectiveKind.LOWER_IS_BETTER, | ||
60 | new ObjectiveThreshold.Inclusive(0.75), 0), | ||
61 | new QueryBasedObjective(AverageFreeHddMetric.instance, ObjectiveKind.LOWER_IS_BETTER, | ||
62 | new ObjectiveThreshold.Inclusive(0.75), 0))) | ||
63 | dse.addObjective( | ||
64 | new QueryBasedObjective(CostMetric.instance, ObjectiveKind.LOWER_IS_BETTER, | ||
65 | ObjectiveThreshold.NO_THRESHOLD, 0)) | ||
66 | dse.maxNumberOfThreads = 1 | ||
67 | dse.stateCoderFactory = new CpsStateCoder.Factory | ||
68 | val strategy = EvolutionaryStrategyBuilder.createNsga2Strategy(25) | ||
69 | dse.startExplorationWithTimeout(strategy, 2 * 60 * 1000) | ||
70 | for (solution : dse.solutions) { | ||
71 | println("Found solution: " + solution.stateCode + " " + solution.arbitraryTrajectory.fitness) | ||
72 | } | ||
73 | } | ||
74 | } | ||
diff --git a/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/generator/CpsGenerator.xtend b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/generator/CpsGenerator.xtend new file mode 100644 index 00000000..e8d29949 --- /dev/null +++ b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/generator/CpsGenerator.xtend | |||
@@ -0,0 +1,117 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.domains.cps.generator | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.domains.cps.CpsFactory | ||
4 | import hu.bme.mit.inf.dslreasoner.domains.cps.CyberPhysicalSystem | ||
5 | import hu.bme.mit.inf.dslreasoner.domains.cps.HostType | ||
6 | import java.util.Collection | ||
7 | import java.util.Random | ||
8 | import org.eclipse.emf.common.util.URI | ||
9 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl | ||
10 | |||
11 | class CpsGenerator { | ||
12 | extension val CpsFactory = CpsFactory.eINSTANCE | ||
13 | |||
14 | static val MIN_MEMORY = 1 | ||
15 | static val MAX_MEMORY = 6 | ||
16 | static val MIN_HDD = 1 | ||
17 | static val MAX_HDD = 30 | ||
18 | static val HIGH_CPU_FRACTION = 4 | ||
19 | static val MIN_REPLICAS = 1 | ||
20 | static val MAX_REPLICAS = 4 | ||
21 | |||
22 | val Random random | ||
23 | val int applicationTypeCount | ||
24 | val int demandFactor | ||
25 | val boolean populateAppInstances | ||
26 | |||
27 | new(long randomSeed, int applicationTypeCount, int demandFactor) { | ||
28 | this(randomSeed, applicationTypeCount, demandFactor, false) | ||
29 | } | ||
30 | |||
31 | new(long randomSeed, int applicationTypeCount, int demandFactor, boolean populateAppInstances) { | ||
32 | this.random = new Random(randomSeed) | ||
33 | this.applicationTypeCount = applicationTypeCount | ||
34 | this.demandFactor = demandFactor | ||
35 | this.populateAppInstances = populateAppInstances | ||
36 | } | ||
37 | |||
38 | def generateCpsProblem() { | ||
39 | val resourceSet = new ResourceSetImpl | ||
40 | val resource = resourceSet.createResource(URI.createFileURI("dummy.dummyext")) | ||
41 | createCyberPhysicalSystem => [ | ||
42 | val cps = it | ||
43 | resource.contents += cps | ||
44 | createLowCpuHostTypes | ||
45 | val highCpuHostTypes = createHighCpuHostTypes | ||
46 | for (var int i = 0; i < applicationTypeCount; i++) { | ||
47 | if (i % HIGH_CPU_FRACTION == 0) { | ||
48 | createRandomApplicationType(highCpuHostTypes) | ||
49 | } else { | ||
50 | createRandomApplicationType(hostTypes) | ||
51 | } | ||
52 | } | ||
53 | for (var int i = 0; i < demandFactor; i++) { | ||
54 | requests += createRequest => [ | ||
55 | for (appType : cps.applicationTypes) { | ||
56 | requirements += createRequirement => [ | ||
57 | count = nextInt(CpsGenerator.MIN_REPLICAS, CpsGenerator.MAX_REPLICAS) | ||
58 | type = appType | ||
59 | if (populateAppInstances) { | ||
60 | for (j : 0 ..< count) { | ||
61 | val app = createApplicationInstance | ||
62 | app.type = appType | ||
63 | appType.instances += app | ||
64 | instances += app | ||
65 | } | ||
66 | } | ||
67 | ] | ||
68 | } | ||
69 | ] | ||
70 | } | ||
71 | ] | ||
72 | } | ||
73 | |||
74 | private def void createRandomApplicationType(CyberPhysicalSystem it, Collection<HostType> allowedHostTypes) { | ||
75 | val appType = createApplicationType | ||
76 | val memory = nextInt(MIN_MEMORY, MAX_MEMORY) | ||
77 | val hdd = nextInt(MIN_HDD, MAX_HDD) | ||
78 | for (hostType : allowedHostTypes) { | ||
79 | appType.requirements += createResourceRequirement => [ | ||
80 | it.hostType = hostType | ||
81 | requiredMemory = memory | ||
82 | requiredHdd = hdd | ||
83 | ] | ||
84 | } | ||
85 | applicationTypes += appType | ||
86 | } | ||
87 | |||
88 | private def createLowCpuHostTypes(CyberPhysicalSystem it) { | ||
89 | #[ | ||
90 | createHostType(2, 8, 75), // m5d.large | ||
91 | createHostType(4, 16, 150), // m5d.xlarge | ||
92 | createHostType(3, 16, 75), // r5d.large | ||
93 | createHostType(6, 32, 150) // r5d.xlarge | ||
94 | ] | ||
95 | } | ||
96 | |||
97 | private def createHighCpuHostTypes(CyberPhysicalSystem it) { | ||
98 | #[ | ||
99 | createHostType(2, 4, 50), // c5d.large | ||
100 | createHostType(4, 8, 100) // c5d.xlarge | ||
101 | ] | ||
102 | } | ||
103 | |||
104 | private def createHostType(CyberPhysicalSystem it, int cost, int memory, int hdd) { | ||
105 | val hostType = createHostType => [ | ||
106 | it.cost = cost | ||
107 | defaultMemory = memory | ||
108 | defaultHdd = hdd | ||
109 | ] | ||
110 | hostTypes += hostType | ||
111 | hostType | ||
112 | } | ||
113 | |||
114 | private def nextInt(int lower, int upper) { | ||
115 | lower + random.nextInt(upper - lower + 1) | ||
116 | } | ||
117 | } | ||
diff --git a/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/CpsMdeOptimiserMain.xtend b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/CpsMdeOptimiserMain.xtend new file mode 100644 index 00000000..4a4b7a87 --- /dev/null +++ b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/CpsMdeOptimiserMain.xtend | |||
@@ -0,0 +1,57 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.domains.cps.mdeo | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.domains.cps.CpsPackage | ||
4 | import hu.bme.mit.inf.dslreasoner.domains.cps.generator.CpsGenerator | ||
5 | import java.io.BufferedReader | ||
6 | import java.io.BufferedWriter | ||
7 | import java.io.FileReader | ||
8 | import java.io.FileWriter | ||
9 | import java.util.Map | ||
10 | import org.eclipse.emf.common.util.URI | ||
11 | import org.eclipse.emf.ecore.EPackage | ||
12 | import org.eclipse.emf.ecore.resource.Resource | ||
13 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl | ||
14 | import uk.ac.kcl.inf.mdeoptimiser.interfaces.cli.Run | ||
15 | |||
16 | class CpsMdeOptimiserMain { | ||
17 | static val PROJECT_PATH = "." | ||
18 | static val PROBLEM_PATH = "model/problem.xmi" | ||
19 | static val MOPT_PATH = "src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/cps.mopt" | ||
20 | |||
21 | private new() { | ||
22 | new IllegalStateException("This is a static utility class and should not be instantiated directly.") | ||
23 | } | ||
24 | |||
25 | public static def void main(String[] args) { | ||
26 | Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put(Resource.Factory.Registry.DEFAULT_EXTENSION, | ||
27 | new XMIResourceFactoryImpl) | ||
28 | EPackage.Registry.INSTANCE.put(CpsPackage.eNS_URI, CpsPackage.eINSTANCE) | ||
29 | val generator = new CpsGenerator(1, 4, 1) | ||
30 | val problem = generator.generateCpsProblem | ||
31 | problem.eResource.URI = URI.createFileURI(PROBLEM_PATH) | ||
32 | problem.eResource.save(emptyMap) | ||
33 | fixupHenshinModel("model/cps.henshin", "model/cps_fixup.henshin", #{"cps.ecore" -> CpsPackage.eNS_URI}) | ||
34 | Run.main(#["-p", PROJECT_PATH, "-m", MOPT_PATH]) | ||
35 | } | ||
36 | |||
37 | private def static void fixupHenshinModel(String originalPath, String outputPath, Map<String, String> remapMap) { | ||
38 | val reader = new BufferedReader(new FileReader(originalPath)) | ||
39 | try { | ||
40 | val writer = new BufferedWriter(new FileWriter(outputPath)) | ||
41 | try { | ||
42 | var String line | ||
43 | while ((line = reader.readLine) !== null) { | ||
44 | for (entry : remapMap.entrySet) { | ||
45 | line = line.replace(entry.key, entry.value) | ||
46 | } | ||
47 | writer.write(line) | ||
48 | writer.write("\n") | ||
49 | } | ||
50 | } finally { | ||
51 | writer.close | ||
52 | } | ||
53 | } finally { | ||
54 | reader.close | ||
55 | } | ||
56 | } | ||
57 | } | ||
diff --git a/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/NonRedundantAllocationsConstraint.xtend b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/NonRedundantAllocationsConstraint.xtend new file mode 100644 index 00000000..1a9286b3 --- /dev/null +++ b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/NonRedundantAllocationsConstraint.xtend | |||
@@ -0,0 +1,29 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.domains.cps.mdeo | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.domains.cps.CyberPhysicalSystem | ||
4 | import uk.ac.kcl.inf.mdeoptimiser.libraries.core.optimisation.IGuidanceFunction | ||
5 | import uk.ac.kcl.inf.mdeoptimiser.libraries.core.optimisation.interpreter.guidance.Solution | ||
6 | import com.google.common.collect.HashMultiset | ||
7 | |||
8 | class NonRedundantAllocationsConstraint implements IGuidanceFunction { | ||
9 | override getName() { | ||
10 | "NonRedundantAllocations" | ||
11 | } | ||
12 | |||
13 | override computeFitness(Solution solution) { | ||
14 | val cps = solution.model as CyberPhysicalSystem | ||
15 | var int cost = 0 | ||
16 | for (hostType : cps.hostTypes) { | ||
17 | for (host : hostType.instances) { | ||
18 | val bins = HashMultiset.create | ||
19 | for (app : host.applications) { | ||
20 | bins.add(app.requirement) | ||
21 | } | ||
22 | for (entry : bins.entrySet) { | ||
23 | cost += entry.count - 1 | ||
24 | } | ||
25 | } | ||
26 | } | ||
27 | cost | ||
28 | } | ||
29 | } | ||
diff --git a/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/NotAllocatedAppInstancesConstraint.xtend b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/NotAllocatedAppInstancesConstraint.xtend new file mode 100644 index 00000000..663aa26c --- /dev/null +++ b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/NotAllocatedAppInstancesConstraint.xtend | |||
@@ -0,0 +1,24 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.domains.cps.mdeo | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.domains.cps.CyberPhysicalSystem | ||
4 | import uk.ac.kcl.inf.mdeoptimiser.libraries.core.optimisation.IGuidanceFunction | ||
5 | import uk.ac.kcl.inf.mdeoptimiser.libraries.core.optimisation.interpreter.guidance.Solution | ||
6 | |||
7 | class NotAllocatedAppInstancesConstraint implements IGuidanceFunction { | ||
8 | override getName() { | ||
9 | "NotAllocatedAppInstances" | ||
10 | } | ||
11 | |||
12 | override computeFitness(Solution solution) { | ||
13 | val cps = solution.model as CyberPhysicalSystem | ||
14 | var int cost = 0 | ||
15 | for (appType : cps.applicationTypes) { | ||
16 | for (app : appType.instances) { | ||
17 | if (app.allocatedTo === null || !appType.requirements.exists[hostType == app.allocatedTo.type]) { | ||
18 | cost++ | ||
19 | } | ||
20 | } | ||
21 | } | ||
22 | cost | ||
23 | } | ||
24 | } | ||
diff --git a/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/NotSatisfiedRequirementsConstraint.xtend b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/NotSatisfiedRequirementsConstraint.xtend new file mode 100644 index 00000000..e44381ec --- /dev/null +++ b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/NotSatisfiedRequirementsConstraint.xtend | |||
@@ -0,0 +1,27 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.domains.cps.mdeo | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.domains.cps.CyberPhysicalSystem | ||
4 | import uk.ac.kcl.inf.mdeoptimiser.libraries.core.optimisation.IGuidanceFunction | ||
5 | import uk.ac.kcl.inf.mdeoptimiser.libraries.core.optimisation.interpreter.guidance.Solution | ||
6 | |||
7 | class NotSatisfiedRequirementsConstraint implements IGuidanceFunction { | ||
8 | override getName() { | ||
9 | "NotSatisfiedRequirements" | ||
10 | } | ||
11 | |||
12 | override computeFitness(Solution solution) { | ||
13 | val cps = solution.model as CyberPhysicalSystem | ||
14 | var int cost = 0 | ||
15 | for (request : cps.requests) { | ||
16 | for (requirement : request.requirements) { | ||
17 | cost += Math.abs(requirement.count - requirement.instances.size) | ||
18 | for (app : requirement.instances) { | ||
19 | if (app.type != requirement.type) { | ||
20 | cost++ | ||
21 | } | ||
22 | } | ||
23 | } | ||
24 | } | ||
25 | cost | ||
26 | } | ||
27 | } | ||
diff --git a/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/ResourceUtilizationUtil.xtend b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/ResourceUtilizationUtil.xtend new file mode 100644 index 00000000..fc1d666f --- /dev/null +++ b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/ResourceUtilizationUtil.xtend | |||
@@ -0,0 +1,31 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.domains.cps.mdeo | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.domains.cps.HostInstance | ||
4 | |||
5 | class ResourceUtilizationUtil { | ||
6 | private new() { | ||
7 | new IllegalStateException("This is a static utility class and should not be instantiated directly.") | ||
8 | } | ||
9 | |||
10 | static def getMemoryUtilization(HostInstance host) { | ||
11 | var int utilization = 0 | ||
12 | for (app : host.applications) { | ||
13 | val req = app.type.requirements.findFirst[hostType == host.type] | ||
14 | if (req !== null) { | ||
15 | utilization += req.requiredMemory | ||
16 | } | ||
17 | } | ||
18 | utilization | ||
19 | } | ||
20 | |||
21 | static def getHddUtilization(HostInstance host) { | ||
22 | var int utilization = 0 | ||
23 | for (app : host.applications) { | ||
24 | val req = app.type.requirements.findFirst[hostType == host.type] | ||
25 | if (req !== null) { | ||
26 | utilization += req.requiredHdd | ||
27 | } | ||
28 | } | ||
29 | utilization | ||
30 | } | ||
31 | } | ||
diff --git a/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/TooLowAverageHddConstraint.xtend b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/TooLowAverageHddConstraint.xtend new file mode 100644 index 00000000..85cc8115 --- /dev/null +++ b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/TooLowAverageHddConstraint.xtend | |||
@@ -0,0 +1,33 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.domains.cps.mdeo | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.domains.cps.CyberPhysicalSystem | ||
4 | import uk.ac.kcl.inf.mdeoptimiser.libraries.core.optimisation.IGuidanceFunction | ||
5 | import uk.ac.kcl.inf.mdeoptimiser.libraries.core.optimisation.interpreter.guidance.Solution | ||
6 | |||
7 | class TooLowAverageHddConstraint implements IGuidanceFunction { | ||
8 | static val THRESHOLD = 0.25 | ||
9 | |||
10 | override getName() { | ||
11 | "TooLowAverageHdd" | ||
12 | } | ||
13 | |||
14 | override computeFitness(Solution solution) { | ||
15 | val cps = solution.model as CyberPhysicalSystem | ||
16 | var double sumUtilization | ||
17 | var int numHosts | ||
18 | for (hostType : cps.hostTypes) { | ||
19 | numHosts += hostType.instances.size | ||
20 | for (host : hostType.instances) { | ||
21 | val utilization = ResourceUtilizationUtil.getHddUtilization(host) | ||
22 | sumUtilization += (utilization as double) / hostType.defaultHdd | ||
23 | } | ||
24 | } | ||
25 | val averageUtilization = sumUtilization / numHosts | ||
26 | val difference = THRESHOLD - averageUtilization | ||
27 | if (difference > 0) { | ||
28 | difference | ||
29 | } else { | ||
30 | 0 | ||
31 | } | ||
32 | } | ||
33 | } | ||
diff --git a/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/TooLowAverageMemoryConstraint.xtend b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/TooLowAverageMemoryConstraint.xtend new file mode 100644 index 00000000..e9b47d4c --- /dev/null +++ b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/TooLowAverageMemoryConstraint.xtend | |||
@@ -0,0 +1,33 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.domains.cps.mdeo | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.domains.cps.CyberPhysicalSystem | ||
4 | import uk.ac.kcl.inf.mdeoptimiser.libraries.core.optimisation.IGuidanceFunction | ||
5 | import uk.ac.kcl.inf.mdeoptimiser.libraries.core.optimisation.interpreter.guidance.Solution | ||
6 | |||
7 | class TooLowAverageMemoryConstraint implements IGuidanceFunction { | ||
8 | static val THRESHOLD = 0.25 | ||
9 | |||
10 | override getName() { | ||
11 | "TooLowAverageMemory" | ||
12 | } | ||
13 | |||
14 | override computeFitness(Solution solution) { | ||
15 | val cps = solution.model as CyberPhysicalSystem | ||
16 | var double sumUtilization | ||
17 | var int numHosts | ||
18 | for (hostType : cps.hostTypes) { | ||
19 | numHosts += hostType.instances.size | ||
20 | for (host : hostType.instances) { | ||
21 | val utilization = ResourceUtilizationUtil.getMemoryUtilization(host) | ||
22 | sumUtilization += (utilization as double) / hostType.defaultMemory | ||
23 | } | ||
24 | } | ||
25 | val averageUtilization = sumUtilization / numHosts | ||
26 | val difference = THRESHOLD - averageUtilization | ||
27 | if (difference > 0) { | ||
28 | difference | ||
29 | } else { | ||
30 | 0 | ||
31 | } | ||
32 | } | ||
33 | } | ||
diff --git a/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/TotalCostFitnessFunction.xtend b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/TotalCostFitnessFunction.xtend new file mode 100644 index 00000000..af65e442 --- /dev/null +++ b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/TotalCostFitnessFunction.xtend | |||
@@ -0,0 +1,23 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.domains.cps.mdeo | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.domains.cps.CyberPhysicalSystem | ||
4 | import uk.ac.kcl.inf.mdeoptimiser.libraries.core.optimisation.IGuidanceFunction | ||
5 | import uk.ac.kcl.inf.mdeoptimiser.libraries.core.optimisation.interpreter.guidance.Solution | ||
6 | |||
7 | class TotalCostFitnessFunction implements IGuidanceFunction { | ||
8 | override getName() { | ||
9 | "TotalCost" | ||
10 | } | ||
11 | |||
12 | override computeFitness(Solution solution) { | ||
13 | val cps = solution.model as CyberPhysicalSystem | ||
14 | var int cost = 0 | ||
15 | for (appType : cps.applicationTypes) { | ||
16 | cost += 5 * appType.instances.size | ||
17 | } | ||
18 | for (hostType : cps.hostTypes) { | ||
19 | cost += hostType.cost * hostType.instances.size | ||
20 | } | ||
21 | cost | ||
22 | } | ||
23 | } | ||
diff --git a/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/UnavailableHddConstraint.xtend b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/UnavailableHddConstraint.xtend new file mode 100644 index 00000000..08450f45 --- /dev/null +++ b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/UnavailableHddConstraint.xtend | |||
@@ -0,0 +1,27 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.domains.cps.mdeo | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.domains.cps.CyberPhysicalSystem | ||
4 | import uk.ac.kcl.inf.mdeoptimiser.libraries.core.optimisation.IGuidanceFunction | ||
5 | import uk.ac.kcl.inf.mdeoptimiser.libraries.core.optimisation.interpreter.guidance.Solution | ||
6 | |||
7 | class UnavailableHddConstraint implements IGuidanceFunction { | ||
8 | override getName() { | ||
9 | "UnavailableHdd" | ||
10 | } | ||
11 | |||
12 | override computeFitness(Solution solution) { | ||
13 | val cps = solution.model as CyberPhysicalSystem | ||
14 | var int cost = 0 | ||
15 | for (hostType : cps.hostTypes) { | ||
16 | for (host : hostType.instances) { | ||
17 | val utilization = ResourceUtilizationUtil.getHddUtilization(host) | ||
18 | val difference = utilization - hostType.defaultHdd | ||
19 | if (difference > 0) { | ||
20 | cost += difference | ||
21 | } | ||
22 | } | ||
23 | } | ||
24 | cost | ||
25 | } | ||
26 | |||
27 | } | ||
diff --git a/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/UnavailableMemoryConstraint.xtend b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/UnavailableMemoryConstraint.xtend new file mode 100644 index 00000000..e46d59a6 --- /dev/null +++ b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/UnavailableMemoryConstraint.xtend | |||
@@ -0,0 +1,27 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.domains.cps.mdeo | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.domains.cps.CyberPhysicalSystem | ||
4 | import uk.ac.kcl.inf.mdeoptimiser.libraries.core.optimisation.IGuidanceFunction | ||
5 | import uk.ac.kcl.inf.mdeoptimiser.libraries.core.optimisation.interpreter.guidance.Solution | ||
6 | |||
7 | class UnavailableMemoryConstraint implements IGuidanceFunction { | ||
8 | override getName() { | ||
9 | "UnavailableMemory" | ||
10 | } | ||
11 | |||
12 | override computeFitness(Solution solution) { | ||
13 | val cps = solution.model as CyberPhysicalSystem | ||
14 | var int cost = 0 | ||
15 | for (hostType : cps.hostTypes) { | ||
16 | for (host : hostType.instances) { | ||
17 | val utilization = ResourceUtilizationUtil.getMemoryUtilization(host) | ||
18 | val difference = utilization - hostType.defaultMemory | ||
19 | if (difference > 0) { | ||
20 | cost += difference | ||
21 | } | ||
22 | } | ||
23 | } | ||
24 | cost | ||
25 | } | ||
26 | |||
27 | } | ||
diff --git a/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/cps.mopt b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/cps.mopt new file mode 100644 index 00000000..67fe7508 --- /dev/null +++ b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/mdeo/cps.mopt | |||
@@ -0,0 +1,43 @@ | |||
1 | problem { | ||
2 | basepath <model> | ||
3 | metamodel <hu.bme.mit.inf.dslreasoner.domains.cps.CpsPackage> | ||
4 | model <problem.xmi> | ||
5 | } | ||
6 | |||
7 | goal { | ||
8 | objective TotalCost minimise java { "hu.bme.mit.inf.dslreasoner.domains.cps.mdeo.TotalCostFitnessFunction" } | ||
9 | constraint NotSatisfiedRequriements java { "hu.bme.mit.inf.dslreasoner.domains.cps.mdeo.NotSatisfiedRequirementsConstraint" } | ||
10 | constraint NotAllocatedAppInstances java { "hu.bme.mit.inf.dslreasoner.domains.cps.mdeo.NotAllocatedAppInstancesConstraint" } | ||
11 | constraint NonRedundantAllocations java { "hu.bme.mit.inf.dslreasoner.domains.cps.mdeo.NonRedundantAllocationsConstraint" } | ||
12 | constraint UnavailableMemory java { "hu.bme.mit.inf.dslreasoner.domains.cps.mdeo.UnavailableMemoryConstraint" } | ||
13 | constraint UnavailableHdd java { "hu.bme.mit.inf.dslreasoner.domains.cps.mdeo.UnavailableHddConstraint" } | ||
14 | constraint TooLowAverageMemory java { "hu.bme.mit.inf.dslreasoner.domains.cps.mdeo.TooLowAverageMemoryConstraint" } | ||
15 | constraint TooLowAverageHdd java { "hu.bme.mit.inf.dslreasoner.domains.cps.mdeo.TooLowAverageHddConstraint" } | ||
16 | } | ||
17 | |||
18 | search { | ||
19 | // mutate using <cps_fixup.henshin> unit "createAppInstance" | ||
20 | // mutate using <cps_fixup.henshin> unit "deleteAppInstance" | ||
21 | // mutate using <cps_fixup.henshin> unit "createHostInstance" | ||
22 | // mutate using <cps_fixup.henshin> unit "deleteHostInstance" | ||
23 | // mutate using <cps_fixup.henshin> unit "allocate" | ||
24 | // mutate using <cps_fixup.henshin> unit "unallocate" | ||
25 | // mutate using <cps_fixup.henshin> unit "reallocate" | ||
26 | mutate { "ApplicationInstance" } | ||
27 | mutate { "HostInstance" } | ||
28 | } | ||
29 | |||
30 | solver { | ||
31 | optimisation provider moea algorithm NSGAII { | ||
32 | variation: mutation | ||
33 | population: 25 | ||
34 | mutation.step: 3 | ||
35 | mutation.strategy: random | ||
36 | } | ||
37 | |||
38 | termination { | ||
39 | time: 120 | ||
40 | } | ||
41 | |||
42 | batches 1 | ||
43 | } | ||
diff --git a/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/queries/CpsQueries.vql b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/queries/CpsQueries.vql new file mode 100644 index 00000000..aa78dc38 --- /dev/null +++ b/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/src/hu/bme/mit/inf/dslreasoner/domains/cps/queries/CpsQueries.vql | |||
@@ -0,0 +1,198 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.domains.cps.queries | ||
2 | |||
3 | import "http://www.eclipse.org/emf/2002/Ecore" | ||
4 | import "http://www.example.org/cps" | ||
5 | |||
6 | private pattern cpsApplications(Cps : CyberPhysicalSystem, AppInstance : ApplicationInstance) { | ||
7 | CyberPhysicalSystem.applicationTypes.instances(Cps, AppInstance); | ||
8 | } | ||
9 | |||
10 | private pattern cpsHosts(Cps : CyberPhysicalSystem, HostInstance : HostInstance) { | ||
11 | CyberPhysicalSystem.hostTypes.instances(Cps, HostInstance); | ||
12 | } | ||
13 | |||
14 | private pattern totalMemory(Host : HostInstance, Memory : EInt) { | ||
15 | HostInstance.type.defaultMemory(Host, Memory); | ||
16 | } | ||
17 | |||
18 | private pattern totalHdd(Host : HostInstance, Hdd : EInt) { | ||
19 | HostInstance.type.defaultHdd(Host, Hdd); | ||
20 | } | ||
21 | |||
22 | private pattern availableMemory(Host : HostInstance, Memory : java Integer) { | ||
23 | find totalMemory(Host, TotalMemory); | ||
24 | RequiredMemory == sum find memoryRequirement(Host, _, #_); | ||
25 | Memory == eval(TotalMemory - RequiredMemory); | ||
26 | } | ||
27 | |||
28 | private pattern memoryRequirement(Host : HostInstance, App : ApplicationInstance, Memory : EInt) { | ||
29 | find resourceRequirement(Host, App, Req); | ||
30 | ResourceRequirement.requiredMemory(Req, Memory); | ||
31 | } | ||
32 | |||
33 | private pattern availableHdd(Host : HostInstance, Hdd : java Integer) { | ||
34 | find totalHdd(Host, TotalHdd); | ||
35 | RequiredHdd == sum find hddRequirement(Host, _, #_); | ||
36 | Hdd == eval(TotalHdd - RequiredHdd); | ||
37 | } | ||
38 | |||
39 | private pattern hddRequirement(Host : HostInstance, App : ApplicationInstance, Hdd : EInt) { | ||
40 | find resourceRequirement(Host, App, Req); | ||
41 | ResourceRequirement.requiredHdd(Req, Hdd); | ||
42 | } | ||
43 | |||
44 | pattern resourceRequirement(Host : HostInstance, App : ApplicationInstance, Req : ResourceRequirement) { | ||
45 | ApplicationInstance.allocatedTo(App, Host); | ||
46 | ApplicationInstance.type.requirements(App, Req); | ||
47 | HostInstance.type(Host, HostType); | ||
48 | ResourceRequirement.hostType(Req, HostType); | ||
49 | } | ||
50 | |||
51 | @Constraint(severity = "error", key = {Host, App}, | ||
52 | message = "Application instance must be allocated to a supported host type.") | ||
53 | pattern allocationWithoutResourceRequirement(Host : HostInstance, App : ApplicationInstance) { | ||
54 | ApplicationInstance.allocatedTo(App, Host); | ||
55 | neg find resourceRequirement(Host, App, _); | ||
56 | } | ||
57 | |||
58 | @Constraint(severity = "error", key = {Host}, | ||
59 | message = "Insufficient memory available on host.") | ||
60 | pattern notEnoughAvailableMemory(Host : HostInstance) { | ||
61 | find availableMemory(Host, Memory); | ||
62 | check(Memory < 0); | ||
63 | } | ||
64 | |||
65 | @Constraint(severity = "error", key = {Host}, | ||
66 | message = "Insufficient HDD available on host.") | ||
67 | pattern notEnoughAvailableHdd(Host : HostInstance) { | ||
68 | find availableHdd(Host, Hdd); | ||
69 | check(Hdd < 0); | ||
70 | } | ||
71 | |||
72 | @Constraint(severity = "error", key = {Req, App}, | ||
73 | message = "Requirement must be satisfied by the required application type.") | ||
74 | pattern instanceDoesNotSatisfyRequirement(Req : Requirement, App : ApplicationInstance) { | ||
75 | Requirement.instances(Req, App); | ||
76 | neg find satisfyingInstance(Req, App); | ||
77 | } | ||
78 | |||
79 | private pattern satisfyingInstance(Req : Requirement, App : ApplicationInstance) { | ||
80 | Requirement.instances(Req, App); | ||
81 | Requirement.type(Req, Type); | ||
82 | ApplicationInstance.type(App, Type); | ||
83 | } | ||
84 | |||
85 | @Constraint(severity = "error", key = {Req}, | ||
86 | message = "Requirement is not satisfied by enough application instances.") | ||
87 | pattern requirementNotSatisfied(Req : Requirement) { | ||
88 | Instances == count find satisfyingInstance(Req, _); | ||
89 | Requirement.count(Req, RequiredCount); | ||
90 | check(Instances < RequiredCount); | ||
91 | } | ||
92 | |||
93 | @Constraint(severity = "error", key = {Req}, | ||
94 | message = "Redundant instances must not be allocated to the same host.") | ||
95 | pattern redundantInstancesOnSameHost(Req : Requirement) { | ||
96 | Requirement.instances(Req, App1); | ||
97 | Requirement.instances(Req, App2); | ||
98 | App1 != App2; | ||
99 | ApplicationInstance.allocatedTo(App1, Host); | ||
100 | ApplicationInstance.allocatedTo(App2, Host); | ||
101 | } | ||
102 | |||
103 | // | ||
104 | // Metrics | ||
105 | // | ||
106 | |||
107 | // Free memory | ||
108 | |||
109 | pattern averageFreeMemoryMetric(Average : java Double) { | ||
110 | Average == avg find freeMemoryPercentage(_, #_); | ||
111 | } | ||
112 | |||
113 | private pattern freeMemoryPercentage(Host : HostInstance, Free : java Double) { | ||
114 | find totalMemory(Host, Total); | ||
115 | find availableMemory(Host, Available); | ||
116 | Free == eval((Available as double) / Total); | ||
117 | } | ||
118 | |||
119 | // Free HDD | ||
120 | |||
121 | pattern averageFreeHddMetric(Average : java Double) { | ||
122 | Average == avg find freeHddPercentage(_, #_); | ||
123 | } | ||
124 | |||
125 | private pattern freeHddPercentage(Host : HostInstance, Free : java Double) { | ||
126 | find totalHdd(Host, Total); | ||
127 | find availableHdd(Host, Available); | ||
128 | Free == eval((Available as double) / Total); | ||
129 | } | ||
130 | |||
131 | // Total cost | ||
132 | |||
133 | pattern costMetric(Cost : java Integer) { | ||
134 | Cost == sum find cpsCost(_, #_); | ||
135 | } | ||
136 | |||
137 | pattern cpsCost(Cps : CyberPhysicalSystem, Cost : java Integer) { | ||
138 | AppCount == count find cpsApplications(Cps, _); | ||
139 | HostCost == sum find hostInstanceCost(Cps, _, #_); | ||
140 | Cost == eval(5 * AppCount + HostCost); | ||
141 | } | ||
142 | |||
143 | private pattern hostInstanceCost(Cps : CyberPhysicalSystem, Host : HostInstance, Cost : EInt) { | ||
144 | find cpsHosts(Cps, Host); | ||
145 | HostInstance.type.cost(Host, Cost); | ||
146 | } | ||
147 | |||
148 | // | ||
149 | // Transformation rule preconditions for rule-based DSE | ||
150 | // | ||
151 | |||
152 | pattern allocate(App : ApplicationInstance, Host : HostInstance) { | ||
153 | ApplicationInstance.type.requirements(App, Req); | ||
154 | ResourceRequirement.hostType.instances(Req, Host); | ||
155 | find unallocatedAppInstance(App); | ||
156 | find availableMemory(Host, AvailableMem); | ||
157 | find availableHdd(Host, AvailableHdd); | ||
158 | ResourceRequirement.requiredMemory(Req, RequiredMem); | ||
159 | ResourceRequirement.requiredHdd(Req, RequiredHdd); | ||
160 | check(AvailableMem >= RequiredMem); | ||
161 | check(AvailableHdd >= RequiredHdd); | ||
162 | neg ApplicationInstance.requirement.instances.allocatedTo(App, Host); | ||
163 | } | ||
164 | |||
165 | pattern unallocateAppInstance(App : ApplicationInstance) { | ||
166 | ApplicationInstance.allocatedTo(App, _); | ||
167 | } | ||
168 | |||
169 | pattern createHostInstance(HostType : HostType) { | ||
170 | find unallocatedAppInstance(App); | ||
171 | ApplicationInstance.type.requirements.hostType(App, HostType); | ||
172 | } | ||
173 | |||
174 | pattern removeHostInstance(HostInstance : HostInstance) { | ||
175 | neg HostInstance.applications(HostInstance, _); | ||
176 | } | ||
177 | |||
178 | private pattern unallocatedAppInstance(App : ApplicationInstance) { | ||
179 | neg ApplicationInstance.allocatedTo(App, _); | ||
180 | } | ||
181 | |||
182 | private pattern requiredAppInstances(Req : Requirement, Remaining : java Integer) { | ||
183 | Instances == count find satisfyingInstance(Req, _); | ||
184 | Requirement.count(Req, RequiredCount); | ||
185 | Remaining == eval(RequiredCount - Instances); | ||
186 | } | ||
187 | |||
188 | private pattern noHostToAllocateTo(App : ApplicationInstance) { | ||
189 | find unallocatedAppInstance(App); | ||
190 | neg find allocate(App, _); | ||
191 | } | ||
192 | |||
193 | pattern guidanceObjective(Value : java Integer) { | ||
194 | UnallocatedInstances == count find unallocatedAppInstance(_); | ||
195 | RequiredInstances == sum find requiredAppInstances(_, #_); | ||
196 | NoHostToAllocate == count find noHostToAllocateTo(_); | ||
197 | Value == eval(2 * UnallocatedInstances + 4 * RequiredInstances + NoHostToAllocate); | ||
198 | } | ||