From cac23f66b498a19c14fb6609f99b1cc34c3c36d2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=B6ren=20Henning?= <soeren.henning@jku.at>
Date: Fri, 14 Apr 2023 11:13:34 +0200
Subject: [PATCH] Fix issue in restriction search, fix #402

---
 .../LowerBoundRestriction.kt                  |  8 +++---
 .../InitialGuessSearchStrategyTest.kt         | 25 +++++++++++++++++++
 .../searchstrategy/RestrictionSearchTest.kt   |  1 -
 3 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/theodolite/src/main/kotlin/rocks/theodolite/core/strategies/restrictionstrategy/LowerBoundRestriction.kt b/theodolite/src/main/kotlin/rocks/theodolite/core/strategies/restrictionstrategy/LowerBoundRestriction.kt
index 2e5a51018..b1c436c32 100644
--- a/theodolite/src/main/kotlin/rocks/theodolite/core/strategies/restrictionstrategy/LowerBoundRestriction.kt
+++ b/theodolite/src/main/kotlin/rocks/theodolite/core/strategies/restrictionstrategy/LowerBoundRestriction.kt
@@ -13,11 +13,9 @@ import rocks.theodolite.core.Results
 class LowerBoundRestriction(results: Results) : RestrictionStrategy(results) {
 
     override fun apply(xValue: Int, yValues: List<Int>): List<Int> {
-        val maxXValue: Int? = this.results.getMaxBenchmarkedXDimensionValue(xValue)
-        var lowerBound: Int? = this.results.getOptYDimensionValue(maxXValue)
-        if (lowerBound == null) {
-            lowerBound = yValues[0]
-        }
+        val maxXValue = this.results.getMaxBenchmarkedXDimensionValue(xValue)
+        // Get previous largest y value or restrict to empty list
+        val lowerBound: Int = this.results.getOptYDimensionValue(maxXValue) ?: return listOf()
         return yValues.filter { x -> x >= lowerBound }
     }
 
diff --git a/theodolite/src/test/kotlin/rocks/theodolite/core/strategies/searchstrategy/InitialGuessSearchStrategyTest.kt b/theodolite/src/test/kotlin/rocks/theodolite/core/strategies/searchstrategy/InitialGuessSearchStrategyTest.kt
index 40cf803d6..155ceb847 100644
--- a/theodolite/src/test/kotlin/rocks/theodolite/core/strategies/searchstrategy/InitialGuessSearchStrategyTest.kt
+++ b/theodolite/src/test/kotlin/rocks/theodolite/core/strategies/searchstrategy/InitialGuessSearchStrategyTest.kt
@@ -9,12 +9,37 @@ import rocks.theodolite.kubernetes.TestExperimentRunner
 import rocks.theodolite.core.strategies.guessstrategy.PrevInstanceOptGuess
 import rocks.theodolite.core.Results
 import rocks.theodolite.core.createResultsFromArray
+import rocks.theodolite.core.strategies.restrictionstrategy.LowerBoundRestriction
 
 private val logger = KotlinLogging.logger {}
 
 @QuarkusTest
 class InitialGuessSearchStrategyTest {
 
+    @Test
+    fun initialGuessSearchNoMatch() {
+        val mockResults = createResultsFromArray(arrayOf(
+            arrayOf(true, true),
+            arrayOf(false, false),
+            arrayOf(true, true),
+        ), Metric.DEMAND)
+        val mockLoads: List<Int> = (1..3).toList()
+        val mockResources: List<Int> = (1..2).toList()
+        val results = Results(Metric.DEMAND)
+        val guessStrategy = PrevInstanceOptGuess()
+        val benchmarkExecutor = TestExperimentRunner(results, mockResults)
+        val strategy = InitialGuessSearchStrategy(benchmarkExecutor,guessStrategy, results)
+
+        val actual: MutableList<Int?> = mutableListOf()
+        val expected: List<Int?> = listOf(1, null, 1)
+
+        for (load in mockLoads) {
+            actual.add(strategy.findSuitableResource(load, mockResources))
+        }
+
+        assertEquals(expected, actual)
+    }
+
     @Test
     fun testInitialGuessSearch() {
         val mockResults = createResultsFromArray(arrayOf(
diff --git a/theodolite/src/test/kotlin/rocks/theodolite/core/strategies/searchstrategy/RestrictionSearchTest.kt b/theodolite/src/test/kotlin/rocks/theodolite/core/strategies/searchstrategy/RestrictionSearchTest.kt
index dbd5454fc..b1f97667f 100644
--- a/theodolite/src/test/kotlin/rocks/theodolite/core/strategies/searchstrategy/RestrictionSearchTest.kt
+++ b/theodolite/src/test/kotlin/rocks/theodolite/core/strategies/searchstrategy/RestrictionSearchTest.kt
@@ -14,7 +14,6 @@ import rocks.theodolite.core.createResultsFromArray
 class RestrictionSearchTest {
 
     @Test
-    @Disabled("Currently failing, has to be fixed")
     fun restrictionSearchNoMatch() {
         val mockResults = createResultsFromArray(arrayOf(
             arrayOf(true, true),
-- 
GitLab