Skip to content

Commit 587abfc

Browse files
committed
init
0 parents  commit 587abfc

18 files changed

+225
-0
lines changed

.classpath

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="ch.epfl.lamp.sdt.launching.SCALA_CONTAINER"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>

.hgignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
syntax: glob
2+
rngseeds.txt
3+
bin\pgep/*.class
Binary file not shown.
Binary file not shown.
Binary file not shown.

.project

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>pgep</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>ch.epfl.lamp.sdt.core.scalabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>ch.epfl.lamp.sdt.core.scalanature</nature>
16+
<nature>org.eclipse.jdt.core.javanature</nature>
17+
</natures>
18+
</projectDescription>

.settings/org.eclipse.jdt.core.prefs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#Sat Apr 11 22:55:18 CEST 2009
2+
eclipse.preferences.version=1
3+
org.eclipse.jdt.core.builder.cleanOutputFolder=ignore
4+
org.eclipse.jdt.core.builder.resourceCopyExclusionFilter=*.scala

src/pgep/Alphabet.scala

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package pgep
2+
3+
abstract class Alphabet[T <: Term](ts: T*) extends Seq[T] {
4+
protected val terms = ts.toArray
5+
6+
protected def _maxParams = ts map (_.nparams) reduceLeft Math.max
7+
def maxParams: Int
8+
override def length = terms.length
9+
override def elements = terms.elements
10+
override def apply(i: Int): T = terms.apply(i)
11+
}
12+
13+
class AlphabetRO[T <: Term](ts: T*) extends Alphabet[T](ts: _*) {
14+
val maxParams = _maxParams
15+
}
16+
17+
class AlphabetRW[T <: Term](ts: T*) extends Alphabet[T](ts: _*) {
18+
var maxParams = _maxParams
19+
def update(i: Int, v: T) {terms(i) = v; maxParams = _maxParams}
20+
}

src/pgep/Gene.scala

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package pgep
2+
3+
import scala.collection.mutable.{HashMap,HashSet}
4+
5+
class Gene(val parameters: GeneParameters) {
6+
val _k_expression = new HashMap[Class[_], Array[Term]]
7+
private val _constants = new HashMap[Class[_], Array[Const]]
8+
9+
private val random = RNGProvider()
10+
11+
{
12+
val types = new HashSet[Class[_]]
13+
types ++= parameters.functions map (_.resultType)
14+
types ++= parameters.variables map (_.typee)
15+
types ++= parameters.constants.keys
16+
17+
types foreach { t =>
18+
_k_expression(t) = new Array(parameters.headLen + parameters.tailLen)
19+
_constants(t) = new Array(parameters.tailLen)
20+
}
21+
}
22+
23+
def randomize(selector_fvc: Selector[SymbolSetKind], selector_vc: Selector[SymbolSetKind],
24+
headp: () => Boolean, tailp: () => Boolean, constantsp: () => Boolean) {
25+
for (tpe <- _k_expression.keys) {
26+
val k_expr = _k_expression(tpy)
27+
val functions = parameters.functions filter (_.resultType == tpe) toArray
28+
val variables = parameters.variables filter (_.typee == tpe) toArray
29+
val constants = parameters.constants(tpy)
30+
}
31+
}
32+
33+
def copySymbols
34+
}

src/pgep/GeneParameters.scala

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package pgep
2+
3+
class GeneParameters(val headLen: Int, val tailLen: Int, val resultType: Class[_],
4+
val functions: Alphabet[Func], val variables: Alphabet[Var], val constants: Map[Class[_], Alphabet[Const]]) {
5+
{
6+
assume(constants forall {case (t, a) => a forall (_.typee == t)}, "inconsistent constants")
7+
8+
val types = new scala.collection.mutable.HashSet[Class[_]]
9+
types ++= functions flatMap (f => f.resultType :: f.parameterTypes)
10+
types ++= variables map (_.typee)
11+
types ++= constants.keys
12+
13+
for (t <- types) {
14+
if (!functions.exists(_.resultType == t) ||
15+
!variables.exists(_.typee == t) ||
16+
!constants.exists(_ == t))
17+
throw new IllegalArgumentException("Not all types are covered by Alphabets!");
18+
}
19+
}
20+
21+
def geneLen = headLen + 2 * tailLen
22+
}
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package pgep
2+
3+
abstract class Selector[T] {
4+
def apply(): T
5+
}
6+
7+
abstract class ProbabilityBasedSelector[T] extends Selector[T] {
8+
protected val random = RNGProvider()
9+
10+
protected def constructorPreconditions(probabilities: Seq[Double], values: Seq[T], scale: Double) {
11+
assert(probabilities != null)
12+
assert(values != null)
13+
assert(probabilities.length > 0)
14+
assert(probabilities.length == values.length)
15+
assert(scale > 0)
16+
assert(probabilities forall (_ >= 0))
17+
assert(probabilities.reduceLeft(_ + _) == scale)
18+
}
19+
}
20+
21+
class ExactSelector[T](probabilities: Seq[Double], val values: Seq[T], scale: Double) extends ProbabilityBasedSelector[T] {
22+
def this(probabilities: Seq[Double], values: Seq[T]) = this(probabilities, values, probabilities reduceLeft (_ + _))
23+
24+
protected val probabilityDistribution = {
25+
constructorPreconditions(probabilities, values, scale)
26+
var sum: Double = 0
27+
val pdist = probabilities map (p => {sum += p / scale; sum})
28+
constructorPostconditions(probabilities, pdist)
29+
pdist
30+
}
31+
32+
protected def constructorPostconditions(probabilities: Seq[Double], pdist: Seq[Double]) {
33+
assert(probabilities.length == pdist.length)
34+
assert(pdist forall (d => d <= 1.00000000001 && d >= -0.00000000001))
35+
val pdistl = pdist.toList
36+
assert((pdistl zip pdistl.tail) forall {case (d1, d2) => d1 <= d2})
37+
assert(Math.abs(pdist.last - 1) < 0.00000000001)
38+
}
39+
40+
override def apply() = {
41+
var rnd = random.nextDouble
42+
val idx = probabilityDistribution map (rnd < _) indexOf true
43+
assert(idx >= 0)
44+
values(idx)
45+
}
46+
}
47+
48+
class SampledSelector[T](probabilities: Seq[Double], val values: Seq[T], scale: Double, nrsamples: Int) extends ProbabilityBasedSelector[T] {
49+
def this(probabilities: Seq[Double], values: Seq[T], scale: Double) = this(probabilities, values, scale, 1000)
50+
def this(probabilities: Seq[Double], values: Seq[T]) = this(probabilities, values, probabilities reduceLeft (_ + _))
51+
52+
protected val samples = {
53+
constructorPreconditions(probabilities, values, scale)
54+
val smpls = ((values.toList zip probabilities.toList) flatMap {case (value, prob) => (List.make(Math.floor(prob * nrsamples.toDouble).toInt, value))}).toArray
55+
constructorPostconditions(values, nrsamples, smpls)
56+
smpls
57+
}
58+
59+
protected def constructorPostconditions(values: Seq[T], nrsamples: Int, samples: Array[T]) {
60+
assert(samples.length == nrsamples)
61+
assert(samples forall (values.contains(_)))
62+
assert(values forall (samples.contains(_)))
63+
}
64+
65+
protected def constructorPreconditions(probabilities: Seq[Double], values: Seq[T], scale: Double, nrsamples: Int) {
66+
constructorPreconditions(probabilities, values, scale)
67+
assert(nrsamples > values.length)
68+
}
69+
70+
override def apply() = {
71+
samples(random.nextInt(samples.length))
72+
}
73+
}

src/pgep/RNGProvider.scala

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package pgep
2+
3+
import java.util.Date
4+
import java.io.FileWriter
5+
import scala.util.Random
6+
import scala.collection.mutable.{HashMap,SynchronizedMap}
7+
8+
object RNGProvider {
9+
var seed: Int = (new Date().getTime & Integer.MAX_VALUE).asInstanceOf[Int]
10+
private val logFile = "rngseeds.txt"
11+
private val rngs = new HashMap[Thread, Random] with SynchronizedMap[Thread,Random]
12+
13+
private def createRng = {
14+
val f = new FileWriter(logFile, true)
15+
f.write("%s ThreadId:%s ThreadName:\"%s\" seed:%s\n" format (new Date(), currentThread.getId, currentThread.getName, seed))
16+
f.close
17+
18+
val rng = new Random(seed)
19+
rngs(currentThread) = rng
20+
rng
21+
}
22+
23+
def apply() = rngs.getOrElseUpdate(currentThread, createRng)
24+
}

src/pgep/SymbolSetKind.scala

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package pgep
2+
3+
sealed abstract class SymbolSetKind
4+
case object Func extends SymbolSetKind
5+
case object Var extends SymbolSetKind
6+
case object Const extends SymbolSetKind

src/pgep/Term.scala

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package pgep
2+
3+
sealed abstract class Term {
4+
val nparams = 0
5+
}
6+
7+
case class Const(name: String, typee: Class[_], value: Any) extends Term
8+
case class Var(name: String, typee: Class[_]) extends Term
9+
case class Func(name: String, parameterTypes: List[Class[_]], resultType: Class[_], fn: List[Any] => Any, strFn: List[String] => String) extends Term {
10+
def apply = fn
11+
def applyStr = strFn
12+
13+
override val nparams = parameterTypes.length
14+
}

0 commit comments

Comments
 (0)