From eb0387c71397c59193e5bc7474e6543adb2fff16 Mon Sep 17 00:00:00 2001
From: Marcel Becker <stu117960@mail.uni-kiel.de>
Date: Tue, 1 Feb 2022 16:35:09 +0100
Subject: [PATCH] Search metric differentiation outsourced to SearchStrategy

---
 .../execution/BenchmarkExecutorImpl.kt        |  3 +++
 .../execution/TheodoliteExecutor.kt           | 16 +-------------
 .../restriction/LowerBoundRestriction.kt      |  4 ++--
 .../restriction/RestrictionStrategy.kt        |  2 +-
 .../strategies/searchstrategy/LinearSearch.kt |  2 --
 .../searchstrategy/RestrictionSearch.kt       |  7 ++++---
 .../searchstrategy/SearchStrategy.kt          | 21 +++++++++++++++++++
 7 files changed, 32 insertions(+), 23 deletions(-)

diff --git a/theodolite/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt b/theodolite/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt
index 733e3ea61..f463ce452 100644
--- a/theodolite/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt
+++ b/theodolite/src/main/kotlin/theodolite/execution/BenchmarkExecutorImpl.kt
@@ -71,6 +71,9 @@ class BenchmarkExecutorImpl(
             }
 
             result = (false !in experimentResults)
+            // differentiate metric here on first/second ele pairs, also wenn demand so und wenn capacity dann mit (resource,load)
+            // so könnten wir die Methoden in Results so lassen und müssten keine Dopplung einbauen
+            // wird alles sehr undurchsichtig damit wenn man die vertauscht, evtl mit metric zu den Results klarer machen
             this.results.setResult(Pair(load, resource), result)
         }
 
