From VisualWorks® NonCommercial, Release 5i.2 of July 14, 2000 on May 7, 2003 at 10:00:34 pm
GPCodeHolder
Smalltalk
Core.Object
false
none
SDML - Bruce
RNG
GPCodeHolder
false
false
As yet unclassified
GPCodeHolder class binarySearch
findPositionIn: sortedList of: number
| len |
len := sortedList listLength.
number <= (sortedList listElementAt: 1) ifTrue: [^1].
number >= (sortedList listElementAt: len) ifTrue: [^len].
^self
findPositionIn: sortedList
of: number
between: 1
and: len
findPositionIn: sortedList of: number between: lowerPos and: upperPos
| middlePos |
upperPos - lowerPos <= 1
ifTrue: [^upperPos]
ifFalse:
[middlePos := (lowerPos + upperPos / 2) truncated.
number <= (sortedList listElementAt: middlePos)
ifTrue: [^self
findPositionIn: sortedList
of: number
between: lowerPos
and: middlePos]
ifFalse: [^self
findPositionIn: sortedList
of: number
between: middlePos
and: upperPos]]
version
GPCodeHolder class maxDepth
avDepth: tree
| len branches num sum |
len := tree listLength.
(len = 0) ifTrue: [self error: 'Should not be a tree length zero!'].
(len = 1) ifTrue: [^1].
num := len - 1.
branches := tree sublistFrom: 2 to: len.
sum := 0.
branches listElementsReverseDo: [:branch | sum := sum + (self avDepth: branch)].
^(sum/num) + 1.
maxDepth: tree
| len branches max |
len := tree listLength.
(len = 0) ifTrue: [self error: 'Should not be a tree length zero!'].
(len = 1) ifTrue: [^1].
branches := tree sublistFrom: 2 to: len.
max := 0.
branches listElementsReverseDo: [:branch | max := max max: (self maxDepth: branch)].
^1 + max
numNodes: tree
| len sum |
len := tree listLength.
len = 0 ifTrue: [self error: 'Should not be a tree length zero!'].
len = 1 ifTrue: [^1].
sum := 1.
(tree sublistFrom: 2 to: len)
listElementsReverseDo: [:branch | sum := sum + (self numNodes: branch)].
^sum
sampleDepth: tree
^self sampleDepth: tree rng: Random new
sampleDepth: tree rng: randomNumGen
| len rn |
len := tree listLength.
len = 0 ifTrue: [self error: 'Should not be a tree length zero!'].
len = 1 ifTrue: [^1].
len = 2 ifTrue: [^1 + (self sampleDepth: (tree listElementAt: 2) rng: randomNumGen)].
rn := (((randomNumGen next) * (len - 1)) truncated) + 2.
^1+ (self sampleDepth: (tree listElementAt: rn) rng: randomNumGen)
GPCodeHolder class crossover
crossoverOf: tree1 at: posList1 and: tree2 at: posList2
| subTree1 subTree2 child1 child2 |
subTree1 := self subTree: tree1 at: posList1.
subTree2 := self subTree: tree2 at: posList2.
child1 := self
subs: subTree1
in: tree2
at: posList2.
child2 := self
subs: subTree2
in: tree1
at: posList1.
^(ListClause withReverseElements: (List with: child2 with: child1))
GPCodeHolder class apply
addLast: item to: list
^((list reversedList) listWithHead: item) reversedList
apply: opTree to: tree1 at: pos1 and: tree2 at: pos2
| ans |
ans := (self applyPair: opTree to: (Array with: tree1 with: pos1) and: (Array with: tree2 with: pos2)).
^(ans at: 1)
apply: opTree to: tree1 at: pos1 and: tree2 at: pos2 andNew: newTree at: newPos
| ans |
ans := self
applyPair: opTree
to: (Array with: tree1 with: pos1)
and: (Array with: tree2 with: pos2)
andNew: (Array with: newTree with: newPos).
^(ans at: 1)
apply: opTree to: tree1 at: pos1 and: tree2 at: pos2 andNew: newTree at: newPos booleans: list
| ans |
ans := self
applyPair: opTree
to: (Array with: tree1 with: pos1)
and: (Array with: tree2 with: pos2)
andNew: (Array with: newTree with: newPos)
booleans: list.
^(ans at: 1)
applyPair: opTree to: treePair1 and: treePair2
| head res1 resTree1 resPos1 subTree res2 resTree2 resPos2 subsTree |
head := (opTree listElementAt: 1) name.
head = #bott1 ifTrue: [^Array with: (treePair1 at: 1)
with: (ListClause withReverseElements: List new)].
head = #bott2 ifTrue: [^Array with: (treePair2 at: 1)
with: (ListClause withReverseElements: List new)].
head = #rand1 ifTrue: [^treePair1].
head = #rand2 ifTrue: [^treePair2].
res1 := self
applyPair: (opTree listElementAt: 2)
to: treePair1
and: treePair2.
resTree1 := res1 at: 1.
resPos1 := res1 at: 2.
head = #down | (head = #down1) ifTrue: [^Array with: resTree1 with: (self butLast: resPos1)].
head = #up1
ifTrue:
[subTree := self subTree: resTree1 at: resPos1.
subTree listLength = 1
ifTrue: [^res1]
ifFalse: [^Array with: resTree1 with: (self addLast: 1 to: resPos1)]].
head = #up2
ifTrue:
[subTree := self subTree: resTree1 at: resPos1.
subTree listLength < 3
ifTrue: [^res1]
ifFalse: [^Array with: resTree1 with: (self addLast: 2 to: resPos1)]].
head = #top ifTrue: [^Array with: (self subTree: resTree1 at: resPos1)
with: (ListClause withReverseElements: List new)].
head = #null | (head = #null1) ifTrue: [^Array with: resTree1 with: resPos1].
res2 := self
applyPair: (opTree listElementAt: 3)
to: treePair1
and: treePair2.
resTree2 := res2 at: 1.
resPos2 := res2 at: 2.
head = #subs
ifTrue:
[subsTree := self subTree: resTree1 at: resPos1.
^Array with: (self
subs: subsTree
in: resTree2
at: resPos2)
with: resPos2].
^self error: 'Not a valid tree label:' , head printString
applyPair: opTree to: treePair1 and: treePair2 andNew: newTreePair
| head res1 resTree1 resPos1 subTree res2 resTree2 resPos2 subsTree res3 |
head := (opTree listElementAt: 1) name.
head = #T ifTrue: [^newTreePair].
head = #F ifTrue: [^self false].
head = #bott1 ifTrue: [^Array with: (treePair1 at: 1)
with: self emptyList].
head = #bott2 ifTrue: [^Array with: (treePair2 at: 1)
with: self emptyList].
head = #rand1 ifTrue: [^treePair1].
head = #rand2 ifTrue: [^treePair2].
head = #newRand ifTrue: [^newTreePair].
head = #newBott ifTrue: [^Array with: (newTreePair at: 1)
with: self emptyList].
res1 := self
applyPair: (opTree listElementAt: 2)
to: treePair1
and: treePair2
andNew: newTreePair.
resTree1 := res1 at: 1.
resPos1 := res1 at: 2.
head = #NOT ifTrue: [(self isFalse: resTree1)
ifTrue: [^newTreePair]
ifFalse: [^self false]].
head = #down ifTrue: [^Array with: resTree1 with: (self butLast: resPos1)].
head = #down1 ifTrue: [^Array with: resTree1 with: (self butLast: resPos1)].
head = #up1
ifTrue:
[subTree := self subTree: resTree1 at: resPos1.
subTree listLength = 1
ifTrue: [^res1]
ifFalse: [^Array with: resTree1 with: (self addLast: 1 to: resPos1)]].
head = #up2
ifTrue:
[subTree := self subTree: resTree1 at: resPos1.
subTree listLength < 3
ifTrue: [^res1]
ifFalse: [^Array with: resTree1 with: (self addLast: 2 to: resPos1)]].
head = #top ifTrue: [^Array with: (self subTree: resTree1 at: resPos1)
with: self emptyList].
head = #null | (head = #null1) ifTrue: [^Array with: resTree1 with: resPos1].
res2 := self
applyPair: (opTree listElementAt: 3)
to: treePair1
and: treePair2
andNew: newTreePair.
resTree2 := res2 at: 1.
resPos2 := res2 at: 2.
head = #subs
ifTrue:
[subsTree := self subTree: resTree1 at: resPos1.
^Array with: (self
subs: subsTree
in: resTree2
at: resPos2)
with: resPos2].
head = #equals ifTrue: [resTree1 = resTree2
ifTrue: [^Array with: resTree1 with: self emptyList]
ifFalse: [^self false]].
head = #equalsRoot ifTrue: [(resTree1 listElementAt: 1)
= (resTree2 listElementAt: 1)
ifTrue: [^Array with: resTree1 with: self emptyList]
ifFalse: [^self false]].
head = #equalsPos ifTrue: [(self subTree: resTree1 at: resPos1)
= (self subTree: resTree2 at: resPos2)
ifTrue: [^Array with: (self subTree: resTree1 at: resPos1)
with: self emptyList]
ifFalse: [^self false]].
head = #equalsPosRoot ifTrue: [((self subTree: resTree1 at: resPos1)
listElementAt: 1)
= ((self subTree: resTree2 at: resPos2)
listElementAt: 1)
ifTrue: [^Array with: (self subTree: resTree1 at: resPos1)
with: self emptyList]
ifFalse: [^self false]].
head = #implies ifTrue: [(self notFalse: resTree1)
ifTrue: [^res2]
ifFalse: [^self false]].
head = #AND ifTrue: [(self isFalse: resTree1)
| (self isFalse: resTree2)
ifTrue: [^self false]
ifFalse: [^Array with: (self listClauseWithNode: #AND andBranches: (List with: resTree1 with: resTree2))
with: self emptyList]].
head = #OR ifTrue: [(self isFalse: resTree1)
ifTrue: [^self false]
ifFalse: [(self isFalse: resTree2)
ifTrue: [^self false]
ifFalse: [^Array with: (self listClauseWithNode: #OR andBranches: (List with: resTree1 with: resTree2))
with: self emptyList]]].
res3 := self
applyPair: (opTree listElementAt: 4)
to: treePair1
and: treePair2
andNew: newTreePair.
head = #ifthenelse ifTrue: [(self notFalse: resTree1)
ifTrue: [^res2]
ifFalse: [^res3]].
^self error: 'Not a valid tree label:' , head printString
applyPair: opTree to: treePair1 and: treePair2 andNew: newTreePair booleans: list
| head res1 resTree1 resPos1 subTree res2 resTree2 resPos2 subsTree res3 trueObject falseObject orObject andObject |
trueObject := list listElementAt: 1.
falseObject := list listElementAt: 2.
orObject := list listElementAt: 3.
andObject := list listElementAt: 4.
head := (opTree listElementAt: 1) name.
head = #T ifTrue: [^self boolean: trueObject].
head = #F ifTrue: [^self boolean: falseObject].
head = #bott1 ifTrue: [^Array with: (treePair1 at: 1)
with: self emptyList].
head = #bott2 ifTrue: [^Array with: (treePair2 at: 1)
with: self emptyList].
head = #rand1 ifTrue: [^treePair1].
head = #rand2 ifTrue: [^treePair2].
head = #newRand ifTrue: [^newTreePair].
head = #newBott ifTrue: [^Array with: (newTreePair at: 1)
with: self emptyList].
res1 := self
applyPair: (opTree listElementAt: 2)
to: treePair1
and: treePair2
andNew: newTreePair
booleans: list.
resTree1 := res1 at: 1.
resPos1 := res1 at: 2.
head = #NOT ifTrue: [(self isFalse: resTree1)
ifTrue: [^self boolean: trueObject]
ifFalse: [^self boolean: falseObject]].
head = #down ifTrue: [^Array with: resTree1 with: (self butLast: resPos1)].
(head = #down1) ifTrue: [^Array with: resTree1 with: (self butLast: resPos1)].
head = #up1
ifTrue:
[subTree := self subTree: resTree1 at: resPos1.
subTree listLength = 1
ifTrue: [^res1]
ifFalse: [^Array with: resTree1 with: (self addLast: 1 to: resPos1)]].
head = #up2
ifTrue:
[subTree := self subTree: resTree1 at: resPos1.
subTree listLength < 3
ifTrue: [^res1]
ifFalse: [^Array with: resTree1 with: (self addLast: 2 to: resPos1)]].
head = #top ifTrue: [^Array with: (self subTree: resTree1 at: resPos1)
with: self emptyList].
head = #null | (head = #null1) ifTrue: [^Array with: resTree1 with: resPos1].
res2 := self
applyPair: (opTree listElementAt: 3)
to: treePair1
and: treePair2
andNew: newTreePair
booleans: list.
resTree2 := res2 at: 1.
resPos2 := res2 at: 2.
head = #subs
ifTrue:
[subsTree := self subTree: resTree1 at: resPos1.
^Array with: (self
subs: subsTree
in: resTree2
at: resPos2)
with: resPos2].
head = #equals ifTrue: [resTree1 = resTree2
ifTrue: [^Array with: resTree1 with: self emptyList]
ifFalse: [^self boolean: falseObject]].
head = #equalsRoot ifTrue: [(resTree1 listElementAt: 1)
= (resTree2 listElementAt: 1)
ifTrue: [^Array with: resTree1 with: self emptyList]
ifFalse: [^self boolean: falseObject]].
head = #equalsPos ifTrue: [(self subTree: resTree1 at: resPos1)
= (self subTree: resTree2 at: resPos2)
ifTrue: [^Array with: (self subTree: resTree1 at: resPos1)
with: self emptyList]
ifFalse: [^self boolean: falseObject]].
head = #equalsPosRoot ifTrue: [((self subTree: resTree1 at: resPos1)
listElementAt: 1)
= ((self subTree: resTree2 at: resPos2)
listElementAt: 1)
ifTrue: [^Array with: (self subTree: resTree1 at: resPos1)
with: self emptyList]
ifFalse: [^self boolean: falseObject]].
head = #implies ifTrue: [(self notFalse: resTree1)
ifTrue: [^res2]
ifFalse: [^self boolean: falseObject]].
head = #AND ifTrue: [(self isFalse: resTree1)
| (self isFalse: resTree2)
ifTrue: [^self boolean: falseObject]
ifFalse: [^Array with: (self listClauseWithNode: andObject andBranches: (List with: resTree1 with: resTree2))
with: self emptyList]].
head = #OR ifTrue: [(self isFalse: resTree1)
ifTrue: [^self boolean: falseObject]
ifFalse: [(self isFalse: resTree2)
ifTrue: [^self boolean: falseObject]
ifFalse: [^Array with: (self listClauseWithNode: orObject andBranches: (List with: resTree1 with: resTree2))
with: self emptyList]]].
res3 := self
applyPair: (opTree listElementAt: 4)
to: treePair1
and: treePair2
andNew: newTreePair
booleans: list.
head = #ifthenelse ifTrue: [(self notFalse: resTree1)
ifTrue: [^res2]
ifFalse: [^res3]].
^self error: 'Not a valid tree label:' , head printString
butFirst: list
| len |
len := list listLength.
(len = 0) ifTrue: [^list].
^list sublistFrom: 2 to: len
butLast: list
| len |
len := list listLength.
(len = 0) ifTrue: [^list].
^list sublistFrom: 1 to: (len - 1)
subs: subsTree in: tree at: posList
| len firstPos subbedTree |
len := posList listLength.
(len = 0) ifTrue: [^subsTree].
firstPos := (posList listElementAt: 1) + 1.
subbedTree := self subs: subsTree in: (tree listElementAt: firstPos) at: (self butFirst: posList).
^tree modifiedListWith: subbedTree at: firstPos
subTree: tree at: posList
| len first |
len := posList listLength.
(len = 0) ifTrue: [^tree].
first := posList listElementAt: 1.
^self subTree: (tree listElementAt: (first + 1)) at: (self butFirst: posList).
GPCodeHolder class inspect filter
inspectFilter: object
object inspect.
^object
GPCodeHolder class allNodesOf
allLeavesOf: tree
| len posListList newPosListList leafList |
len := tree listLength.
leafList := ListClause withReverseElements: List new.
len = 1 ifTrue: [^ListClause withReverseElements: (List with: leafList)].
2 to: len
do:
[:pos |
posListList := self allLeavesOf: (tree listElementAt: pos).
newPosListList := ListClause withReverseElements: List new.
posListList listElementsReverseDo: [:posList | newPosListList := newPosListList listWithHead: (posList listWithHead: pos - 1)].
leafList := leafList appendStart: newPosListList].
^leafList
allNodesOf: tree
| len leafList posListList newPosListList |
len := tree listLength.
leafList := (ListClause withReverseElements: List new)
listWithHead: (Clause emptyList).
len = 1 ifTrue: [^leafList].
2 to: len
do:
[:pos |
posListList := self allNodesOf: (tree listElementAt: pos).
newPosListList := ListClause withReverseElements: List new.
posListList listElementsReverseDo: [:posList | newPosListList := newPosListList listWithHead: (posList listWithHead: pos - 1)].
leafList := leafList appendStart: newPosListList].
^leafList
numOf: label in: tree
| count treeLabel searchLabel |
(tree listElementAt: 1) isNumber
ifTrue: [treeLabel := tree listElementAt: 1]
ifFalse: [treeLabel := (tree listElementAt: 1) name].
label isNumber
ifTrue: [searchLabel := label]
ifFalse: [searchLabel := label name].
searchLabel = treeLabel
ifTrue: [count := 1]
ifFalse: [count := 0].
(self butFirst: tree)
listElementsReverseDo: [:branch | count := count + (self numOf: label in: branch)].
^count
GPCodeHolder class leafList
leafAndNodeList: tree
| len branches list |
len := tree listLength.
list := ListClause withReverseElements: (List with: (tree listElementAt: 1)).
len = 1
ifFalse:
[branches := tree sublistFrom: 2 to: len.
branches listElementsReverseDo: [:branch | list := list appendStart: (self leafAndNodeList: branch)]].
^list
leafArityList: tree
| len branches leafList first last |
len := tree listLength.
len = 1
ifTrue:
[first := (tree listElementAt: 1) name printString.
last := first at: first size.
last isDigit
ifTrue: [^ListClause withReverseElements: (List with: last digitValue)]
ifFalse: [^ListClause withReverseElements: List new]].
branches := tree sublistFrom: 2 to: len.
leafList := ListClause withReverseElements: List new.
branches listElementsReverseDo: [:branch | leafList := leafList appendStart: (self leafArityList: branch)].
^leafList
leafList: tree
| len branches leafList |
len := tree listLength.
(len = 1) ifTrue: [^ListClause withReverseElements: (List with: (tree listElementAt: 1))].
branches := tree sublistFrom: 2 to: len.
leafList := ListClause withReverseElements: (List new).
branches listElementsReverseDo: [:branch |
leafList := leafList appendStart: (self leafList: branch)].
^leafList
nodeList: tree
| len branches leafList |
len := tree listLength.
len = 1 ifTrue: [^ListClause withReverseElements: List new].
branches := tree sublistFrom: 2 to: len.
leafList := ListClause withReverseElements: (List with: (tree listElementAt: 1)).
branches listElementsReverseDo:
[:branch | leafList := leafList appendStart: (self nodeList: branch)].
^leafList
GPCodeHolder class cummulateList
cummulate: list
^self cummulate: list withTotal: 0
cummulate: list withTotal: num
| sum |
((list listLength) = 0) ifTrue: [^ListClause withReverseElements: List new].
sum := (list listElementAt: 1) + num.
^(self cummulate: (self butFirst: list) withTotal: sum) listWithHead: sum.
GPCodeHolder class fitness
arg: num of: tree
tree listElementAt: num
asBaseTwoList: num
"Op a representation of num as an array in base 2"
| coll |
num < 2
ifTrue:
[^OrderedCollection with: num]
ifFalse:
[coll := (self asBaseTwoList: (num // 2)).
coll addLast: ( num \\ 2).
^coll ].
asBaseTwoList: num size: target
"Op a representation of num as an array in base 2"
| collection len |
collection := self asBaseTwoList: num.
len := collection size.
((target - len) max: 0) timesRepeat: [collection addFirst: 0].
^collection
calc: tree on: testCase
| head firstArg secondArg thirdArg sum |
head := tree listElementAt: 1.
head isNumber ifTrue: [^testCase at: head].
firstArg := self calc: (tree listElementAt: 2)
on: testCase.
head name = #NOT ifTrue: [^1 - firstArg].
head name = #NULL | (head name = #NULL) ifTrue: [^firstArg].
secondArg := self calc: (tree listElementAt: 3)
on: testCase.
head name = #AND ifTrue: [^firstArg * secondArg].
head name = #OR ifTrue: [^firstArg + secondArg - (firstArg * secondArg)].
thirdArg := self calc: (tree listElementAt: 4)
on: testCase.
head name = #MAJ
ifTrue:
[sum := firstArg + secondArg + thirdArg.
sum > 1
ifTrue: [^1]
ifFalse: [^0]].
head name = #AND3 ifTrue: [^firstArg * secondArg * thirdArg].
head name = #OR3
ifTrue:
[sum := firstArg + secondArg + thirdArg.
sum > 0
ifTrue: [^1]
ifFalse: [^0]]
calcMajorityLengthOf: tree over: numInputs
^(self calcMajorityOf: tree over: numInputs)
+ (1 nonFractionDivide: (((self maxDepth: tree)
max: numInputs)
+ 1))
calcMajorityNumNodesOf: tree over: numInputs
^(self calcMajorityOf: tree over: numInputs)
+ (1 nonFractionDivide: (self numNodes: tree)
+ 1)
calcMajorityOf: tree over: numInputs
| success testCase |
success := 0.
0 to: (2 ** numInputs - 1) do: [ :num |
testCase := (self asBaseTwoList: num size: numInputs) asArray.
(self calc: tree on: testCase) = (self majority: testCase)
ifTrue: [ success := success + 1]].
^success
calcParityLengthOf: tree over: numInputs
^(self calcParityOf: tree over: numInputs)
+ (1 nonFractionDivide: (((self maxDepth: tree)
max: numInputs)
+ 1))
calcParityOf: tree over: numInputs
| success testCase |
success := 0.
0 to: (2 ** numInputs - 1) do: [ :num |
testCase := (self asBaseTwoList: num size: numInputs) asArray.
(self calc: tree on: testCase) = (self parity: testCase)
ifTrue: [ success := success + 1]].
^success
calcSingleMajorityOf: tree over: testCase
| success |
success := 0.
(self calc: tree on: testCase) = (self majority: testCase)
ifTrue: [ success := success + 1].
^success
calcSingleParityOf: tree over: testCase
| success |
success := 0.
(self calc: tree on: testCase) = (self parity: testCase)
ifTrue: [ success := success + 1].
^success
head: tree
^tree first
majority: binaryArray
(binaryArray inject: 0 into: [:total :bit | total + bit]) >= ((binaryArray size)//2)
ifTrue: [^1]
ifFalse: [^0]
pad: collection toLength: num
"pad a collection up to length num with 0s"
| extraNeeded padding |
extraNeeded := (num - collection size) max: 0.
padding := Array new: extraNeeded withAll: 0.
parity: binaryArray
^(binaryArray inject: 0 into: [:total :bit | total + bit]) \\ 2
GPCodeHolder class report time
timeString
"return the date and time as a string"
| dateTime |
dateTime := Time dateAndTimeNow.
^(dateTime at: 1) printString , ' at ' , (dateTime at: 2) printString
GPCodeHolder class class initialize
initialize
self resetRandom
GPCodeHolder class random tree
randomEvenTreeDepth: depth nodeList: nl termList: tl
| term nodePair label numBranches bl |
depth = 0
ifTrue:
[term := tl listElementAt: (self randomIntegerUpto: tl listLength).
^ListClause withReverseElements: (List with: term)]
ifFalse:
[nodePair := nl listElementAt: (self randomIntegerUpto: nl listLength).
label := nodePair listElementAt: 1.
numBranches := nodePair listElementAt: 2.
bl := ListClause withReverseElements: List new.
numBranches timesRepeat: [bl := bl listWithHead: (self
randomEvenTreeDepth: depth - 1
nodeList: nl
termList: tl)].
^bl listWithHead: label]
randomEvenTreeDepth: depth nodeList: nl termList: tl seed: num
self seed: num.
^self
randomEvenTreeDepth: depth
nodeList: nl
termList: tl
GPCodeHolder class shuffle
shuffle: list
^self shuffleInto: (ListClause withReverseElements: List new)
from: list
shuffleInto: list from: sourceList
| len pos list1 list2 itemList newList |
sourceList listLength = 0 ifTrue: [^list].
itemList := ListClause withReverseElements: (List with: sourceList first).
len := list listLength.
pos := self randomIntegerFrom: 0 to: len.
pos > 0
ifTrue: [list1 := list sublistFrom: 1 to: pos]
ifFalse: [list1 := ListClause withReverseElements: List new].
pos < len
ifTrue: [list2 := list sublistFrom: (pos + 1) to: len]
ifFalse: [list2 := ListClause withReverseElements: List new].
newList := (list2 appendStart: itemList)
appendStart: list1.
^self shuffleInto: newList from: sourceList last
GPCodeHolder class find
findIn: placeList the: itemList withLimits: limitList
placeList listLength = 0 ifTrue: [^ListClause withReverseElements: List new].
placeList first <= limitList first
ifTrue: [^(self
findIn: placeList last
the: itemList
withLimits: limitList)
listWithHead: itemList first]
ifFalse: [^self
findIn: placeList
the: itemList last
withLimits: limitList last]
GPCodeHolder class random
nextNormalRandom
| sum fact |
sum := 0.
fact := 12.
fact timesRepeat: [sum := sum + self nextRandom].
^(sum - (fact / 2)) * 12 / fact
nextNormalRandomOld
| w w1 w2 |
w := 2.
[w >= 1] whileTrue: [
w1 := self nextRandom.
w2 := self nextRandom.
w := (w1 * w1) + (w2 * w2)].
^w1 * ((-2 * (w ln) / w) sqrt)
nextRandom
^RNG next
randomIntegerFrom: min to: max
^(self nextRandom * (max - min + 1)) truncated + min
randomIntegerUpto: max
^self randomIntegerFrom: 1 to: max
resetRandom
RNG := Random new
seed: num
RNG seed: num
GPCodeHolder class ss-fitness
calcSun: tree at: pos
| head firstArg secondArg |
head := tree listElementAt: 1.
head name = #x1 ifTrue: [^SSN at: pos - 1].
head name = #x2 ifTrue: [^SSN at: pos - 2].
head name = #x4 ifTrue: [^SSN at: pos - 4].
head name = #x8 ifTrue: [^SSN at: pos - 8].
head name = #x16 ifTrue: [^SSN at: pos - 16].
head name = #RNT ifTrue: [^0.5d].
firstArg := self
calcSun: (tree listElementAt: 2)
at: pos.
head name = #SIN ifTrue: [^firstArg asDouble sin].
head name = #COS ifTrue: [^firstArg asDouble cos].
secondArg := self
calcSun: (tree listElementAt: 3)
at: pos.
head name = #PLUS ifTrue: [^firstArg + secondArg].
head name = #MINUS ifTrue: [^firstArg - secondArg].
head name = #TIMES ifTrue: [^firstArg * secondArg].
head name = #SAFEDIVIDE ifTrue: [(secondArg = 0) ifTrue: [^firstArg] ifFalse: [^firstArg nonFractionDivide: secondArg]]
calcSun: tree at: pos withConst: const
| head firstArg secondArg |
head := tree listElementAt: 1.
head name = #x1 ifTrue: [^SSN at: pos - 1].
head name = #x2 ifTrue: [^SSN at: pos - 2].
head name = #x4 ifTrue: [^SSN at: pos - 4].
head name = #x8 ifTrue: [^SSN at: pos - 8].
head name = #x16 ifTrue: [^SSN at: pos - 16].
head name = #RNT ifTrue: [^const asDouble].
firstArg := self
calcSun: (tree listElementAt: 2)
at: pos.
head name = #SIN ifTrue: [^firstArg asDouble sin].
head name = #COS ifTrue: [^firstArg asDouble cos].
secondArg := self
calcSun: (tree listElementAt: 3)
at: pos.
head name = #PLUS ifTrue: [^firstArg + secondArg].
head name = #MINUS ifTrue: [^firstArg - secondArg].
head name = #TIMES ifTrue: [^firstArg * secondArg].
head name = #SAFEDIVIDE ifTrue: [(secondArg = 0) ifTrue: [^firstArg] ifFalse: [^firstArg nonFractionDivide: secondArg]]
calcSunspotLengthOf: tree
| len sumErrorSq predicted errorSq actual |
len := SSN size.
sumErrorSq := 0.
9 to: len
do:
[:pos |
predicted := self calcSun: tree at: pos.
actual := SSN at: pos.
errorSq := predicted - actual ** 2.
sumErrorSq := sumErrorSq + errorSq].
^1 / (sumErrorSq sqrt + 1) + (1 nonFractionDivide: (self maxDepth: tree)
+ 1)
calcSunspotOf: tree
| len sumErrorSq predicted errorSq actual |
len := SSN size.
sumErrorSq := 0.
9 to: len do:
[:pos |
predicted := self calcSun: tree at: pos.
actual := (SSN at: pos).
errorSq := (predicted - actual) **2.
sumErrorSq := sumErrorSq + errorSq].
^(1 / (sumErrorSq + 1))
calcSunspotOf: tree withConst: const
| len sumErrorSq predicted errorSq actual |
len := SSN size.
sumErrorSq := 0.
9 to: len
do:
[:pos |
predicted := self
calcSun: tree
at: pos
withConst: const.
actual := SSN at: pos.
errorSq := predicted - actual ** 2.
sumErrorSq := sumErrorSq + errorSq].
^1 / (sumErrorSq + 1)
GPCodeHolder class support
boolean: bool
^Array with: (self listClauseWith: bool) with: self emptyList
emptyList
^ListClause withReverseElements: List new
false
^Array with: (self emptyList) with: self emptyList
isEmpty: tree
^(tree listLength) = 0
isFalse: tree
| first |
(self isEmpty: tree ) ifTrue: [^true].
first := (tree listElementAt: 1).
first isNumber ifTrue: [^false].
first name = #F ifTrue: [^true].
^false
listClauseFromList: list
^ListClause withReverseElements: (list reverse)
listClauseWith: item
^self listClauseFromList: (List with: item)
listClauseWith: item with: item2
^self listClauseFromList: (List with: item with: item2)
listClauseWith: item with: item2 with: item3
^self listClauseFromList: (List with: item with: item2 with: item3)
listClauseWithNode: item andBranches: list
| newList |
newList := list copy.
newList addFirst: item.
^self listClauseFromList: newList
notFalse: tree
^(self isFalse: tree) not
subTreeOf: tree cutBy: num
| len nextPos |
num = 0 ifTrue: [^tree].
len := tree listLength.
len < 2 ifTrue: [^tree].
nextPos := self listClauseWith: (self randomIntegerUpto: len - 1).
^self subTreeOf: (self subTree: tree at: nextPos)
cutBy: num - 1
GPCodeHolder class parity-fitness
isF: tree
| head len |
head := tree listElementAt: 1.
head isNumber ifTrue: [^0].
head name = #F ifTrue: [^1].
len := tree listLength.
len = 1 ifTrue: [^0].
head name = #NULL | (head name = #null) ifTrue: [^self isF: (tree listElementAt: 2)].
(self isF: (tree listElementAt: 2))
= 0 ifTrue: [^0].
len = 2 ifTrue: [^0].
(self isF: (tree listElementAt: 3))
= 0 ifTrue: [^0].
head name ~= #AND & (head name ~= #OR) ifTrue: [^0].
^1
isT: tree
| head len |
head := tree listElementAt: 1.
head isNumber ifTrue: [^0].
head name = #T ifTrue: [^1].
len := tree listLength.
len = 1 ifTrue: [^0].
head name = #NULL | (head name = #null) ifTrue: [^self isT: (tree listElementAt: 2)].
(self isT: (tree listElementAt: 2))
= 0 ifTrue: [^0].
len = 2 ifTrue: [^0].
(self isT: (tree listElementAt: 3))
= 0 ifTrue: [^0].
head name ~= #AND & (head name ~= #OR) ifTrue: [^0].
^1
GPCodeHolder class testing
isCharacter: item
"comment stating purpose of message"
self halt.
GPCodeHolder class case
lowerCase: stringClause
^StringClause from: stringClause asString asLowercase
GPCodeHolder