Skip to content

Commit

Permalink
added graph based searches
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneslenfers committed Sep 29, 2022
1 parent 0da535c commit 361c68d
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ case class Metaheuristic[P](name: String,

val real_output = output
s"mkdir -p ${real_output}" !!

val result = heuristic.start(panel, solution, depth, samples)

// todo move this to output or is this part of exploration?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package elevate.heuristic_search.heuristics

import elevate.heuristic_search._
import elevate.heuristic_search.util.{Solution, hashProgram}

class LocalSearchGraph[P] extends Heuristic[P] {

def start(panel: HeuristicPanel[P], initialSolution: Solution[P], depth: Int, samples: Int): ExplorationResult[P] = {
var solution: Solution[P] = initialSolution
var solutionValue: Option[Double] = panel.f(solution)
var old_solution = solution
// var min: Option[Double] = solutionValue

val random = scala.util.Random

// later:
// don't allow duplicates
// save position and action

do {
//get neighbourhood
val Ns: Seq[Solution[P]] = panel.N(solution)

// evaluate neighborhood and get minimum
old_solution = solution
solution = Ns.reduceLeft((a, b) => min(panel, a, b))
solutionValue = panel.f(solution)

// end loop if no new element was found
} while (!old_solution.equals(solution))

ExplorationResult(
solution,
solutionValue,
None
)
}

private def min(panel: HeuristicPanel[P], a: Solution[P], b: Solution[P]): Solution[P] = {
panel.f(a) match {
case Some(f_a) => panel.f(b) match {
case Some(f_b) if f_a < f_b => a
case Some(f_b) => b
case None => a
}
case None => panel.f(b) match {
case Some(f_b) => b
case None => a
}
}
}

}



Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package elevate.heuristic_search.heuristics

import elevate.heuristic_search._
import elevate.heuristic_search.util.Solution

class RandomGraph[P] extends Heuristic[P] {

def start(panel: HeuristicPanel[P], initialSolution: Solution[P], depth: Int, samples: Int): ExplorationResult[P] = {
var solution: Solution[P] = initialSolution
var solutionValue: Option[Double] = panel.f(solution)
// var min: Option[Double] = solutionValue

val random = scala.util.Random

// later:
// don't allow duplicates
// save position and action

for (_ <- Range(0, samples)) {
//get neighbourhood
val Ns: Seq[Solution[P]] = panel.N(solution)
solution = Ns.size match {
case 0 => println("empty neighborhood - this should not happend")
return ExplorationResult(
solution,
solutionValue,
None
)
case _ => Ns(random.nextInt(Ns.size))
}
solutionValue = panel.f(solution)

// check if new min was found
// min = solutionValue match {
// case Some(value) => value < min match {
// case true => solutionValue
// case false => min
// }
// case None => min
// }
}

ExplorationResult(
solution,
solutionValue,
None
)
}
}



0 comments on commit 361c68d

Please sign in to comment.