Last active
August 29, 2015 13:57
-
-
Save kamiyaowl/9362820 to your computer and use it in GitHub Desktop.
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
target,381 | |
generate,784,362,378,365,365,365,365,381,381,377,381,381,377,377,377,381,381,381,381,381, |
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
#range = 100000 | |
#初期個体数 100 | |
#世代数 30 | |
target,84290 | |
generate,54102,84833,84846,84785,84334,82737,84337,84337,84270,84337,84782,82289,84785,84786,82285,82285,82285,84787,82284,84796,82275,82275,82275,84835,84284,84835,84835,86073,84841,84531, |
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
import scala.language.postfixOps | |
import scala.util.Random | |
import scala.reflect._ | |
object SimpleGenetic { | |
abstract class Cell[T] { | |
var self:T | |
val seed:Random | |
def show() | |
def make() | |
def eval(target:T):Int | |
def +(target:T) : (Cell[T],Cell[T]) | |
def copy() : Cell[T] | |
} | |
class IntCell extends Cell[Int] { | |
var self:Int = _ | |
var range:Int = _ | |
val seed = new Random | |
override def show() = { | |
println(String.format("0b%" + (range.toBinaryString.length + 1) +"s" ,self.toBinaryString).replace(' ','0')) | |
} | |
override def make() = { | |
self = seed.nextInt(range) | |
} | |
override def eval(target:Int) = { | |
range - math.abs(self - target) | |
} | |
override def +(target:Int) = { | |
val l = if(target > self) target.toBinaryString.length else self.toBinaryString.length | |
val pivot = seed.nextInt(l) | |
val a = copy | |
val b = copy | |
a.self = (self & (~0 << pivot)) | (target & ~(~0 << pivot)) | |
b.self = (self & ~(~0 << pivot)) | (target & (~0 << pivot)) | |
(a,b) | |
} | |
override def copy() = { | |
val n = new IntCell | |
n.self = self | |
n.range = range | |
n | |
} | |
} | |
class IntCellTool(target:Int,range:Int) | |
{ | |
def makeFirst() : Stream[IntCell] = { | |
val n = new IntCell | |
n.range = range | |
n.make | |
Stream.cons(n, makeFirst) | |
} | |
def crossing[T](evaled:Stream[IntCell]) : Stream[IntCell] = { | |
if(evaled.length < 2) Stream.empty | |
else { | |
val crossed = evaled(0) + evaled(1).self | |
Stream.cons(crossed._1, Stream.cons(crossed._2, crossing(evaled.tail.tail))) | |
} | |
} | |
def nextGen(current:Stream[IntCell]) : Stream[IntCell] = { | |
val ft = current zip (current map(_.eval(target))) sortWith((a,b) => a._2 > b._2) map(_._1) | |
crossing(ft) | |
} | |
def autoGen(current:Stream[IntCell]) : Stream[Stream[IntCell]] = { | |
Stream.cons(current,autoGen(nextGen(current))) | |
} | |
} | |
def main(ars:Array[String]) : Unit = { | |
val range = 1000 | |
val target = (new Random).nextInt(range) | |
val t = new IntCellTool(target,range) | |
val first = t.makeFirst take 50 | |
val processed = t.autoGen(first) take 20 | |
println("target," + target) | |
print("generate,") | |
for(p <- processed) print(p(0).self + ",") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment