Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object Anneal {
- val rng = new java.util.Random()
- // здесь p - это вероятность взять новую особь, если она хуже
- def anneal[A, E: Ordering, T: Ordering](f: A => E)(startA: A, startT: T)(nextA: (A, T) => A, nextT: (A, T) => T)(minT: T)(p: (T, E, E) => Double): (A, E) = {
- import Ordering.Implicits._
- def iteration(currA: A, currE: E, currT: T): (A, E) = {
- if (currT < minT) {
- (currA, currE)
- } else {
- val newA = nextA(currA, currT)
- val newT = nextT(currA, currT)
- val newE = f(newA)
- if (newE <= currE || p(newT, currE, newE) > rng.nextDouble()) {
- iteration(newA, newE, newT)
- } else {
- iteration(currA, currE, newT)
- }
- }
- }
- iteration(startA, f(startA), startT)
- }
- def main(args: Array[String]) {
- val oneMin: Long => Int = 64 - java.lang.Long.bitCount(_)
- val nextA: (Long, Int) => Long = (a, t) => a ^ (1L << rng.nextInt(64))
- val nextT: (Long, Int) => Int = (a, t) => t - 1
- println(anneal(oneMin)(0, 250)(nextA, nextT)(0)((_, _, _) => 0.0))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement