replaced :output with :print

This commit is contained in:
Shuzo Katayama 2021-07-19 16:26:10 -04:00
parent a23b007b0c
commit 372e45895e
5 changed files with 108 additions and 11 deletions

View File

@ -0,0 +1,96 @@
(ns propeller.problems.PSB2.coin-sums
(:require [psb2.core :as psb2]
[propeller.genome :as genome]
[propeller.push.interpreter :as interpreter]
[propeller.utils :as utils]
[propeller.push.utils.helpers :refer [get-stack-instructions]]
[propeller.push.state :as state]
[propeller.tools.math :as math]
[propeller.gp :as gp]
[propeller.push.utils.macros :refer [def-instruction
generate-instructions]]))
; =========== PROBLEM DESCRIPTION ============================
; COIN SUMS from PSB2
; Given a number of cents, find the fewest
; number of US coins (pennies, nickles, dimes, quarters) needed
; to make that amount, and return the number of each type
; of coin as a separate output
;
; Source: https://arxiv.org/pdf/2106.06086.pdf
; ===============================================================
(def train-and-test-data (psb2/fetch-examples "data" "coin-sums" 200 2000))
; Random integer between -100 and 100 (from smallest)
(defn random-int [] (- (rand-int 201) 100))
(def-instruction
:output_pennies
^{:stacks [:integer]}
(fn [state]
(if (empty? (:integer state))
state
(let [top-int (state/peek-stack :integer state)]
))))
;(stack-assoc top-int :output 0 state))))
;(assoc map key val)
(def instructions
(utils/not-lazy
(concat
;;; stack-specific instructions
(get-stack-instructions #{:exec :integer :boolean :print})
;;; input instructions
(list :in1)
;;; close
(list 'close)
;;; ERCs (constants)
(list 0 1 5 10 25))))
(defn error-function
[argmap data individual]
(let [program (genome/plushy->push (:plushy individual) argmap)
inputs (map (fn [i] (get i :input1)) data)
correct-outputs (map (fn [i] (get i :output1)) data)
outputs (map (fn [input]
(state/peek-stack
(interpreter/interpret-program
program
(assoc state/empty-state :input {:in1 input})
(:step-limit argmap))
:integer))
inputs)
errors (map (fn [correct-output output]
(if (= output :no-stack-item)
1000000
(min 1000.0 (math/abs (- correct-output output)))))
correct-outputs
outputs)]
(assoc individual
:behaviors outputs
:errors errors
:total-error #?(:clj (apply +' errors)
:cljs (apply + errors)))))
(defn -main
"Runs propel-gp, giving it a map of arguments."
[& args]
(gp/gp
(merge
{:instructions instructions
:error-function error-function
:training-data (:train train-and-test-data)
:testing-data (:test train-and-test-data)
:max-generations 300
:population-size 1000
:max-initial-plushy-size 250
:step-limit 2000
:parent-selection :lexicase
:tournament-size 5
:umad-rate 0.1
:variation {:umad 1.0 :crossover 0.0}
:elitism false}
(apply hash-map (map #(if (string? %) (read-string %) %) args)))))

View File

@ -71,9 +71,9 @@
program
(assoc state/empty-state :input {:in1 (first input)
:in2 (last input)}
:output '(""))
:print '(""))
(:step-limit argmap))
:output))
:print))
inputs)
parsed-outputs (map (fn [output]
(try (read-string output)

View File

@ -75,9 +75,9 @@
:in2 (get input 1)
:in3 (get input 2)
:in4 (get input 3)}
:output '(""))
:print '(""))
(:step-limit argmap))
:output))
:print))
inputs)
errors (map (fn [correct-output output]
(let [parsed-output (try (read-string output)

View File

@ -31,9 +31,9 @@
:print_newline
^{:stacks [:print]}
(fn [state]
(let [current-output (state/peek-stack state :output)
popped-state (state/pop-stack state :output)]
(state/push-to-stack popped-state :output (str current-output \newline)))))
(let [current-output (state/peek-stack state :print)
popped-state (state/pop-stack state :print)]
(state/push-to-stack popped-state :print (str current-output \newline)))))
(def _print
^{:stacks [:print]
@ -46,10 +46,10 @@
(char? top-item))
top-item
(pr-str top-item))
current-output (state/peek-stack state :output)
popped-state (state/pop-stack (state/pop-stack state stack) :output)]
current-output (state/peek-stack state :print)
popped-state (state/pop-stack (state/pop-stack state stack) :print)]
(state/push-to-stack popped-state
:output
:print
(str current-output top-item-str))))))
(generate-instructions

View File

@ -7,8 +7,9 @@
:exec '()
:float '()
:input {}
:output {}
:integer '()
:output '("")
:print '("")
:string '()
:vector_boolean '()
:vector_float '()