From 62eb5154b41ba776f7ca5a8fbd4a124bac4b9dde Mon Sep 17 00:00:00 2001 From: Johannes Lenfers Date: Thu, 24 Oct 2024 17:20:22 +0200 Subject: [PATCH] adjust random sampling --- .../heuristics/RandomGraph.scala | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main/scala/elevate/heuristic_search/heuristics/RandomGraph.scala b/src/main/scala/elevate/heuristic_search/heuristics/RandomGraph.scala index 45c0b879..3a965a0f 100644 --- a/src/main/scala/elevate/heuristic_search/heuristics/RandomGraph.scala +++ b/src/main/scala/elevate/heuristic_search/heuristics/RandomGraph.scala @@ -27,13 +27,12 @@ class RandomGraph[P] extends Heuristic[P] { val Ns: Seq[Solution[P]] = panel.N(solution) // choose solution from neighborhood - solution = Ns.size match { + Ns.size match { case 0 => // empty neighborhood // abort this try and reset depthCounter = depth - solution // choose valid solution randomly from neighborhood case _ => @@ -54,19 +53,19 @@ class RandomGraph[P] extends Heuristic[P] { } else { // get next element sampleCounter += 1 - solution = candidates.apply(random.nextInt(candidates.size)) - solutionValue = panel.f(solution) - - if (solutionValue.equals(None)) { - // add attempt - attempts += solution - - } else { - foundValid = true + val candidate = candidates.apply(random.nextInt(candidates.size)) + val candidateValue = panel.f(candidate) + + candidateValue match { + case None => + attempts += candidate + case Some(value) => + foundValid = true + solution = candidate + solutionValue = candidateValue } } } - solution } } }