Skip to content

Commit

Permalink
added limit by depth to random sampling in graph structure
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneslenfers committed Jan 11, 2024
1 parent 2a2166a commit 7a77010
Showing 1 changed file with 53 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,43 @@ import scala.collection.mutable.ListBuffer
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
var sampleCounter: Int = 0

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

// todo: limit rewrite depth
// (number of rewrites applied in total (collapse rules+inverse? -> how to recognise?)
while (sampleCounter < samples) {

for (_ <- Range(0, samples)) {
//get neighbourhood
val Ns: Seq[Solution[P]] = panel.N(solution)
// reset solution
solution = initialSolution

solution = Ns.size match {
case 0 =>
// neighborhood emtpy -> stop search
println("empty neighborhood - this should not happen with bidirectional rules")
return ExplorationResult(
solution,
solutionValue,
None
)
for (_ <- Range(0, depth)) {

// chose valid solution randomly from neighborhood
case _ =>
//get neighbourhood
val Ns: Seq[Solution[P]] = panel.N(solution)

// get permutation for
val permutationIterator = generateRandomPermutation(Ns.size, random).iterator
// choose solution from neighborhood
solution = Ns.size match {
case 0 =>
// neighborhood emtpy -> stop search
println("empty neighborhood - this should not happen with bidirectional rules")
return ExplorationResult(
solution,
solutionValue,
None
)

var found = false
while (!found) {
// chose valid solution randomly from neighborhood
case _ =>

// get next element
solution = Ns.apply(permutationIterator.next())

found = panel.f(solution) match {
// invalid candidate
case None =>
false

// valid candidate
case Some(value) =>
solutionValue = Some(value)
true
}
}

// return chosen solution
solution
solution = Ns.apply(random.nextInt(Ns.size))
solutionValue = panel.f(solution)
sampleCounter += 1

solution
}
}
}

Expand All @@ -70,6 +54,33 @@ class RandomGraph[P] extends Heuristic[P] {
)
}

// Code to only allow valid programs
//
// // get permutation for
// val permutationIterator = generateRandomPermutation(Ns.size, random).iterator
//
// var found = false
// while (!found) {
//
// // get next element
// solution = Ns.apply(permutationIterator.next())
// panel.f(solution)
//
// found = panel.f(solution) match {
// // invalid candidate
// case None =>
// false
//
// // valid candidate
// case Some(value) =>
// solutionValue = Some(value)
// true
// }
// }
//
// // return chosen solution
// solution

def generateRandomPermutation(n: Int, random: scala.util.Random): List[Int] = {
val buffer = ListBuffer.range(0, n)

Expand Down

0 comments on commit 7a77010

Please sign in to comment.