diff --git a/theodolite/src/main/kotlin/theodolite/execution/TheodoliteExecutor.kt b/theodolite/src/main/kotlin/theodolite/execution/TheodoliteExecutor.kt
index 8664ea034..7ab161993 100644
--- a/theodolite/src/main/kotlin/theodolite/execution/TheodoliteExecutor.kt
+++ b/theodolite/src/main/kotlin/theodolite/execution/TheodoliteExecutor.kt
@@ -120,21 +120,7 @@ class TheodoliteExecutor(
 
         //execute benchmarks for each load for the demand metric, or for each resource amount for capacity metric
         try {
-            if (config.metric == "demand") {
-                //demand metric
-                for (load in config.loads) {
-                    if (executor.run.get()) {
-                        config.searchStrategy.findSuitableResource(load, config.resources)
-                    }
-                }
-            } else {
-                //capacity metric
-                for (resource in config.resources) {
-                    if (executor.run.get()) {
-                        config.searchStrategy.findSuitableLoad(resource, config.loads)
-                    }
-                }
-            }
+            config.searchStrategy.findSuitableCapacity(config.loads, config.resources, config.metric)
 
         } finally {
             ioHandler.writeToJSONFile(
diff --git a/theodolite/src/main/kotlin/theodolite/strategies/restriction/LowerBoundRestriction.kt b/theodolite/src/main/kotlin/theodolite/strategies/restriction/LowerBoundRestriction.kt
index 6849d74fd..f4ff6c5dd 100644
--- a/theodolite/src/main/kotlin/theodolite/strategies/restriction/LowerBoundRestriction.kt
+++ b/theodolite/src/main/kotlin/theodolite/strategies/restriction/LowerBoundRestriction.kt
@@ -12,8 +12,8 @@ class LowerBoundRestriction(results: Results) : RestrictionStrategy(results) {
 
     override fun apply(load: Int, resources: List<Int>): List<Int> {
         val maxLoad: Int? = this.results.getMaxBenchmarkedLoad(load)
-        var lowerBound: Int? = this.results.getMinRequiredInstances(maxLoad)
-        if (lowerBound == null) {
+        var lowerBound: Int = this.results.getMinRequiredInstances(maxLoad)
+        if (lowerBound == Int.MIN_VALUE || lowerBound == Int.MAX_VALUE) {
             lowerBound = resources[0]
         }
         return resources.filter { x -> x >= lowerBound }
diff --git a/theodolite/src/main/kotlin/theodolite/strategies/restriction/RestrictionStrategy.kt b/theodolite/src/main/kotlin/theodolite/strategies/restriction/RestrictionStrategy.kt
index 963da39e9..b402b917f 100644
--- a/theodolite/src/main/kotlin/theodolite/strategies/restriction/RestrictionStrategy.kt
+++ b/theodolite/src/main/kotlin/theodolite/strategies/restriction/RestrictionStrategy.kt
@@ -14,7 +14,7 @@ abstract class RestrictionStrategy(val results: Results) {
     /**
      * Apply the restriction of the given resource list for the given load based on the results object.
      *
-     * @param load LoadDimension for which a subset of resources are required.
+     * @param load LoadDimension for which a subset of resources is required.
      * @param resources List of Resource s to be restricted.
      * @return Returns a list containing only elements that have not been filtered out by the
      * restriction (possibly empty).
diff --git a/theodolite/src/main/kotlin/theodolite/strategies/searchstrategy/LinearSearch.kt b/theodolite/src/main/kotlin/theodolite/strategies/searchstrategy/LinearSearch.kt
index c4497934b..a02505844 100644
--- a/theodolite/src/main/kotlin/theodolite/strategies/searchstrategy/LinearSearch.kt
+++ b/theodolite/src/main/kotlin/theodolite/strategies/searchstrategy/LinearSearch.kt
@@ -20,8 +20,6 @@ class LinearSearch(benchmarkExecutor: BenchmarkExecutor) : SearchStrategy(benchm
         return null
     }
 
-    // Stops after having the first load which is not possible anymore with the current resource, maybe some later load still possible tho
-    // kinda like GuessSearchStrat case -> differentiate or is it fine like that?
     override fun findSuitableLoad(resource: Int, loads: List<Int>): Int? {
         var maxSuitableLoad: Int? = null
         for (load in loads) {
diff --git a/theodolite/src/main/kotlin/theodolite/strategies/searchstrategy/RestrictionSearch.kt b/theodolite/src/main/kotlin/theodolite/strategies/searchstrategy/RestrictionSearch.kt
index 0367686c2..10cc4a7a2 100644
--- a/theodolite/src/main/kotlin/theodolite/strategies/searchstrategy/RestrictionSearch.kt
+++ b/theodolite/src/main/kotlin/theodolite/strategies/searchstrategy/RestrictionSearch.kt
@@ -26,9 +26,10 @@ class RestrictionSearch(
         return this.searchStrategy.findSuitableResource(load, restrictedResources)
     }
 
-    //TODO: not sure if it makes sense but actually doing the same as for finding suitable resource with the restrictions
     override fun findSuitableLoad(resource: Int, loads: List<Int>): Int? {
-        //erste Zeile komisch, wird auch bei resource so gemacht aber warum? das ist doch ne liste warum also toList?
-        TODO("Not yet implemented")
+        var restrictedLoads = loads
+        for (strategy in this.restrictionStrategies) {
+            restrictedLoads = restrictedLoads.intersect(strategy.apply(resource, loads).toSet()).toList()
+        }
     }
 }
\ No newline at end of file
diff --git a/theodolite/src/main/kotlin/theodolite/strategies/searchstrategy/SearchStrategy.kt b/theodolite/src/main/kotlin/theodolite/strategies/searchstrategy/SearchStrategy.kt
index 31f9deed5..ff8827ce6 100644
--- a/theodolite/src/main/kotlin/theodolite/strategies/searchstrategy/SearchStrategy.kt
+++ b/theodolite/src/main/kotlin/theodolite/strategies/searchstrategy/SearchStrategy.kt
@@ -14,6 +14,27 @@ import theodolite.util.Results
 @RegisterForReflection
 abstract class SearchStrategy(val benchmarkExecutor: BenchmarkExecutor, val guessStrategy: GuessStrategy? = null,
                               val results: Results? = null) {
+
+
+    fun findSuitableCapacity(loads: List<Int>, resources: List<Int>, metric: String) {
+
+        if (metric == "demand") {
+            //demand metric
+            for (load in loads) {
+                if (benchmarkExecutor.run.get()) {
+                    this.findSuitableResource(load, resources)
+                }
+            }
+        } else {
+            //capacity metric
+            for (resource in resources) {
+                if (benchmarkExecutor.run.get()) {
+                    this.findSuitableLoad(resource, loads)
+                }
+            }
+        }
+    }
+
     /**
      * Find smallest suitable resource from the specified resource list for the given load.
      *
-- 
GitLab