diff --git a/deme/deme-id-creation.metta b/deme/deme-id-creation.metta index d98e41a6..82275f07 100644 --- a/deme/deme-id-creation.metta +++ b/deme/deme-id-creation.metta @@ -4,7 +4,7 @@ ;; the number of expansion is set to zero (0) in moses_params.h in the moses_statistics struct ;; for number to string conversion -! (bind! numStr (py-atom str)) +!(bind! numStr (py-atom str)) ;; DemeId Type (: DemeId Type) diff --git a/metapopulation/exemplar-selection.metta b/metapopulation/exemplar-selection.metta index 0a7bece7..f05e34dd 100644 --- a/metapopulation/exemplar-selection.metta +++ b/metapopulation/exemplar-selection.metta @@ -3,26 +3,25 @@ ;; which, for now, is not clear what it is ;; removed python binding for compatibility with workflow -- XXX -; ! (bind! rndfloat (py-atom random.random)) -; ! (bind! round (py-atom round)) -! (bind! NO_EXEMPLAR "empty metapopulation") -! (bind! COMPXY_TEMP 4) -! (bind! INV_TEMP (/ 100.0 COMPXY_TEMP)) +;; bind is not supported for constants (as of 13 NOV) in PeTTa -- thus using function style notation +(= (NO_EXEMPLAR) "empty metapopulation") +(= (COMPXY_TEMP) 4) +(= (INV_TEMP) (/ 100.0 (COMPXY_TEMP))) ;; exemplar selection ;; if the the metapopulation is empty - throw error and quit ;; if only one exemplar in the metapopulation select that ;; if more than one exemplar in mp - get the max penalized score and ;; make a roulette selection on those after converting scores into probability values -(: selectExemplar (-> (OS (Exemplar $a)) (Exemplar $a))) +; (: selectExemplar (-> (OS (Exemplar $a)) (Exemplar $a))) (= (selectExemplar $metaPop) (case $metaPop - ((NilOS (Error $metaPop NO_EXEMPLAR)) + ((NilOS (Error $metaPop (NO_EXEMPLAR))) ((ConsOS $x NilOS) $x) ($_ (let* (($probs (getPnScore $metaPop)) ($hstScr (List.max >= $probs)) ;; get list of normalized ScoreT values - ($normalizedProbs (normalizeProbs INV_TEMP $hstScr $probs)) + ($normalizedProbs (normalizeProbs (INV_TEMP) $hstScr $probs)) ($sum (List.sum $normalizedProbs))) (rouletteSelect $metaPop $probs $sum) ))))) @@ -30,7 +29,7 @@ ;; (ConsOS $x $xs) -- pattern for the list of exemplars constructed by Cons ;; made use of deconstruction by pattern matching -(: getPnScore (-> (OS (Exemplar $a)) (List Number))) +; (: getPnScore (-> (OS (Exemplar $a)) (List Number))) (= (getPnScore NilOS) Nil) (= (getPnScore (ConsOS $x $xs)) (let (mkExemplar $tree1 $demeId1 (mkCscore $scrr $cpxy $cpxyPen $uniPen $penScr) $bscr1) $x @@ -38,11 +37,11 @@ ;; a function to normalize the score values into normalized scores of Boltzman distribution ;; the function is specific to this distribution type -(: normalizeProbs (-> Number Number (List Number) (List Number))) +; (: normalizeProbs (-> Number Number (List Number) (List Number))) (= (normalizeProbs $invTemp $best Nil) Nil) (= (normalizeProbs $invTemp $best (Cons $x $xs)) (let* - (($new (if (isInf $x) 0 (pow-math EXP (* (- $x $best) $invTemp)))) + (($new (if (isinf-math $x) 0 (exp (* (- $x $best) $invTemp)))) ($c (normalizeProbs $invTemp $best $xs))) (Cons $new $c))) @@ -53,9 +52,9 @@ ;; $sum -- sum of penalized score values after normalization using the (pow-math EXP (* (- $val $higestScr) INV_TEMP)) ;; to favour the selection of high scoring exemplars -- Boltzman distribution -(: rouletteSelect (-> (OS (Exemplar $a)) (List Number) Number (Exemplar $a))) +; (: rouletteSelect (-> (OS (Exemplar $a)) (List Number) Number (Exemplar $a))) (= (rouletteSelect $metaPop $probs $sum) - (let* (($rndfloat (randomFloat)) + (let* (($rndfloat (random-float 0 1)) ($ajstdSum (* $sum $rndfloat)) ($index (roulette $probs 0 $ajstdSum))) (OS.getByIdx $index $metaPop))) @@ -64,7 +63,7 @@ ;; $probs -- (List Number) -- list of prob values ;; $sIdx -- start index -- 0 ;; $ajstdsum -- sum of probabilities which has been multiplied with a random with random value in (0 .. 1) range -(: roulette (-> (List Number) Number Number Number)) +; (: roulette (-> (List Number) Number Number Number)) (= (roulette Nil $sIdx $ajstdsum) (- $sIdx 1)) (= (roulette (Cons $p $xs) $sIdx $ajstdsum) (if (<= (- $ajstdsum $p) 0) diff --git a/metapopulation/exemplar-type.metta b/metapopulation/exemplar-type.metta index b725d657..e5938dcb 100644 --- a/metapopulation/exemplar-type.metta +++ b/metapopulation/exemplar-type.metta @@ -29,7 +29,7 @@ ;; Penalized Score: very_worst_score (e.g., -1e37) ?? ;; Behavioral Score: empty set -- Nil -(: xmplrInit (-> (Tree $a) DemeId Xmplr)) +; (: xmplrInit (-> (Tree $a) DemeId Xmplr)) (= (xmplrInit $tree (mkDemeId $id)) (let ($iScore $iComplexity $iComplexityPen $iUinformityPen $iBehavioralScore) ((pow-math 10 -37) 0 0 (pow-math 10 -37) Nil) (mkXmplr diff --git a/metapopulation/metapopulation.metta b/metapopulation/metapopulation.metta index f4fe10fe..0feab6df 100644 --- a/metapopulation/metapopulation.metta +++ b/metapopulation/metapopulation.metta @@ -1,7 +1,7 @@ ;; importing constants -- a suitable value for each of these has to be determined based on experiment -; ! (bind! MIN-POOL-SIZE 250) ;; this is copied from the c++ implementation -- XXX a values that works best for the current implementation should be found by experrimentation?? -; ! (bind! COMP-TEMP 6.0) -; ! (bind! CAP-COEF 50) +; !(bind! MIN-POOL-SIZE 250) ;; this is copied from the c++ implementation -- XXX a values that works best for the current implementation should be found by experrimentation?? +; !(bind! COMP-TEMP 6.0) +; !(bind! CAP-COEF 50) ;; Exemplar type (: Exemplar (-> $a Type)) @@ -13,36 +13,36 @@ ;; $compTemp -- used to calculate the range of scores that are likely to be preserved by the selection process ;; $capCoef -- use to balance the number of individuals in the metapopulation by balancing population size so that it neither blows out the RAM or lacks enough variety -- may not even be necessary ;; $genCount -- used with $capCoef in the determination of max pop allowed given the above contstraints -(: resizeMetapop (-> (OS (Exemplar $a)) Number Number Number Number Number (OS (Exemplar $a)))) +; (: resizeMetapop (-> (OS (Exemplar $a)) Number Number Number Number Number (OS (Exemplar $a)))) (= (resizeMetapop (ConsOS $exemplar $rest) $nToKeep $minPoolSize $compTemp $capCoef $genCount) - (let ($size $capSize) ((OS.length (ConsOS $exmplar $rest)) (int (capSize $capCoef $genCount))) ;; bind two variables with two function calls + (let ($size $capSize) ((eval (OS.length (ConsOS $exmplar $rest))) (eval (int (capSize $capCoef $genCount)))) ;; bind two variables with two function calls (if (<= $size $minPoolSize) - (ConsOS $exemplar $rest) + (ConsOS $exemplar $rest) (let (mkExemplar $tree $demeId $cScore $bScore) $exemplar ;; exemplar with the best score - (chain (getPenScore $cScore) $topScore + (chain (eval (getPenScore $cScore)) $topScore (chain (- $topScore (usefulScoreRange $compTemp)) $worstScore (chain (getBetterCandidates (ConsOS $exemplar $rest) $worstScore) $reducedMetapop - (chain (OS.length $reducedMetapop) $popSize + (chain (eval (OS.length $reducedMetapop)) $popSize (if (<= $popSize $capSize) $reducedMetapop (cullAtRandom $reducedMetapop $nToKeep (- $popSize $capSize))))))))))) ;; compute useful range -(: usefulScoreRange (-> Number Number)) +; (: usefulScoreRange (-> Number Number)) (= (usefulScoreRange $compTemp) (/ (* $compTemp 30.0) 100.0)) ;; calculates cap size based on number of generations -- must be recomuted based on actual data in the new implementation ;; for the time being implement the C++ code as it -(: capSize (-> Number Number Number)) +; (: capSize (-> Number Number Number)) (= (capSize $capCoef $genCount) - (* (* $capCoef (+ $genCount 250)) (+ 1 (* 2 (pow-math EXP (/ (* -1 $genCount) 500)))))) + (* (* $capCoef (+ $genCount 250)) (+ 1 (* 2 (exp (/ (* -1 $genCount) 500)))))) ;; getBetterCandidates -- returns all the memmbers whose score are greater than or equal to a predetermined worst score -(: getBetterCandidates (-> (OS (Exemplar $a)) Number (OS (Exemplar $a)))) +; (: getBetterCandidates (-> (OS (Exemplar $a)) Number (OS (Exemplar $a)))) (= (getBetterCandidates NilOS $score) NilOS) (= (getBetterCandidates (ConsOS $exemplar $rest) $worstScore) (let (mkExemplar $tree $demeId $cScore $bScore) $exemplar - (chain (getPenScore $cScore) $penScore + (chain (eval (getPenScore $cScore)) $penScore (if (>= $penScore $worstScore) (ConsOS $exemplar (getBetterCandidates $rest $worstScore)) @@ -50,12 +50,12 @@ ;; cullAtRandom -- maintains the top N exemplars and removes candidates that are in the list with worse scores by chance ;; gives a certain chance for poor candidates to survive -(: cullAtRandom (-> (OS (Exemplar $a)) Number Number (OS (Exemplar $a)))) +; (: cullAtRandom (-> (OS (Exemplar $a)) Number Number (OS (Exemplar $a)))) (= (cullAtRandom (ConsOS $exemplar $rest) $offset $nToRemove) (if (> $nToRemove 0) - (chain (OS.length (ConsOS $exemplar $rest)) $popSize - (chain (random-int &rng $offset $popSize) $index - (chain (OS.removeByIdx (ConsOS $exemplar $rest) $index) $newPop + (chain (eval (OS.length (ConsOS $exemplar $rest))) $popSize + (chain (- (random-int $offset $popSize) 1) $index + (chain (eval (OS.removeByIdx (ConsOS $exemplar $rest) $index)) $newPop (cullAtRandom $newPop $offset (- $nToRemove 1))))) (ConsOS $exemplar $rest))) @@ -64,37 +64,37 @@ ;; (Exemplar $a): First exemplar ;; (Exemplar $a): Second exemplar ;; Return: L (less), E (equal), or G (greater) -(: compareExemplar (-> (Exemplar $a) (Exemplar $a) Atom)) +; (: compareExemplar (-> (Exemplar $a) (Exemplar $a) Atom)) (= (compareExemplar (mkExemplar $tree1 $demeId1 $cscore1 $bscr1) (mkExemplar $tree2 $demeId2 $cscore2 $bscr2)) - (if (apply < $cscore1 $cscore2) L - (if (apply == $cscore1 $cscore2) E G))) + (if (eval (apply < $cscore1 $cscore2) )L + (if (eval (apply == $cscore1 $cscore2)) E G))) ;; Extract BScore from Exemplar ;; Params: ;; (Exemplar $a): exemplar ;; Return: $bscore -(: getExemplarBScore (-> (Exemplar $a) BehavioralScore)) +; (: getExemplarBScore (-> (Exemplar $a) BehavioralScore)) (= (getExemplarBScore (mkExemplar $tree $demeId $cscore $bscore)) $bscore) ;; Extract PenScore from Exemplar ;; Params: ;; (Exemplar $a): exemplar ;; Return: $penScore -(: getExemplarPenScore (-> (Exemplar $a) Number)) -(= (getExemplarPenScore (mkExemplar $tree $demeId $cscore $bscore)) (getPenScore $cscore)) +; (: getExemplarPenScore (-> (Exemplar $a) Number)) +(= (getExemplarPenScore (mkExemplar $tree $demeId $cscore $bscore)) (eval (getPenScore $cscore))) ;; Extract Cscore from Exemplar ;; Params: ;; (Exemplar $a): exemplar ;; Return: $cscore -(: getExemplarCscore (-> (Exemplar $a) Cscore)) +; (: getExemplarCscore (-> (Exemplar $a) Cscore)) (= (getExemplarCscore (mkExemplar $tree $demeId $cscore $bscore)) $cscore) ;; Extract Tree from Exemplar ;; Params: ;; (Exemplar $a): exemplar ;; Return: $tree -(: getExemplarTree (-> (Exemplar $a) (Tree $a))) +; (: getExemplarTree (-> (Exemplar $a) (Tree $a))) (= (getExemplarTree (mkExemplar $tree $demeId $cscore $bscore)) $tree) \ No newline at end of file diff --git a/metapopulation/tests/exemplar-selection-test.metta b/metapopulation/tests/exemplar-selection-test.metta new file mode 100644 index 00000000..ea6d3afd --- /dev/null +++ b/metapopulation/tests/exemplar-selection-test.metta @@ -0,0 +1,109 @@ +; !(register-module! ../../../metta-moses) +!(import! &self ../../../metta-moses/utilities/ordered-set) +!(import! &self ../../../metta-moses/utilities/list-methods) +!(import! &self ../../../metta-moses/utilities/general-helpers) +!(import! &self ../../../metta-moses/metapopulation/exemplar-selection) +!(import! &self ../../../metta-moses/metapopulation/metapopulation) +!(import! &self ../../../metta-moses/utilities/tree) +(: A Bool) +;; Get penalized scores +; !(assertEqual (getPnScore NilOS) Nil) +; !(assertEqual +; (getPnScore +; (ConsOS (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.2 1 0.1 0.1 -0.4) (mkBScore (Cons 0 (Cons 0 Nil)))) +; (ConsOS (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil)) (mkDemeId "1") (mkCscore -0.1 2 0.2 0.3 -0.6) (mkBScore (Cons 0 (Cons 0 Nil)))) +; NilOS))) +; (Cons -0.4 (Cons -0.6 Nil))) +; !(assertEqual +; (getPnScore +; (ConsOS +; (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil)) (mkDemeId "1") (mkCscore -0.1 2 0.2 0.3 -0.6) (mkBScore (Cons 0 (Cons 0 Nil)))) +; (ConsOS +; (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil)) (mkDemeId "1") (mkCscore -0.1 2 0.2 0.3 -0.6) (mkBScore (Cons 0 (Cons 0 Nil)))) +; NilOS))) +; (Cons -0.6 (Cons -0.6 Nil))) + +; ;; max of list of values of (List $a) type +; !(assertEqual (List.max >= Nil) Nil) +; !(assertEqual (List.max >= (Cons 0.75 Nil)) 0.75) +; !(assertEqual (List.max >= (Cons 0.9 (Cons 0.5 Nil))) 0.9) +; !(assertEqual (List.max >= (Cons 0.1 (Cons 0.4 (Cons 0.65 (Cons 0.9 Nil))))) 0.9) + +; ;; Boltzman adjusted probablity values +; !(assertEqual (let $a (List.sum (normalizeProbs (INV_TEMP) 0.5 (Cons 0.3 Nil))) (get-type $a)) Number) +; !(assertEqual (let $a (List.sum (normalizeProbs (INV_TEMP) 0.7 (Cons 0.6 (Cons 0.2 Nil)))) (get-type $a)) Number) +; !(assertEqual (let $a (List.sum (normalizeProbs (INV_TEMP) 0.95 (Cons 0.95 (Cons 0.9 (Cons 0.85 Nil))))) (get-type $a)) Number) +; !(assertEqual (let $a (List.sum (normalizeProbs (INV_TEMP) 1.0 (Cons 0.9 (Cons 0.85 (Cons 0.95 (Cons 0.7 Nil)))))) (get-type $a)) Number) + +; ;; roulette -- spinning the wheel favoring expressions with higher scores +; !(assertEqual (roulette (Cons 1.0 Nil) 0 0.57) 0) +; !(assertEqual (roulette (Cons 1.0 (Cons 0.0067 Nil)) 0 0.9564) 0) +; !(assertEqual (roulette (Cons 0.0821 (Cons 0.0067 (Cons 1 Nil))) 0 0.7622) 2) +; !(assertEqual (roulette (Cons 0.2 (Cons 0.5 (Cons 0.6 (Cons 0.2 Nil)))) 0 1.1) 2) +; !(assertEqual (roulette (Cons 0.0067 (Cons 0.0821 (Cons 1.0 Nil))) 0 0.0108) 1) + +;; Tree Definitions +(= (treeA) (mkTree (mkNode A) Nil)) +(= (treeB) (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode C) Nil) Nil)))) +(= (treeC) (mkTree (mkNode OR) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode B) Nil) Nil)))) +(= (treeY) (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode Z) Nil) Nil)))) +(= (treeZ) (mkTree (mkNode OR) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode X) Nil) Nil)))) +(= (tree2) (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) Nil))) +(= (tree3) (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode E) Nil) Nil)))) + +;; rouletteSelect +(= (exemplar-list0) (ConsOS (mkExemplar (treeA) (mkDemeId "1") (mkCscore 0.9 0.5 0.2 0.1 -0.6) (mkBscore (Cons 1 (Cons 0 Nil)))) + (ConsOS (mkExemplar (treeB) (mkDemeId "2") (mkCscore 0.85 0.6 0.1 0.05 0.6) (mkBscore (Cons 1 (Cons 0 Nil)))) + (ConsOS (mkExemplar (treeC) (mkDemeId "3") (mkCscore 0.4 0.7 0.05 0.01 0.3) (mkBscore (Cons 1 (Cons 0 Nil)))) + NilOS)))) +!(assertEqual + (let $choice (rouletteSelect (exemplar-list0) + (Cons 0.996 (Cons 1.0 (Cons 0.9857 Nil))) 2.9817) + (OS.contains (exemplar-list0) $choice)) True) + +(= (exemplar-list1) (ConsOS (mkExemplar (treeA) (mkDemeId "9") (mkCscore 0.91 0.3 0.25 0.13 0.3) (mkBscore (Cons 1 (Cons 0 Nil)))) + (ConsOS (mkExemplar (treeY) (mkDemeId "10") (mkCscore 0.88 0.4 0.22 0.12 0.4) (mkBscore (Cons 1 (Cons 1 Nil)))) + (ConsOS (mkExemplar (treeZ) (mkDemeId "11") (mkCscore 0.83 0.5 0.15 0.10 0.5) (mkBscore (Cons 0 (Cons 1 Nil)))) + NilOS)))) +!(assertEqual + (let $choice (rouletteSelect (exemplar-list1) (Cons 0.9753 (Cons 0.9936 (Cons 1.0 Nil))) 2.9689) + (OS.contains (exemplar-list1) $choice )) True) + +(= (exemplar-list2) (ConsOS (mkExemplar (treeA) (mkDemeId "7") (mkCscore 0.95 0.3 0.2 0.1 0.2) (mkBscore (Cons 1 (Cons 1 Nil)))) + (ConsOS (mkExemplar (tree2) (mkDemeId "8") (mkCscore 0.80 0.6 0.1 0.05 0.2) (mkBscore (Cons 1 (Cons 1 Nil)))) + (ConsOS (mkExemplar (tree3) (mkDemeId "12") (mkCscore 0.5 0.9 0.05 0.03 0.1) (mkBscore (Cons 1 (Cons 0 Nil)))) + NilOS)))) + +!(assertEqual + (let $choice (rouletteSelect (exemplar-list2) (Cons 0.9654 (Cons 1.0 (Cons 0.9057 Nil))) 2.8711) + (OS.contains (exemplar-list2) $choice)) True) + +;; selectExemplar +!(assertEqual (selectExemplar NilOS) (Error NilOS "empty metapopulation")) + +(= (treeA-x) (mkExemplar treeA (mkDemeId "7") (mkCscore 0.95 0.3 0.2 0.1 0.4) (mkBscore (Cons 1 (Cons 1 Nil))))) + +!(assertEqual (selectExemplar (ConsOS (treeA-x) NilOS)) (treeA-x)) + +(= (test1-exemplars) + (ConsOS (mkExemplar (treeA) (mkDemeId "1") (mkCscore 0.8 0.3 0.1 0.01 0.4) (mkBscore (Cons 1 (Cons 1 Nil)))) + (ConsOS (mkExemplar (tree2) (mkDemeId "2") (mkCscore 0.7 0.4 0.05 0.05 0.5) (mkBscore (Cons 0 (Cons 1 Nil)))) + (ConsOS (mkExemplar (tree3) (mkDemeId "3") (mkCscore 0.6 0.5 0.0 0.0 0.6) (mkBscore (Cons 1 (Cons 0 Nil)))) + NilOS)))) + +!(assertEqual (let $choice (selectExemplar (test1-exemplars)) (OS.contains (test1-exemplars) $choice)) True) + +(= (test2-exemplars) + (ConsOS (mkExemplar (treeA) (mkDemeId "101") (mkCscore 0.9 0.3 0.05 0.02 0.6) + (mkBscore (Cons 1 (Cons 1 (Cons 1 (Cons 1 Nil)))))) + (ConsOS (mkExemplar (treeB) (mkDemeId "102") (mkCscore 0.6 0.5 0.1 0.1 0.5) (mkBscore (Cons 1 (Cons 1 (Cons 1 (Cons 1 Nil)))))) + (ConsOS (mkExemplar (treeC) (mkDemeId "103") (mkCscore 0.4 0.4 0.05 0.1 0.4) (mkBscore (Cons 0 (Cons 0 (Cons 1 (Cons 0 Nil)))))) + NilOS)))) +!(assertEqual (let $choice (selectExemplar (test2-exemplars)) (OS.contains (test2-exemplars) $choice)) True) + +(= (metaPop) (ConsOS (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.2 1 0.1 0.1 5) (mkBScore (Cons 0 (Cons 0 Nil)))) + (ConsOS (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil)) (mkDemeId "1") (mkCscore -0.1 2 0.2 0.3 -0.6) (mkBScore (Cons 0 (Cons 0 Nil)))) + (ConsOS (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil)) (mkDemeId "1") (mkCscore -0.1 2 0.2 0.3 -0.006) (mkBScore (Cons 0 (Cons 0 Nil)))) NilOS)))) + +; ! (assertEqual (let $choice (selectExemplar metaPop) ((get-type $choice) (OS.contains metaPop $choice))) ((Exemplar Bool) True)) +!(assertEqual (let $choice (selectExemplar (metaPop)) (OS.contains (metaPop) $choice)) True) diff --git a/metapopulation/tests/exemplar-selection-testold.metta b/metapopulation/tests/exemplar-selection-testold.metta index cd20b752..ea6d3afd 100644 --- a/metapopulation/tests/exemplar-selection-testold.metta +++ b/metapopulation/tests/exemplar-selection-testold.metta @@ -1,109 +1,109 @@ -! (register-module! ../../../metta-moses) -! (import! &self metta-moses:utilities:ordered-set) -! (import! &self metta-moses:utilities:list-methods) -! (import! &self metta-moses:utilities:general-helpers) -! (import! &self metta-moses:metapopulation:exemplar-selection) -! (import! &self metta-moses:metapopulation:metapopulation) -! (import! &self metta-moses:utilities:tree) +; !(register-module! ../../../metta-moses) +!(import! &self ../../../metta-moses/utilities/ordered-set) +!(import! &self ../../../metta-moses/utilities/list-methods) +!(import! &self ../../../metta-moses/utilities/general-helpers) +!(import! &self ../../../metta-moses/metapopulation/exemplar-selection) +!(import! &self ../../../metta-moses/metapopulation/metapopulation) +!(import! &self ../../../metta-moses/utilities/tree) (: A Bool) ;; Get penalized scores -! (assertEqual (getPnScore NilOS) Nil) -! (assertEqual - (getPnScore - (ConsOS (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.2 1 0.1 0.1 -0.4) (mkBScore (Cons 0 (Cons 0 Nil)))) - (ConsOS (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil)) (mkDemeId "1") (mkCscore -0.1 2 0.2 0.3 -0.6) (mkBScore (Cons 0 (Cons 0 Nil)))) - NilOS))) - (Cons -0.4 (Cons -0.6 Nil))) -! (assertEqual - (getPnScore - (ConsOS - (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil)) (mkDemeId "1") (mkCscore -0.1 2 0.2 0.3 -0.6) (mkBScore (Cons 0 (Cons 0 Nil)))) - (ConsOS - (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil)) (mkDemeId "1") (mkCscore -0.1 2 0.2 0.3 -0.6) (mkBScore (Cons 0 (Cons 0 Nil)))) - NilOS))) - (Cons -0.6 (Cons -0.6 Nil))) +; !(assertEqual (getPnScore NilOS) Nil) +; !(assertEqual +; (getPnScore +; (ConsOS (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.2 1 0.1 0.1 -0.4) (mkBScore (Cons 0 (Cons 0 Nil)))) +; (ConsOS (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil)) (mkDemeId "1") (mkCscore -0.1 2 0.2 0.3 -0.6) (mkBScore (Cons 0 (Cons 0 Nil)))) +; NilOS))) +; (Cons -0.4 (Cons -0.6 Nil))) +; !(assertEqual +; (getPnScore +; (ConsOS +; (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil)) (mkDemeId "1") (mkCscore -0.1 2 0.2 0.3 -0.6) (mkBScore (Cons 0 (Cons 0 Nil)))) +; (ConsOS +; (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil)) (mkDemeId "1") (mkCscore -0.1 2 0.2 0.3 -0.6) (mkBScore (Cons 0 (Cons 0 Nil)))) +; NilOS))) +; (Cons -0.6 (Cons -0.6 Nil))) -;; max of list of values of (List $a) type -! (assertEqual (List.max >= Nil) Nil) -! (assertEqual (List.max >= (Cons 0.75 Nil)) 0.75) -! (assertEqual (List.max >= (Cons 0.9 (Cons 0.5 Nil))) 0.9) -! (assertEqual (List.max >= (Cons 0.1 (Cons 0.4 (Cons 0.65 (Cons 0.9 Nil))))) 0.9) +; ;; max of list of values of (List $a) type +; !(assertEqual (List.max >= Nil) Nil) +; !(assertEqual (List.max >= (Cons 0.75 Nil)) 0.75) +; !(assertEqual (List.max >= (Cons 0.9 (Cons 0.5 Nil))) 0.9) +; !(assertEqual (List.max >= (Cons 0.1 (Cons 0.4 (Cons 0.65 (Cons 0.9 Nil))))) 0.9) -;; Boltzman adjusted probablity values -! (assertEqual (let $a (List.sum (normalizeProbs INV_TEMP 0.5 (Cons 0.3 Nil))) (get-type $a)) Number) -! (assertEqual (let $a (List.sum (normalizeProbs INV_TEMP 0.7 (Cons 0.6 (Cons 0.2 Nil)))) (get-type $a)) Number) -! (assertEqual (let $a (List.sum (normalizeProbs INV_TEMP 0.95 (Cons 0.95 (Cons 0.9 (Cons 0.85 Nil))))) (get-type $a)) Number) -! (assertEqual (let $a (List.sum (normalizeProbs INV_TEMP 1.0 (Cons 0.9 (Cons 0.85 (Cons 0.95 (Cons 0.7 Nil)))))) (get-type $a)) Number) +; ;; Boltzman adjusted probablity values +; !(assertEqual (let $a (List.sum (normalizeProbs (INV_TEMP) 0.5 (Cons 0.3 Nil))) (get-type $a)) Number) +; !(assertEqual (let $a (List.sum (normalizeProbs (INV_TEMP) 0.7 (Cons 0.6 (Cons 0.2 Nil)))) (get-type $a)) Number) +; !(assertEqual (let $a (List.sum (normalizeProbs (INV_TEMP) 0.95 (Cons 0.95 (Cons 0.9 (Cons 0.85 Nil))))) (get-type $a)) Number) +; !(assertEqual (let $a (List.sum (normalizeProbs (INV_TEMP) 1.0 (Cons 0.9 (Cons 0.85 (Cons 0.95 (Cons 0.7 Nil)))))) (get-type $a)) Number) -;; roulette -- spinning the wheel favoring expressions with higher scores -! (assertEqual (roulette (Cons 1.0 Nil) 0 0.57) 0) -! (assertEqual (roulette (Cons 1.0 (Cons 0.0067 Nil)) 0 0.9564) 0) -! (assertEqual (roulette (Cons 0.0821 (Cons 0.0067 (Cons 1 Nil))) 0 0.7622) 2) -! (assertEqual (roulette (Cons 0.2 (Cons 0.5 (Cons 0.6 (Cons 0.2 Nil)))) 0 1.1) 2) -! (assertEqual (roulette (Cons 0.0067 (Cons 0.0821 (Cons 1.0 Nil))) 0 0.0108) 1) +; ;; roulette -- spinning the wheel favoring expressions with higher scores +; !(assertEqual (roulette (Cons 1.0 Nil) 0 0.57) 0) +; !(assertEqual (roulette (Cons 1.0 (Cons 0.0067 Nil)) 0 0.9564) 0) +; !(assertEqual (roulette (Cons 0.0821 (Cons 0.0067 (Cons 1 Nil))) 0 0.7622) 2) +; !(assertEqual (roulette (Cons 0.2 (Cons 0.5 (Cons 0.6 (Cons 0.2 Nil)))) 0 1.1) 2) +; !(assertEqual (roulette (Cons 0.0067 (Cons 0.0821 (Cons 1.0 Nil))) 0 0.0108) 1) ;; Tree Definitions -! (bind! treeA (mkTree (mkNode A) Nil)) -! (bind! treeB (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode C) Nil) Nil)))) -! (bind! treeC (mkTree (mkNode OR) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode B) Nil) Nil)))) -! (bind! treeY (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode Z) Nil) Nil)))) -! (bind! treeZ (mkTree (mkNode OR) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode X) Nil) Nil)))) -! (bind! tree2 (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) Nil))) -! (bind! tree3 (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode E) Nil) Nil)))) +(= (treeA) (mkTree (mkNode A) Nil)) +(= (treeB) (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode C) Nil) Nil)))) +(= (treeC) (mkTree (mkNode OR) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode B) Nil) Nil)))) +(= (treeY) (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode Z) Nil) Nil)))) +(= (treeZ) (mkTree (mkNode OR) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode X) Nil) Nil)))) +(= (tree2) (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) Nil))) +(= (tree3) (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode E) Nil) Nil)))) ;; rouletteSelect -! (bind! exemplar-list0 (ConsOS (mkExemplar treeA (mkDemeId "1") (mkCscore 0.9 0.5 0.2 0.1 -0.6) (mkBscore (Cons 1 (Cons 0 Nil)))) - (ConsOS (mkExemplar treeB (mkDemeId "2") (mkCscore 0.85 0.6 0.1 0.05 0.6) (mkBscore (Cons 1 (Cons 0 Nil)))) - (ConsOS (mkExemplar treeC (mkDemeId "3") (mkCscore 0.4 0.7 0.05 0.01 0.3) (mkBscore (Cons 1 (Cons 0 Nil)))) +(= (exemplar-list0) (ConsOS (mkExemplar (treeA) (mkDemeId "1") (mkCscore 0.9 0.5 0.2 0.1 -0.6) (mkBscore (Cons 1 (Cons 0 Nil)))) + (ConsOS (mkExemplar (treeB) (mkDemeId "2") (mkCscore 0.85 0.6 0.1 0.05 0.6) (mkBscore (Cons 1 (Cons 0 Nil)))) + (ConsOS (mkExemplar (treeC) (mkDemeId "3") (mkCscore 0.4 0.7 0.05 0.01 0.3) (mkBscore (Cons 1 (Cons 0 Nil)))) NilOS)))) -! (assertEqual - (let $choice (rouletteSelect exemplar-list0 +!(assertEqual + (let $choice (rouletteSelect (exemplar-list0) (Cons 0.996 (Cons 1.0 (Cons 0.9857 Nil))) 2.9817) - (OS.contains exemplar-list0 $choice)) True) + (OS.contains (exemplar-list0) $choice)) True) -! (bind! exemplar-list1 (ConsOS (mkExemplar treeA (mkDemeId "9") (mkCscore 0.91 0.3 0.25 0.13 0.3) (mkBscore (Cons 1 (Cons 0 Nil)))) - (ConsOS (mkExemplar treeY (mkDemeId "10") (mkCscore 0.88 0.4 0.22 0.12 0.4) (mkBscore (Cons 1 (Cons 1 Nil)))) - (ConsOS (mkExemplar treeZ (mkDemeId "11") (mkCscore 0.83 0.5 0.15 0.10 0.5) (mkBscore (Cons 0 (Cons 1 Nil)))) +(= (exemplar-list1) (ConsOS (mkExemplar (treeA) (mkDemeId "9") (mkCscore 0.91 0.3 0.25 0.13 0.3) (mkBscore (Cons 1 (Cons 0 Nil)))) + (ConsOS (mkExemplar (treeY) (mkDemeId "10") (mkCscore 0.88 0.4 0.22 0.12 0.4) (mkBscore (Cons 1 (Cons 1 Nil)))) + (ConsOS (mkExemplar (treeZ) (mkDemeId "11") (mkCscore 0.83 0.5 0.15 0.10 0.5) (mkBscore (Cons 0 (Cons 1 Nil)))) NilOS)))) -! (assertEqual - (let $choice (rouletteSelect exemplar-list1 (Cons 0.9753 (Cons 0.9936 (Cons 1.0 Nil))) 2.9689) - (OS.contains exemplar-list1 $choice )) True) +!(assertEqual + (let $choice (rouletteSelect (exemplar-list1) (Cons 0.9753 (Cons 0.9936 (Cons 1.0 Nil))) 2.9689) + (OS.contains (exemplar-list1) $choice )) True) -! (bind! exemplar-list2 (ConsOS (mkExemplar treeA (mkDemeId "7") (mkCscore 0.95 0.3 0.2 0.1 0.2) (mkBscore (Cons 1 (Cons 1 Nil)))) - (ConsOS (mkExemplar tree2 (mkDemeId "8") (mkCscore 0.80 0.6 0.1 0.05 0.2) (mkBscore (Cons 1 (Cons 1 Nil)))) - (ConsOS (mkExemplar tree3 (mkDemeId "12") (mkCscore 0.5 0.9 0.05 0.03 0.1) (mkBscore (Cons 1 (Cons 0 Nil)))) +(= (exemplar-list2) (ConsOS (mkExemplar (treeA) (mkDemeId "7") (mkCscore 0.95 0.3 0.2 0.1 0.2) (mkBscore (Cons 1 (Cons 1 Nil)))) + (ConsOS (mkExemplar (tree2) (mkDemeId "8") (mkCscore 0.80 0.6 0.1 0.05 0.2) (mkBscore (Cons 1 (Cons 1 Nil)))) + (ConsOS (mkExemplar (tree3) (mkDemeId "12") (mkCscore 0.5 0.9 0.05 0.03 0.1) (mkBscore (Cons 1 (Cons 0 Nil)))) NilOS)))) -! (assertEqual - (let $choice (rouletteSelect exemplar-list2 (Cons 0.9654 (Cons 1.0 (Cons 0.9057 Nil))) 2.8711) - (OS.contains exemplar-list2 $choice)) True) +!(assertEqual + (let $choice (rouletteSelect (exemplar-list2) (Cons 0.9654 (Cons 1.0 (Cons 0.9057 Nil))) 2.8711) + (OS.contains (exemplar-list2) $choice)) True) ;; selectExemplar -! (assertEqual (selectExemplar NilOS) (Error NilOS "empty metapopulation")) +!(assertEqual (selectExemplar NilOS) (Error NilOS "empty metapopulation")) -! (bind! treeA-x (mkExemplar treeA (mkDemeId "7") (mkCscore 0.95 0.3 0.2 0.1 0.4) (mkBscore (Cons 1 (Cons 1 Nil))))) +(= (treeA-x) (mkExemplar treeA (mkDemeId "7") (mkCscore 0.95 0.3 0.2 0.1 0.4) (mkBscore (Cons 1 (Cons 1 Nil))))) -! (assertEqual (selectExemplar (ConsOS treeA-x NilOS)) treeA-x) +!(assertEqual (selectExemplar (ConsOS (treeA-x) NilOS)) (treeA-x)) -! (bind! test1-exemplars - (ConsOS (mkExemplar treeA (mkDemeId "1") (mkCscore 0.8 0.3 0.1 0.01 0.4) (mkBscore (Cons 1 (Cons 1 Nil)))) - (ConsOS (mkExemplar tree2 (mkDemeId "2") (mkCscore 0.7 0.4 0.05 0.05 0.5) (mkBscore (Cons 0 (Cons 1 Nil)))) - (ConsOS (mkExemplar tree3 (mkDemeId "3") (mkCscore 0.6 0.5 0.0 0.0 0.6) (mkBscore (Cons 1 (Cons 0 Nil)))) +(= (test1-exemplars) + (ConsOS (mkExemplar (treeA) (mkDemeId "1") (mkCscore 0.8 0.3 0.1 0.01 0.4) (mkBscore (Cons 1 (Cons 1 Nil)))) + (ConsOS (mkExemplar (tree2) (mkDemeId "2") (mkCscore 0.7 0.4 0.05 0.05 0.5) (mkBscore (Cons 0 (Cons 1 Nil)))) + (ConsOS (mkExemplar (tree3) (mkDemeId "3") (mkCscore 0.6 0.5 0.0 0.0 0.6) (mkBscore (Cons 1 (Cons 0 Nil)))) NilOS)))) -! (assertEqual (let $choice (selectExemplar test1-exemplars) (OS.contains test1-exemplars $choice)) True) +!(assertEqual (let $choice (selectExemplar (test1-exemplars)) (OS.contains (test1-exemplars) $choice)) True) -! (bind! test2-exemplars - (ConsOS (mkExemplar treeA (mkDemeId "101") (mkCscore 0.9 0.3 0.05 0.02 0.6) +(= (test2-exemplars) + (ConsOS (mkExemplar (treeA) (mkDemeId "101") (mkCscore 0.9 0.3 0.05 0.02 0.6) (mkBscore (Cons 1 (Cons 1 (Cons 1 (Cons 1 Nil)))))) - (ConsOS (mkExemplar treeB (mkDemeId "102") (mkCscore 0.6 0.5 0.1 0.1 0.5) (mkBscore (Cons 1 (Cons 1 (Cons 1 (Cons 1 Nil)))))) - (ConsOS (mkExemplar treeC (mkDemeId "103") (mkCscore 0.4 0.4 0.05 0.1 0.4) (mkBscore (Cons 0 (Cons 0 (Cons 1 (Cons 0 Nil)))))) + (ConsOS (mkExemplar (treeB) (mkDemeId "102") (mkCscore 0.6 0.5 0.1 0.1 0.5) (mkBscore (Cons 1 (Cons 1 (Cons 1 (Cons 1 Nil)))))) + (ConsOS (mkExemplar (treeC) (mkDemeId "103") (mkCscore 0.4 0.4 0.05 0.1 0.4) (mkBscore (Cons 0 (Cons 0 (Cons 1 (Cons 0 Nil)))))) NilOS)))) -! (assertEqual (let $choice (selectExemplar test2-exemplars) (OS.contains test2-exemplars $choice)) True) +!(assertEqual (let $choice (selectExemplar (test2-exemplars)) (OS.contains (test2-exemplars) $choice)) True) -! (bind! metaPop (ConsOS (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.2 1 0.1 0.1 5) (mkBScore (Cons 0 (Cons 0 Nil)))) +(= (metaPop) (ConsOS (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.2 1 0.1 0.1 5) (mkBScore (Cons 0 (Cons 0 Nil)))) (ConsOS (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil)) (mkDemeId "1") (mkCscore -0.1 2 0.2 0.3 -0.6) (mkBScore (Cons 0 (Cons 0 Nil)))) (ConsOS (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil)) (mkDemeId "1") (mkCscore -0.1 2 0.2 0.3 -0.006) (mkBScore (Cons 0 (Cons 0 Nil)))) NilOS)))) ; ! (assertEqual (let $choice (selectExemplar metaPop) ((get-type $choice) (OS.contains metaPop $choice))) ((Exemplar Bool) True)) -! (assertEqual (let $choice (selectExemplar metaPop) (OS.contains metaPop $choice)) True) +!(assertEqual (let $choice (selectExemplar (metaPop)) (OS.contains (metaPop) $choice)) True) diff --git a/metapopulation/tests/exemplar-type-test.metta b/metapopulation/tests/exemplar-type-test.metta new file mode 100644 index 00000000..123b11ba --- /dev/null +++ b/metapopulation/tests/exemplar-type-test.metta @@ -0,0 +1,38 @@ +; ! (register-module! ../../../metta-moses) +!(import! &self ../../../metta-moses/utilities/ordered-set) +!(import! &self ../../../metta-moses/metapopulation/exemplar-type) + +;; Exemplar initialization +!(test (xmplrInit (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode B) Nil) Nil))) (mkDemeId 1)) + (mkXmplr + (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode B) Nil) Nil))) + (mkDemeId 1) + (mkCscore + (mkScoreT (pow-math 10 -37)) + (mkComplexity 0) + (mkScoreT 0) + (mkScoreT (pow-math 10 -37))) + (mkBscore Nil))) +!(test + (xmplrInit (mkTree (mkTree (mkNode A) Nil)) (mkDemeId 2)) + (mkXmplr + (mkTree (mkTree (mkNode A) Nil)) + (mkDemeId 2) + (mkCscore + (mkScoreT (pow-math 10 -37)) + (mkComplexity 0) + (mkScoreT 0) + (mkScoreT (pow-math 10 -37))) + (mkBscore Nil))) + +!(test + (xmplrInit (mkTree (mkNode OR) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode B) Nil) Nil))) (mkDemeId 100)) + (mkXmplr + (mkTree (mkNode OR) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode B) Nil) Nil))) + (mkDemeId 100) + (mkCscore + (mkScoreT (pow-math 10 -37)) + (mkComplexity 0) + (mkScoreT 0) + (mkScoreT (pow-math 10 -37))) + (mkBscore Nil))) diff --git a/metapopulation/tests/exemplar-type-testold.metta b/metapopulation/tests/exemplar-type-testold.metta index f0aee507..123b11ba 100644 --- a/metapopulation/tests/exemplar-type-testold.metta +++ b/metapopulation/tests/exemplar-type-testold.metta @@ -1,9 +1,9 @@ -! (register-module! ../../../metta-moses) -; ! (import! &self metta-moses:utilities:ordered-set) -! (import! &self metta-moses:metapopulation:exemplar-type) +; ! (register-module! ../../../metta-moses) +!(import! &self ../../../metta-moses/utilities/ordered-set) +!(import! &self ../../../metta-moses/metapopulation/exemplar-type) ;; Exemplar initialization -! (assertEqual (xmplrInit (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode B) Nil) Nil))) (mkDemeId 1)) +!(test (xmplrInit (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode B) Nil) Nil))) (mkDemeId 1)) (mkXmplr (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode B) Nil) Nil))) (mkDemeId 1) @@ -13,7 +13,7 @@ (mkScoreT 0) (mkScoreT (pow-math 10 -37))) (mkBscore Nil))) -! (assertEqual +!(test (xmplrInit (mkTree (mkTree (mkNode A) Nil)) (mkDemeId 2)) (mkXmplr (mkTree (mkTree (mkNode A) Nil)) @@ -25,7 +25,7 @@ (mkScoreT (pow-math 10 -37))) (mkBscore Nil))) -! (assertEqual +!(test (xmplrInit (mkTree (mkNode OR) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode B) Nil) Nil))) (mkDemeId 100)) (mkXmplr (mkTree (mkNode OR) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode B) Nil) Nil))) diff --git a/metapopulation/tests/metapopulation-testold.metta b/metapopulation/tests/metapopulation-test.metta similarity index 61% rename from metapopulation/tests/metapopulation-testold.metta rename to metapopulation/tests/metapopulation-test.metta index efd44598..856b65bc 100644 --- a/metapopulation/tests/metapopulation-testold.metta +++ b/metapopulation/tests/metapopulation-test.metta @@ -1,16 +1,16 @@ -! (register-module! ../../../metta-moses) -! (import! &self metta-moses:metapopulation:metapopulation) -! (import! &self metta-moses:scoring:cscore) -! (import! &self metta-moses:scoring:fitness) -! (import! &self metta-moses:scoring:bscore) -! (import! &self metta-moses:utilities:ordered-set) -! (import! &self metta-moses:utilities:tree) -! (import! &self metta-moses:utilities:general-helpers) -! (import! &self metta-moses:utilities:list-methods) -! (import! &self metta-moses:deme:deme-id-creation) +; ! (register-module! ../../../metta-moses) +!(import! &self ../../../metta-moses/metapopulation/metapopulation) +!(import! &self ../../../metta-moses/scoring/cscore) +!(import! &self ../../../metta-moses/scoring/fitness) +!(import! &self ../../../metta-moses/scoring/bscore) +!(import! &self ../../../metta-moses/utilities/ordered-set) +!(import! &self ../../../metta-moses/utilities/tree) +!(import! &self ../../../metta-moses/utilities/general-helpers) +!(import! &self ../../../metta-moses/utilities/list-methods) +!(import! &self ../../../metta-moses/deme/deme-id-creation) ;; Test cases for getBetterCandidates -! (assertEqual (getBetterCandidates (ConsOS +!(assertEqual (getBetterCandidates (ConsOS (mkExemplar (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (mkDemeId "1") @@ -18,7 +18,7 @@ (mkBScore (Cons 0.5 Nil))) NilOS) -0.2) NilOS) -! (assertEqual +!(assertEqual (getBetterCandidates (ConsOS (mkExemplar @@ -36,7 +36,7 @@ (mkBScore (Cons 0.5 Nil))) NilOS)) -! (bind! pop1 (ConsOS +(= (pop1) (ConsOS (mkExemplar (mkTree (mkNode XOR) (Cons (mkTree (mkNode X) Nil) (Cons (mkTree (mkNode Y) Nil) Nil))) (mkDemeId "1") @@ -56,7 +56,7 @@ (mkBScore (Cons 0.4 Nil))) NilOS)))) -! (assertEqual (getBetterCandidates pop1 -0.5) +!(assertEqual (getBetterCandidates (pop1) -0.5) (ConsOS (mkExemplar (mkTree (mkNode XOR) (Cons (mkTree (mkNode X) Nil) (Cons (mkTree (mkNode Y) Nil) Nil))) @@ -70,7 +70,7 @@ (mkCscore -0.2 1 0.1 0.1 -0.4) (mkBScore (Cons 0.3 Nil))) NilOS))) -! (bind! pop2 (ConsOS (mkExemplar +(= (pop2) (ConsOS (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode B) Nil) Nil))) (mkDemeId "1") (mkCscore -0.1 2 0.1 0.1 -0.3) @@ -101,8 +101,8 @@ (mkCscore -0.9 3 0.1 0.1 -1.1) (mkBScore (Cons 0.4 Nil))) NilOS))))))) -! (assertEqual - (getBetterCandidates pop2 -0.75) +!(assertEqual + (getBetterCandidates (pop2) -0.75) (ConsOS (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode B) Nil) Nil))) (mkDemeId "1") @@ -126,7 +126,7 @@ NilOS))))) ;; Test cases for cullAtRandom -! (bind! pop3 (ConsOS +(= (pop3) (ConsOS (mkExemplar (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode B) Nil) Nil))) (mkDemeId "1") @@ -145,9 +145,9 @@ (mkCscore -0.3 3 0.1 0.1 -0.5) (mkBScore (Cons 0.3 Nil))) NilOS)))) -! (assertEqual (let $a (cullAtRandom pop3 1 2) (OS.length $a)) 1) +!(assertEqual (let $a (cullAtRandom (pop3) 1 2) (OS.length $a)) 1) -! (bind! pop4 (ConsOS +(= (pop4) (ConsOS (mkExemplar (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode B) Nil) Nil))) (mkDemeId "1") @@ -179,10 +179,10 @@ (mkBScore (Cons 0.1 Nil))) NilOS)))))) -! (assertEqual (let $result (cullAtRandom pop4 2 2) (OS.length $result)) 3) +!(assertEqual (let $result (cullAtRandom (pop4) 2 2) (OS.length $result)) 3) ;; Test cases for resizeMetapop -! (bind! pop5 +(= (pop5) (ConsOS (mkExemplar (mkTree (mkNode AND) (Cons (mkTree (mkNode A) Nil) (Cons (mkTree (mkNode B) Nil) Nil))) @@ -240,49 +240,49 @@ (mkBScore (Cons 0.6 Nil))) NilOS))))))))) -! (assertEqual (let $a (resizeMetapop pop5 3 5 3 0.004 1000) (OS.length $a)) 6) -! (assertEqual (let $a (resizeMetapop pop5 3 5 2 0.003 1000) (OS.length $a)) 4) +!(assertEqual (let $a (resizeMetapop (pop5) 3 5 3 0.004 1000) (OS.length $a)) 6) +!(assertEqual (let $a (resizeMetapop (pop5) 3 5 2 0.003 1000) (OS.length $a)) 4) -; ;; Testcases for compareExemplar +;; Testcases for compareExemplar !(assertEqual -(compareExemplar -(mkExemplar (mkTree (mkNode AND) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.1 2 0.1 0.1 -0.3) (mkBScore (Cons 0 (Cons 0 Nil)))) -(mkExemplar (mkTree (mkNode AND) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.2 3 0.1 0.1 -0.4) (mkBScore (Cons 0 (Cons 0 Nil)))) -)G) + (compareExemplar + (mkExemplar (mkTree (mkNode AND) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.1 2 0.1 0.1 -0.3) (mkBScore (Cons 0 (Cons 0 Nil)))) + (mkExemplar (mkTree (mkNode AND) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.2 3 0.1 0.1 -0.4) (mkBScore (Cons 0 (Cons 0 Nil)))) + ) G) !(assertEqual -(compareExemplar -(mkExemplar (mkTree (mkNode AND) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.5 3 0.1 0.1 -0.7) (mkBScore (Cons 0 (Cons 0 Nil)))) -(mkExemplar (mkTree (mkNode AND) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.5 2 0.1 0.1 -0.7) (mkBScore (Cons 0 (Cons 0 Nil)))) -) L) + (compareExemplar + (mkExemplar (mkTree (mkNode AND) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.5 3 0.1 0.1 -0.7) (mkBScore (Cons 0 (Cons 0 Nil)))) + (mkExemplar (mkTree (mkNode AND) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.5 2 0.1 0.1 -0.7) (mkBScore (Cons 0 (Cons 0 Nil)))) + ) L) !(assertEqual -(compareExemplar -(mkExemplar (mkTree (mkNode AND) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.5 3 0.1 0.1 -0.7) (mkBScore (Cons 0 (Cons 0 Nil)))) -(mkExemplar (mkTree (mkNode AND) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.5 3 0.1 0.1 -0.7) (mkBScore (Cons 0 (Cons 0 Nil)))) -) E) + (compareExemplar + (mkExemplar (mkTree (mkNode AND) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.5 3 0.1 0.1 -0.7) (mkBScore (Cons 0 (Cons 0 Nil)))) + (mkExemplar (mkTree (mkNode AND) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.5 3 0.1 0.1 -0.7) (mkBScore (Cons 0 (Cons 0 Nil)))) + ) E) ;; Test cases for getExemplarBScore !(assertEqual -(getExemplarBScore (mkExemplar (mkTree (mkNode AND) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.5 3 0.1 0.1 -0.7) (mkBScore (Cons 0 (Cons 0 Nil))))) -(mkBScore (Cons 0 (Cons 0 Nil)))) + (getExemplarBScore (mkExemplar (mkTree (mkNode AND) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.5 3 0.1 0.1 -0.7) (mkBScore (Cons 0 (Cons 0 Nil))))) + (mkBScore (Cons 0 (Cons 0 Nil)))) !(assertEqual -(getExemplarBScore (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.5 3 0.1 0.1 -0.7) (mkBScore (Cons 0 (Cons 0 Nil))))) -(mkBScore (Cons 0 (Cons 0 Nil)))) + (getExemplarBScore (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.5 3 0.1 0.1 -0.7) (mkBScore (Cons 0 (Cons 0 Nil))))) + (mkBScore (Cons 0 (Cons 0 Nil)))) ;; Test cases for getExemplarPenScore !(assertEqual -(getExemplarPenScore (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.5 3 0.1 0.1 -0.7) (mkBScore (Cons 0 (Cons 0 Nil))))) --0.7) + (getExemplarPenScore (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.5 3 0.1 0.1 -0.7) (mkBScore (Cons 0 (Cons 0 Nil))))) + -0.7) ;; Test cases for getExemplarCscore !(assertEqual -(getExemplarCscore (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.5 3 0.1 0.1 -0.7) (mkBScore (Cons 0 (Cons 0 Nil))))) -(mkCscore -0.5 3 0.1 0.1 -0.7)) + (getExemplarCscore (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.5 3 0.1 0.1 -0.7) (mkBScore (Cons 0 (Cons 0 Nil))))) + (mkCscore -0.5 3 0.1 0.1 -0.7)) ;; Test cases for getExemplarTree !(assertEqual -(getExemplarTree (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.5 3 0.1 0.1 -0.7) (mkBScore (Cons 0 (Cons 0 Nil))))) -(mkTree (mkNode OR) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil)))) + (getExemplarTree (mkExemplar (mkTree (mkNode OR) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil))) (mkDemeId "1") (mkCscore -0.5 3 0.1 0.1 -0.7) (mkBScore (Cons 0 (Cons 0 Nil))))) + (mkTree (mkNode OR) (Cons (mkTree (mkNode NOT) (Cons (mkTree (mkNode A) Nil) Nil)) (Cons (mkTree (mkNode OR) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode B) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode C) Nil) Nil)) (Cons (mkTree (mkNode AND) (Cons (mkTree (mkNode D) Nil) Nil)) Nil)))) Nil)))) diff --git a/scoring/cscore.metta b/scoring/cscore.metta index 4ebc060a..58619a98 100644 --- a/scoring/cscore.metta +++ b/scoring/cscore.metta @@ -84,7 +84,7 @@ (mkCscore (* -1 (pow-math 10 308)) 0 0.0 0.0 (* -1 (pow-math 10 308)))) ;; score getters -(: getPenScore (-> Cscore Number)) +; (: getPenScore (-> Cscore Number)) (= (getPenScore (mkCscore $scor $cpxy $complexityPenalty $uniformityPenalty $penalizedScore)) $penalizedScore) (: getScore (-> Cscore Number)) diff --git a/scoring/fitness.metta b/scoring/fitness.metta index 059c013d..e744e808 100644 --- a/scoring/fitness.metta +++ b/scoring/fitness.metta @@ -1,5 +1,5 @@ -! (bind! &space (new-space)) +!(bind! &space (new-space)) (= (removeFromSpace $row $space) (remove-atom $space $row)) diff --git a/utilities/general-helpers.metta b/utilities/general-helpers.metta index 12cdae9a..db5a3ae4 100644 --- a/utilities/general-helpers.metta +++ b/utilities/general-helpers.metta @@ -648,17 +648,18 @@ (: >>= (-> $a $a Bool)) (= (>>= ($ctor $x) ($ctor $y))(>= $x $y));; List.Sum for any (List $a) $a of (typeConstructor Number) type ;; -(: List.sum (-> (-> $a $a $a) (List $a) $a)) -(= (List.sum $adder Nil) Nil) -(= (List.sum $adder (Cons $x $xs)) - (if (== $xs Nil) - $x - (let $c (List.sum $adder $xs) - ($adder $x $c)))) - -; ;; overloading the above function to work with list of numbers -; (: List.sum (-> (List $a) $a)) -(= (List.sum $list) (List.sum + $list)) +;; the following is defined in list-methods.metta as well +; (: List.sum (-> (-> $a $a $a) (List $a) $a)) +; (= (List.sum $adder Nil) Nil) +; (= (List.sum $adder (Cons $x $xs)) +; (if (== $xs Nil) +; $x +; (let $c (List.sum $adder $xs) +; ($adder $x $c)))) + +; ; ;; overloading the above function to work with list of numbers +; ; (: List.sum (-> (List $a) $a)) +; (= (List.sum $list) (List.sum + $list)) ;; A function to take any two types which have the same constructor and add them. ;; params: ($ctor $a): The first argument with a constructor $ctor @@ -750,7 +751,7 @@ ;; approximate equality -- takes the relative magnitude of the numbers into consideration !(bind! EPSILON (new-state (pow-math 10 -6))) ;in petta to bind some constant like this we need to do it by new-state function and to retrieve get-state function -(: isApproxEq (-> Number Number Bool)) +; (: isApproxEq (-> Number Number Bool)) (= (isApproxEq $x $y) (let $diff (abs-math (- $x $y)) (if (< $diff (get-state EPSILON)) @@ -827,13 +828,13 @@ ;; - Cscore -> KPair(penScore, -complexity) with NaN mapped to low sentinel ;; - ScoredInstance/Exemplar variants -> delegate to inner Cscore ;; - Default -> KScalar(x) (works for Number and other simple types) -(: key-of (-> $a $k)) ;; NOTE: Renamed from 'key' to 'key-of' for PeTTa compatibility. builtin function naming conflict with internal compilation in prolog +; (: key-of (-> $a $k)) ;; NOTE: Renamed from 'key' to 'key-of' for PeTTa compatibility. builtin function naming conflict with internal compilation in prolog (= (key-of $x) (case $x ( ;; 5-field Cscore (scoring/cscore.metta) ((mkCscore $scor $cpxy $cpxyPen $uniPen $penScore) - (let $p (if (isnan $penScore) (* -1 (pow-math 10 308)) $penScore) + (let $p (if (isnan-math $penScore) (* -1 (pow-math 10 308)) $penScore) (KPair $p (* -1 $cpxy)))) ((mkSInst (mkPair $inst $score)) (key-of $score)) @@ -1215,9 +1216,15 @@ (add-atom $space (= $head $bodyreduced)))) ;Error Handling +; (= (if-error $X $A $B) +; (if (== (car-atom $X) Error) +; $A $B)) + (= (if-error $X $A $B) - (if (== (car-atom $X) Error) - $A $B)) + (if (or (== (get-metatype $X) Grounded) (== (get-metatype $X) Symbol)) + $B + (if (== (car-atom $X) Error) + $A $B))) (= (return-on-error $result $B) (if-error $result diff --git a/utilities/list-methods.metta b/utilities/list-methods.metta index d2f39ae6..541d9d58 100644 --- a/utilities/list-methods.metta +++ b/utilities/list-methods.metta @@ -8,12 +8,12 @@ ;; Fold a tuple from left to right -(: List.foldl (-> (-> $a $b $b) $b (List $a) $b)) +; (: List.foldl (-> (-> $a $b $b) $b (List $a) $b)) (= (List.foldl $f $i Nil) $i) (= (List.foldl $f $i (Cons $h $t)) (chain ($f $h $i) $i' (List.foldl $f $i' $t))) ;; Fold a tuple from right to left -(: List.foldr (-> (-> $a $b $b) $b (List $a) $b)) +; (: List.foldr (-> (-> $a $b $b) $b (List $a) $b)) (= (List.foldr $f $i Nil) $i) (= (List.foldr $f $i (Cons $h $t)) ($f $h (List.foldr $f $i $t))) @@ -29,21 +29,21 @@ (= (List.tFoldr (Cons $x $xs) $f $t $acc) (let $new-acc (List.tFoldr $xs $f $t $acc) (let $tr ($t $x) ($f $tr $new-acc)))) ;; Define List.sum -(: List.sum (-> (List Number) Number)) -(= (List.sum $xs) (List.foldr + 0 $xs)) +; (: List.sum (-> (List Number) Number)) +(= (List.sum $xs) (eval (List.foldr + 0 $xs))) -(: List.append (-> $a (List $a) (List $a))) +; (: List.append (-> $a (List $a) (List $a))) (= (List.append $val Nil) (Cons $val Nil)) (= (List.append $val (Cons $head $tail)) (Cons $head (List.append $val $tail))) ;; Get an element by index from a list -(: List.getByIdx (-> (List $a) Number $a)) +; (: List.getByIdx (-> (List $a) Number $a)) (= (List.getByIdx Nil $idx) (Error Nil (Index out of range))) (= (List.getByIdx (Cons $head $tail) $idx) (if (== $idx 0 ) $head (List.getByIdx $tail (- $idx 1))) ) ;; Insert an element to a presumably sorted list, remains sorted. -(: List.insert (-> $a (List $a) (List $a))) +; (: List.insert (-> $a (List $a) (List $a))) (= (List.insert $x Nil) (Cons $x Nil)) (= (List.insert $x (Cons $head $tail)) (if (< $x $head) @@ -57,27 +57,27 @@ ;; Default to sort in asecnding order (= (List.sort (Cons $head $tail)) (List.sort (Cons $head $tail) <)) -;;sort using custome oparator +;;sort using custom oparator (= (List.sort (Cons $head $tail) $op) (let* (($expr (List.listToExpr (Cons $head $tail))) ($n (size-atom $expr)) - ($sortedExpr (selectionSort $expr $n $op)) - ($sortedList (exprToList $sortedExpr))) + ($sortedExpr (eval (selectionSort $expr $n $op))) + ($sortedList (eval (exprToList $sortedExpr)))) $sortedList) ) ;; helper function to find the length of the list -(: List.length (-> (List $a) Number)) +; (: List.length (-> (List $a) Number)) (= (List.length Nil) 0) (= (List.length (Cons $head $tail)) (+ 1 (List.length $tail))) ;; Map a function over a list -(: List.map (-> (-> $a $b) (List $a) (List $b))) +; (: List.map (-> (-> $a $b) (List $a) (List $b))) (= (List.map $f Nil) Nil) (= (List.map $f (Cons $x $xs)) (Cons ($f $x) (List.map $f $xs))) ;; Map a function that takes an additional arg -(: List.mapOverArg (-> (-> $a $b $a) $b $arg (List $a))) +; (: List.mapOverArg (-> (-> $a $b $a) $b $arg (List $a))) (= (List.mapOverArg $f Nil $arg) Nil) (= (List.mapOverArg $f (Cons $head $tail) $arg) (let $new-head ($f $head $arg) @@ -86,17 +86,17 @@ (Cons $new-head (List.mapOverArg $f $tail $arg))))) ;; Filter a list based on a predicate. -(: List.filter (-> (-> $a Bool) (List $a) (List $a))) +; (: List.filter (-> (-> $a Bool) (List $a) (List $a))) (= (List.filter $p Nil) Nil) (= (List.filter $p (Cons $x $xs)) (if ($p $x) (Cons $x (List.filter $p $xs)) (List.filter $p $xs))) ;; Convert a list to an expression. -(: List.listToExpr (-> (List $a) Expression)) +; (: List.listToExpr (-> (List $a) Expression)) (= (List.listToExpr Nil) ()) (= (List.listToExpr (Cons $x $xs)) (let $t (List.listToExpr $xs) (cons-atom $x $t))) ;; defining List.max function with a comparator that compares non-numerical type values -(: List.max (-> (-> $a $a Bool) (List $a) $a)) +; (: List.max (-> (-> $a $a Bool) (List $a) $a)) (= (List.max $comparator Nil) Nil) (= (List.max $comparator (Cons $x $xs)) (if (== $xs Nil) @@ -107,22 +107,22 @@ (List.max $comparator $xs))))) ;; Overloading the above List.max with the built in >= comparison operator for operation on List of numbers -(: List.max (-> (List $a) $a)) +; (: List.max (-> (List $a) $a)) (= (List.max (Cons $x $xs)) (List.max >= (Cons $x $xs))) ;; Checks if an element is member of a list -(: List.contains (-> $a (List $a) Bool)) +; (: List.contains (-> $a (List $a) Bool)) (= (List.contains $a Nil) False) (= (List.contains $a (Cons $head $tail)) (if (== $a $head) True (List.contains $a $tail))) ; Replaces an element at a specific index with another element -(: List.replaceAt (-> (List $a) Number $a (List $a))) +; (: List.replaceAt (-> (List $a) Number $a (List $a))) (= (List.replaceAt Nil $n $elem) Nil) (= (List.replaceAt (Cons $head $tail) $n $elem) (if (== $n 0) (Cons $elem $tail) (Cons $head (List.replaceAt $tail (- $n 1) $elem)))) ;; List.prepend .. adds an element at the beginnig of a list -(: List.prepend (-> $a (List $a) (List $a))) +; (: List.prepend (-> $a (List $a) (List $a))) (= (List.prepend $a $list) (Cons $a $list)) ;; Find an element in a list @@ -132,7 +132,7 @@ ;; Returns: ;; The index of the first found element or ;; -1 If the element can't be found -(: List.index (-> (List $a) $a Number)) +; (: List.index (-> (List $a) $a Number)) (= (List.index Nil $target) -1) (= (List.index (Cons $x $xs) $target) (if (== $x $target) @@ -147,7 +147,7 @@ ;; $list: The list to return from ;; Returns: ;; The first element of the list -(: List.head (-> (List $a) $a)) +; (: List.head (-> (List $a) $a)) (= (List.head Nil) (Error Nil EmptyList)) (= (List.head (Cons $x $xs)) $x) @@ -157,29 +157,30 @@ ;; $list: The list to return from ;; Returns: ;; The rest of the list -(: List.tail (-> (List $a) (List $a))) +; (: List.tail (-> (List $a) (List $a))) (= (List.tail Nil) Nil) (= (List.tail (Cons $x $xs)) $xs) ;; Subtract list element wise -(: List.sub (-> (List Number) (List Number) (List Number))) +; (: List.sub (-> (List Number) (List Number) (List Number))) (= (List.sub Nil Nil) Nil) (= (List.sub Nil (Cons $y $ys)) (Error (Cons $y $ys) LenghtOfListNotEqual)) (= (List.sub (Cons $x $xs) Nil) (Error (Cons $x $xs) LenghtOfListsNotEqual)) (= (List.sub (Cons $x $xs) (Cons $y $ys)) - (chain (List.sub $xs $ys) $res (if-error $res $res (Cons (- $x $y) $res)))) + (let $res (List.sub $xs $ys) (eval (if-error $res $res (Cons (- $x $y) $res))))) -(: List.zip (-> (List $a) (List $b) (List ($a $b)))) +; (: List.zip (-> (List $a) (List $b) (List ($a $b)))) (= (List.zip Nil Nil) Nil) (= (List.zip Nil (Cons $y $ys)) Nil) (= (List.zip (Cons $x $xs) Nil) Nil) (= (List.zip (Cons $x $xs) (Cons $y $ys)) (Cons ($x $y) (List.zip $xs $ys))) -(: List.zipWith (-> (-> $a $b $c) (List $a) (List $b) (List $c))) +; (: List.zipWith (-> (-> $a $b $c) (List $a) (List $b) (List $c))) (= (List.zipWith $f Nil Nil) Nil) -(= (List.zipWith $f Nil (Cons $y $ys)) Nil) (= (List.zipWith $f (Cons $x $xs) Nil) Nil) (= (List.zipWith $f (Cons $x $xs) (Cons $y $ys)) (Cons ($f $x $y) (List.zipWith $f $xs $ys))) +(= (List.zipWith $f Nil (Cons $y $ys)) Nil) (= (List.zipWith $f (Cons $x $xs) Nil) Nil) +(= (List.zipWith $f (Cons $x $xs) (Cons $y $ys)) (Cons ($f $x $y) (List.zipWith $f $xs $ys))) -(: List.drop (-> Number (List $a) (List $a))) +; (: List.drop (-> Number (List $a) (List $a))) (= (List.drop $n Nil) Nil) (= (List.drop $n (Cons $x $xs)) (if (== $n 0) @@ -188,15 +189,15 @@ ;; Temporary flatten function that works only for list of ;; lists where the second list has only single elements. -(: List.flatten (-> (List (List $a)) (List $a))) +; (: List.flatten (-> (List (List $a)) (List $a))) (= (List.flatten Nil) Nil) (= (List.flatten (Cons $x $xs)) (case $x ( - ((Cons $y Nil) (chain (List.flatten $xs) $res (if-error $res $res (Cons $y $res)))) + ((Cons $y Nil) (chain (List.flatten $xs) $res (eval (if-error $res $res (Cons $y $res))))) ((Cons $y $ys) (Error $x "Can't be flattened"))))) -(: List.repeat (-> Number $a (List $a))) +; (: List.repeat (-> Number $a (List $a))) (= (List.repeat $n $a) (if (== $n 0) Nil @@ -208,7 +209,7 @@ ;; $list2: Second list to concatenate. ;; Returns: ;; (List $a) - New list containing all elements of $list1 followed by $list2. -(: List.concat (-> (List $a) (List $a) (List $a))) +; (: List.concat (-> (List $a) (List $a) (List $a))) (= (List.concat Nil $list2) $list2) (= (List.concat (Cons $head $tail) $list2) (Cons $head (List.concat $tail $list2))) @@ -219,7 +220,7 @@ ;; $idx: Non-negative index of element to remove (0-based). ;; Returns: ;; (List $a) - New list with element at $idx removed, or original list if $idx is invalid. -(: List.removeAtIdx (-> (List $a) Number (List $a))) +; (: List.removeAtIdx (-> (List $a) Number (List $a))) (= (List.removeAtIdx Nil $idx) Nil) (= (List.removeAtIdx (Cons $head $tail) $idx) (if (< $idx 0) (Cons $head $tail) @@ -232,14 +233,14 @@ ;; $list: Input list to search in. ;; Returns: ;; Bool - True if $elem is found in $list, False otherwise. -(: List.isMember (-> $a (List $a) Bool)) +; (: List.isMember (-> $a (List $a) Bool)) (= (List.isMember $elem Nil) False) (= (List.isMember $elem (Cons $head $tail)) (if (== $elem $head) True (List.isMember $elem $tail))) ;; List.partialSort -- sorts the top n values in a list and leaves the rest unsorted -(: List.partialSort (-> (-> $a $a Bool) (List $a) Number (List $a) (List $a))) +; (: List.partialSort (-> (-> $a $a Bool) (List $a) Number (List $a) (List $a))) (= (List.partialSort $comparator (Cons $x $xs) $n $acc) (let* (($max (List.max $comparator (Cons $x $xs))) ($unsortedList (List.delete $max (Cons $x $xs))) @@ -250,18 +251,18 @@ (List.partialSort $comparator $unsortedList (- $n 1) $sortedList)))) ;; Overloading the above partialSort for partial Sorting list of numbers -- decreasing order -(: List.partialSort (-> (List Number) Number (List Number) (List Number))) +; (: List.partialSort (-> (List Number) Number (List Number) (List Number))) (= (List.partialSort $list $n $acc) (List.partialSort >= $list $n $acc)) ;; append a list to a list -(: List.appendList (-> (List $a) (List $a) (List $a))) +; (: List.appendList (-> (List $a) (List $a) (List $a))) (= (List.appendList $a (Cons $x $xs)) (if (== $xs Nil) (Cons $x $a) (Cons $x (List.appendList $a $xs)))) ;; deletes the first occurence of an item in the list -(: (List.delete (-> $a (List $a) (List $a)))) +; (: (List.delete (-> $a (List $a) (List $a)))) (= (List.delete $a Nil) (Error Nil "empty list")) (= (List.delete $a (Cons $x $xs)) (if (== $x $a) @@ -269,7 +270,7 @@ (Cons $x (List.delete $a $xs)))) ;; List.takeN -- takes the first N members of a list -(: List.takeN (-> Number (List $a) (List $a))) +; (: List.takeN (-> Number (List $a) (List $a))) (= (List.takeN $n Nil) Nil) (= (List.takeN $n (Cons $x $xs)) (if (== $n 0) @@ -277,7 +278,7 @@ (let $t (List.takeN (- $n 1) $xs) (Cons $x $t)))) ;; List.takeNFrom -- takes N members starting from given start position position -(: List.takeNFrom (-> Number Number (List $a) (List $a))) +; (: List.takeNFrom (-> Number Number (List $a) (List $a))) (= (List.takeNFrom $start $n Nil) Nil) (= (List.takeNFrom $start $n (Cons $x $xs)) (if (== $n 0) @@ -287,21 +288,20 @@ (List.takeNFrom (- $start 1) $n $xs)))) ;; Returns True if any element in the list is True, otherwise False. -(: List.any (-> (List Bool) Bool)) +; (: List.any (-> (List Bool) Bool)) (= (List.any Nil) False) (= (List.any (Cons True $xs)) True) (= (List.any (Cons False $xs)) (List.any $xs)) ;; Generates a list of a given length containing a specified number repeated. -(: List.generate (-> Number Number (List Number))) +; (: List.generate (-> Number Number (List Number))) (= (List.generate $length $element) (if (> $length 0) (Cons $element (List.generate (- $length 1) $element)) Nil)) ;; remove an element from List -(= (List.pop $i $list) - (unify $list Nil (Error $list EmptyList) - (unify $list (Cons $x $xs) - (if (== $i 0) - $xs - (chain (List.pop (- $i 1) $xs) $res (if-error $res (Cons $x Nil) (Cons $x $res))))))) +(= (List.pop $i Nil) (Error Nil EmptyList)) +(= (List.pop $i (Cons $x $xs)) + (if (== $i 0) + $xs + (chain (List.pop (- $i 1) $xs) $res (eval (if-error $res (Cons $x Nil) (Cons $x $res)))))) \ No newline at end of file diff --git a/utilities/tests/general-helper-functions-test.metta b/utilities/tests/general-helper-functions-test.metta index ce906cfe..7592de97 100644 --- a/utilities/tests/general-helper-functions-test.metta +++ b/utilities/tests/general-helper-functions-test.metta @@ -610,11 +610,11 @@ !(println! ("apply >=")) !(assertEqual (apply >= (mkCscore -2.0 3.0 1.0 0.5 -3.5) - (mkCscore -1.0 2.0 1.0 0.5 1.5NaN)) + (mkCscore -1.0 2.0 1.0 0.5 nan)) True) (println! ("apply <")) !(assertEqual - (apply < (mkCscore -1.0 2.0 1.0 0.5 1.5NaN) + (apply < (mkCscore -1.0 2.0 1.0 0.5 nan) (mkCscore -2.0 3.0 1.0 0.5 -3.5)) True) diff --git a/utilities/tests/list-methods-testold.metta b/utilities/tests/list-methods-test.metta similarity index 81% rename from utilities/tests/list-methods-testold.metta rename to utilities/tests/list-methods-test.metta index 5255cb41..299a3c58 100644 --- a/utilities/tests/list-methods-testold.metta +++ b/utilities/tests/list-methods-test.metta @@ -1,10 +1,10 @@ -!(register-module! ../../../metta-moses) +; !(register-module! ../../../metta-moses) -! (import! &self metta-moses:utilities:list-methods) -! (import! &self metta-moses:utilities:general-helpers) -! (import! &self metta-moses:representation:instance) -!(import! &self metta-moses:scoring:cscore) +!(import! &self ../../../metta-moses/utilities/list-methods) +!(import! &self ../../../metta-moses/utilities/general-helpers) +!(import! &self ../../../metta-moses/representation/instance) +!(import! &self ../../../metta-moses/scoring/cscore) ;; Test cases for List.foldr @@ -19,9 +19,9 @@ !(assertEqual (List.foldl - 0 (Cons 10 (Cons 5 (Cons 2 Nil)))) 7) ;; Test cases for List.sum -!(assertEqual (List.sum (Cons 1 (Cons 2 (Cons 3 (Cons 2 (Cons 3 Nil)))))) 11) -!(assertEqual (List.sum (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil))))) 10) -!(assertEqual (List.sum (Cons 3.0 (Cons 4.0 (Cons 5.0 (Cons 5.0 (Cons 5.0 (Cons 5.0 (Cons 4.0 (Cons 5.0 (Cons 4.0 (Cons 4.0 (Cons 4.0 (Cons 5.0 (Cons 5.0 (Cons 4.0 (Cons 5.0 (Cons 5.0 (Cons 5.0 (Cons 4.0 (Cons 5.0 (Cons 5.0 (Cons 5.0 (Cons 5.0 (Cons 5.0 (Cons 4.0 (Cons 5.0 (Cons 4.0 Nil))))))))))))))))))))))))))) 119) +!(test (List.sum (Cons 1 (Cons 2 (Cons 3 (Cons 2 (Cons 3 Nil)))))) 11) +!(test (List.sum (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil))))) 10) +!(test (List.sum (Cons 3.0 (Cons 4.0 (Cons 5.0 (Cons 5.0 (Cons 5.0 (Cons 5.0 (Cons 4.0 (Cons 5.0 (Cons 4.0 (Cons 4.0 (Cons 4.0 (Cons 5.0 (Cons 5.0 (Cons 4.0 (Cons 5.0 (Cons 5.0 (Cons 5.0 (Cons 4.0 (Cons 5.0 (Cons 5.0 (Cons 5.0 (Cons 5.0 (Cons 5.0 (Cons 4.0 (Cons 5.0 (Cons 4.0 Nil))))))))))))))))))))))))))) 119.0) ;; Test cases for List.append !(assertEqual (List.append 3 (Cons 1 (Cons 2 Nil))) (Cons 1 (Cons 2 (Cons 3 Nil)))) @@ -53,8 +53,8 @@ !(assertEqual (List.map isEven (Cons 1 (Cons 2 (Cons 3 Nil)))) (Cons False (Cons True (Cons False Nil)))) !(assertEqual (List.map isOdd Nil) Nil) -! (assertEqual (List.mapOverArg $f Nil $arg) Nil) -! (assertEqual (List.mapOverArg + (Cons 1 Nil) 1) (Cons 2 Nil)) +!(assertEqual (List.mapOverArg $f Nil $arg) Nil) +!(assertEqual (List.mapOverArg + (Cons 1 Nil) 1) (Cons 2 Nil)) ;; Test cases for List.filter !(assertEqual (List.filter isEven (Cons 1 (Cons 2 (Cons 3 Nil)))) (Cons 2 Nil)) @@ -102,8 +102,8 @@ !(assertEqual (List.tail Nil) Nil) !(assertEqual (List.tail (Cons 1 (Cons 2 Nil))) (Cons 2 Nil)) -;; Test case for List.sub -!(assertEqual (List.sub (Cons 1 (Cons 3 Nil)) (Cons 0 (Cons 3 Nil))) (Cons 1 (Cons 0 Nil))) +; ;; Test case for List.sub +!(test (List.sub (Cons 1 (Cons 3 Nil)) (Cons 0 (Cons 3 Nil))) (Cons 1 (Cons 0 Nil))) !(assertEqual (List.sub (Cons 1 (Cons 3 Nil)) (Cons 0 (Cons 3 (Cons 2 Nil)))) (Error (Cons 2 Nil) LenghtOfListNotEqual)) ;; Test case for List.zip @@ -123,7 +123,7 @@ !(assertEqual (List.drop 1 (Cons A (Cons B Nil))) (Cons B Nil)) !(assertEqual (List.drop 2 (Cons A (Cons B (Cons C Nil)))) (Cons C Nil)) -;; Test case for List.flatten +; ;; Test case for List.flatten !(assertEqual (List.flatten (Cons (Cons 1 Nil) (Cons (Cons 2 Nil) Nil))) (Cons 1 (Cons 2 Nil))) !(assertEqual (List.flatten (Cons (Cons 1 Nil) (Cons (Cons 2 (Cons 3 Nil)) Nil))) (Error (Cons 2 (Cons 3 Nil)) "Can't be flattened")) @@ -146,7 +146,7 @@ !(assertEqual (List.isMember 4 (Cons 1 (Cons 2 (Cons 3 Nil)))) False) ;; List.partialSort -- for list of Scored instances -! (assertEqual (List.partialSort instance>= +!(assertEqual (List.partialSort instance>= (Cons (mkSInst (mkPair (mkInst Nil) (mkCscore 1 2 3 4 15))) (Cons (mkSInst (mkPair (mkInst (Cons 1 Nil)) (mkCscore 1 2 3 4 5))) (Cons (mkSInst (mkPair (mkInst (Cons 1 Nil)) (mkCscore 1 2 3 4 0.5))) Nil))) 2 Nil) @@ -155,7 +155,7 @@ (Cons (mkSInst (mkPair (mkInst (Cons 1 Nil)) (mkCscore 1 2 3 4 5))) (Cons (mkSInst (mkPair (mkInst (Cons 1 Nil)) (mkCscore 1 2 3 4 0.5))) Nil)))) -! (assertEqual (List.partialSort instance>= +!(assertEqual (List.partialSort instance>= (Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -2.0 1 1.0 0.5 -3.5))) (Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -1.0 2 1.0 0.5 -2.5))) (Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -3.0 3 1.0 0.5 -4.5))) Nil)))2 Nil) @@ -164,7 +164,7 @@ (Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -2.0 1 1.0 0.5 -3.5))) (Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -3.0 3 1.0 0.5 -4.5))) Nil)))) -! (assertEqual (List.partialSort instance>= +!(assertEqual (List.partialSort instance>= (Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -2.0 3 1.0 0.5 -3.5))) (Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -2.0 2 1.0 0.5 -3.5))) (Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -2.0 1 1.0 0.5 -3.5))) Nil))) 2 Nil) @@ -173,7 +173,7 @@ (Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -2.0 2 1.0 0.5 -3.5))) (Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -2.0 3 1.0 0.5 -3.5))) Nil)))) -! (assertEqual (List.partialSort instance>= +!(assertEqual (List.partialSort instance>= (Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -3.0 2 1.0 0.5 -4.5))) (Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -1.0 3 1.0 0.5 -2.5))) (Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -2.0 1 1.0 0.5 -3.5))) Nil))) 2 Nil) @@ -182,37 +182,37 @@ (Cons (mkSInst (mkPair (mkInst Nil) (mkCscore -3.0 2 1.0 0.5 -4.5))) Nil)))) ;; For list of numbers -! (assertEqual (List.partialSort (Cons 5 (Cons 1 (Cons 8 (Cons 3 Nil)))) 2 Nil) +!(assertEqual (List.partialSort (Cons 5 (Cons 1 (Cons 8 (Cons 3 Nil)))) 2 Nil) (Cons 8 (Cons 5 (Cons 1 (Cons 3 Nil)))) ) -! (assertEqual (List.partialSort (Cons -4 (Cons -3 (Cons -2 (Cons -1 Nil)))) 2 Nil) +!(assertEqual (List.partialSort (Cons -4 (Cons -3 (Cons -2 (Cons -1 Nil)))) 2 Nil) (Cons -1 (Cons -2 (Cons -4 (Cons -3 Nil))))) -! (assertEqual (List.partialSort (Cons 5 (Cons 1 (Cons 8 (Cons 3 (Cons 1 (Cons 7 Nil)))))) 3 Nil) +!(assertEqual (List.partialSort (Cons 5 (Cons 1 (Cons 8 (Cons 3 (Cons 1 (Cons 7 Nil)))))) 3 Nil) (Cons 8 (Cons 7 (Cons 5 (Cons 1 (Cons 3 (Cons 1 Nil))))))) ;; For List.takeN -! (assertEqual (List.takeN 2 (Cons 2 (Cons 1 (Cons 3 Nil)))) (Cons 2 (Cons 1 Nil))) -! (assertEqual (List.takeN 0 (Cons 2 (Cons 1 (Cons 3 Nil)))) Nil) +!(assertEqual (List.takeN 2 (Cons 2 (Cons 1 (Cons 3 Nil)))) (Cons 2 (Cons 1 Nil))) +!(assertEqual (List.takeN 0 (Cons 2 (Cons 1 (Cons 3 Nil)))) Nil) ;; List.takeNFrom test cases -! (assertEqual (List.takeNFrom 2 3 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 (Cons 5 Nil))))))) +!(assertEqual (List.takeNFrom 2 3 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 (Cons 5 Nil))))))) (Cons 2 (Cons 3 (Cons 4 Nil)))) -! (assertEqual (List.takeNFrom 0 3 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 (Cons 5 Nil))))))) +!(assertEqual (List.takeNFrom 0 3 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 (Cons 5 Nil))))))) (Cons 0 (Cons 1 (Cons 2 Nil)))) -! (assertEqual (List.takeNFrom 3 2 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil)))))) +!(assertEqual (List.takeNFrom 3 2 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil)))))) (Cons 3 (Cons 4 Nil))) -! (assertEqual (List.takeNFrom 4 3 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil)))))) +!(assertEqual (List.takeNFrom 4 3 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil)))))) (Cons 4 Nil)) -! (assertEqual (List.takeNFrom 10 2 (Cons 0 (Cons 1 (Cons 2 (Cons 3 Nil))))) +!(assertEqual (List.takeNFrom 10 2 (Cons 0 (Cons 1 (Cons 2 (Cons 3 Nil))))) Nil) -! (assertEqual (List.takeNFrom 2 0 (Cons 0 (Cons 1 (Cons 2 (Cons 3 Nil))))) +!(assertEqual (List.takeNFrom 2 0 (Cons 0 (Cons 1 (Cons 2 (Cons 3 Nil))))) Nil) -! (assertEqual (List.takeNFrom 0 2 Nil) +!(assertEqual (List.takeNFrom 0 2 Nil) Nil) -! (assertEqual (List.takeNFrom 4 1 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil)))))) +!(assertEqual (List.takeNFrom 4 1 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil)))))) (Cons 4 Nil)) -! (assertEqual (List.takeNFrom 2 10 (Cons 0 (Cons 1 (Cons 2 (Cons 3 Nil))))) +!(assertEqual (List.takeNFrom 2 10 (Cons 0 (Cons 1 (Cons 2 (Cons 3 Nil))))) (Cons 2 (Cons 3 Nil))) -! (assertEqual (List.takeNFrom 2 10 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil)))))) +!(assertEqual (List.takeNFrom 2 10 (Cons 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil)))))) (Cons 2 (Cons 3 (Cons 4 Nil)))) ;; List.generate test cases diff --git a/utilities/tree.metta b/utilities/tree.metta index 92cda36f..19387659 100644 --- a/utilities/tree.metta +++ b/utilities/tree.metta @@ -1,26 +1,26 @@ -(: Tree (-> $a Type)) -(: mkTree (-> (Node $a) (List (Tree $a)) (Tree $a))) -(: Node (-> $a Type)) -(: mkNode (-> $a (Node $a))) +; (: Tree (-> $a Type)) +; (: mkTree (-> (Node $a) (List (Tree $a)) (Tree $a))) +; (: Node (-> $a Type)) +; (: mkNode (-> $a (Node $a))) -(: NullVertex (Tree $a)) -(: mkNullVex (-> (List (Tree $a)) (Tree $a))) +; (: NullVertex (Tree $a)) +; (: mkNullVex (-> (List (Tree $a)) (Tree $a))) -(: getNodeValue (-> (Tree $a) (Node $a))) +; (: getNodeValue (-> (Tree $a) (Node $a))) (= (getNodeValue (mkNullVex $xs)) (Error (mkNullVex $xs) "Null Vertex has no value")) (= (getNodeValue (mkTree $nodeValue $chl)) $nodeValue) -(: getChildren (-> (Tree $a) (List (Tree $a)))) +; (: getChildren (-> (Tree $a) (List (Tree $a)))) (= (getChildren (mkTree (mkNode $r) $children)) $children) (= (getChildren (mkNullVex $children)) $children) -(: updateChildren (-> (Tree $a) (List (Tree $a)) (Tree $a))) +; (: updateChildren (-> (Tree $a) (List (Tree $a)) (Tree $a))) (= (updateChildren (mkTree (mkNode $r) $oldChildren) $newChildren) (mkTree (mkNode $r) $newChildren)) (= (updateChildren (mkNullVex $oldChildren) $newChildren) (mkNullVex $newChildren)) -(: preOrder (-> (Tree $a) $a)) +; (: preOrder (-> (Tree $a) $a)) (= (preOrder (mkTree (mkNode $r) Nil)) $r) (= (preOrder (mkNullVex $knobs)) ()) (= (preOrder (mkTree (mkNode $r) (Cons $x $xs))) @@ -35,7 +35,7 @@ $exp) ) -(: buildTree (-> $a (Tree $a))) +; (: buildTree (-> $a (Tree $a))) (= (buildTree $expr) (if (== (get-metatype $expr) Expression) ; (let ($head $tail) (decons-atom $expr) @@ -55,7 +55,7 @@ ) ) -(: cleanTree (-> (Tree $a) (Tree $a))) +; (: cleanTree (-> (Tree $a) (Tree $a))) (= (cleanTree $tree) (let* ( @@ -75,9 +75,9 @@ ;; $targetTree: The target tree to find the ID of. ;; Returns: ;; NodeId: The id of the targetTree if found. -(: getNodeId (-> (Tree $a) (Tree $a) NodeId)) -(: getNodeId (-> (Tree $a) (Tree $a) NodeId NodeId)) -(= (getNodeId $tree $targetTree) (getNodeId $tree $targetTree (mkNodeId (0)))) +; (: getNodeId (-> (Tree $a) (Tree $a) NodeId)) +; (: getNodeId (-> (Tree $a) (Tree $a) NodeId NodeId)) +(= (getNodeId $tree $targetTree) (eval (getNodeId $tree $targetTree (mkNodeId (0))))) (= (getNodeId $tree $targetTree (mkNodeId $parentIdx)) (if (== $tree $targetTree) (mkNodeId $parentIdx) @@ -93,7 +93,7 @@ ;; Helper function for the getNodeId function. ;; The function's sole purpose is to make the ;; getNodeById compatible for the foldr function call. -(: applyGetNodeId (-> (Tree $a) ((Tree $a) NodeId NodeId Number) ((Tree $a) NodeId NodeId Number))) +; (: applyGetNodeId (-> (Tree $a) ((Tree $a) NodeId NodeId Number) ((Tree $a) NodeId NodeId Number))) (= (applyGetNodeId $currTree ($targetTree (mkNodeId $parentIdx) $accId $iter)) (if (== $accId (mkNodeId (-1))) (chain (getNodeId $currTree $targetTree (mkNodeId ($iter))) $nodeId @@ -112,7 +112,7 @@ ;; $iter: - index to check children, increments recursively. ;; Returns: ;; NodeId - The NodeId of the Subtree -(: getSubtreeId (-> (Tree $a) NodeId (Tree $a) Number NodeId)) +; (: getSubtreeId (-> (Tree $a) NodeId (Tree $a) Number NodeId)) (= (getSubtreeId $tree (mkNodeId $targetId) $subtree $iter) (let* ( @@ -135,7 +135,7 @@ ;; $id: - ID of the target node. ;; Returns: ;; (List (Tree $a)) - The list of children of the target node. -(: getChildrenById (-> (Tree $a) NodeId (List (Tree $a)))) +; (: getChildrenById (-> (Tree $a) NodeId (List (Tree $a)))) (= (getChildrenById $tree (mkNodeId $id)) (let $targetNode (getNodeById $tree (mkNodeId $id)) (getChildren $targetNode))) @@ -146,7 +146,7 @@ ;; $node: - The new node to become the root, wrapping $tree. ;; Returns: ;; (Tree $a) - A new mkTree with $node as the root and $tree as its only child. -(: insertAbove (-> (Tree $a) (Node $n) (Tree $a))) +; (: insertAbove (-> (Tree $a) (Node $n) (Tree $a))) (= (insertAbove $tree $node) (mkTree $node (Cons $tree Nil)) ) @@ -208,14 +208,14 @@ ($updatedTree (mkNodeId $idOfChild)))) ;; getChildrenByIdx -- retrieve children of a tree using index values -(: getChildrenByIdx (-> (Tree $a) Number (List (Tree $a)))) +; (: getChildrenByIdx (-> (Tree $a) Number (List (Tree $a)))) (= (getChildrenByIdx $tree $idx) (case $tree (((mkTree (mkNode $r) $childrenTgt) (List.getByIdx $childrenTgt $idx)) ((mkNullVex $childrenTgt) (List.getByIdx $childrenTgt $idx)) ($else (Error (Node not found or invalid)))))) -(: insertNodeAtPosition (-> (Tree $a) NodeId (Node $n) (Tree $a))) +; (: insertNodeAtPosition (-> (Tree $a) NodeId (Node $n) (Tree $a))) (= (insertNodeAtPosition $tree (mkNodeId $id) $newNode) (if (or (== $id ()) (== (mkNodeId $id) (mkNodeId (0)))) (mkTree $newNode (Cons $tree Nil)) @@ -225,14 +225,14 @@ ) (replaceNodeById $tree (mkNodeId $id) $newSubtree)))) ;; check if tree is empty -(: isEmpty (-> (Tree $a) Bool)) +; (: isEmpty (-> (Tree $a) Bool)) (= (isEmpty $tree) (case $tree (((mkNullVex Nil) True) ($else False)))) ;; check if tree is null vertex -(: isNullVertex (-> (Tree $a) Bool)) +; (: isNullVertex (-> (Tree $a) Bool)) (= (isNullVertex $tree) (case $tree (((mkNullVex $children) True) @@ -240,7 +240,7 @@ ;; Takes a tree and decides if the node is an argument or not. ;; An argument is anything that's not an operator or a null vertex. -(: isArgument (-> (Tree $a) Bool)) +; (: isArgument (-> (Tree $a) Bool)) (= (isArgument (mkNullVex $x)) False) (= (isArgument (mkTree (mkNode $x) $children)) (and (not (is-member $x (AND OR NOT))) (== $children Nil))) @@ -249,7 +249,7 @@ ;; The complexity of a tree is the number of arguments it contains. ;; That means, ANDs, ORs and NOTs have no complexity. ;; Knobs, or null vetices aren't included in the complexity calculatio. -(: treeComplexity (-> (Tree $a) Number)) +; (: treeComplexity (-> (Tree $a) Number)) (= (treeComplexity (mkNullVex $x)) 0) (= (treeComplexity (mkTree (mkNode $a) $children)) ;; AND, OR and NOT have no complexity (if (isArgument (mkTree (mkNode $a) $children)) @@ -258,12 +258,12 @@ ;; NOTE: for future use -- a function to determine the alphabet size of a given tree for computation of complexity ratio ;; takes a truth table and adds 3 (for AND,. OR and NOT) to the number of input labels -(: alphabetSize (-> (ITable $a) Number)) +; (: alphabetSize (-> (ITable $a) Number)) (= (alphabetSize (mkITable $rows $labels)) (+ 3 (- (List.length $labels) 1))) ;; extract labels from a tree structure as list of symbols -(: extractArgs (-> (Tree $a) (List Symbol))) +; (: extractArgs (-> (Tree $a) (List Symbol))) (= (extractArgs (mkTree (mkNode $node) $children)) (if (is-member $node (AND OR NOT)) (extractArgsFromList $children) ;; if it's an operator, collect args from children @@ -271,7 +271,7 @@ ;; apply the above function recursively accross the tree's children -(: extractArgsFromList (-> (List (Tree $a)) (List Symbol))) +; (: extractArgsFromList (-> (List (Tree $a)) (List Symbol))) (= (extractArgsFromList Nil) Nil) (= (extractArgsFromList (Cons $head $tail)) (List.appendList (extractArgsFromList $tail) (extractArgs $head))) @@ -287,7 +287,7 @@ (= (pruneExemplar $exemplar $arg-set) (foldl-atom $arg-set $exemplar $acc $arg (removeLiteral $acc $arg))) -(: removeLiteral (-> (Tree $a) Symbol (Tree $a))) +; (: removeLiteral (-> (Tree $a) Symbol (Tree $a))) (= (removeLiteral (mkTree (mkNode $node) $children) $symbol) (if (is-member $node (AND OR NOT)) (mkTree (mkNode $node) (List.mapOverArg removeLiteral $children $symbol))