-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0da535c
commit 361c68d
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/scala/elevate/heuristic_search/heuristics/LocalSearchGraph.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
|
52 changes: 52 additions & 0 deletions
52
src/main/scala/elevate/heuristic_search/heuristics/RandomGraph.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} | ||
|
||
|
||
|