John Smith commited on 2022-337 22:15:41
Showing 1 changed files, with 25 additions and 20 deletions.
* adjustments such as setting time to 4 seconds, #!ing the script
* instead of assuming that the computation takes as input
a list of numbers, the computation may now have a corresponding
problem-type, generated however is reasonable
| ... | ... |
@@ -1,20 +1,27 @@ |
| 1 |
+#!/usr/bin/env lua |
|
| 2 |
+ |
|
| 1 | 3 |
local function identity(x) return x end |
| 2 | 4 |
|
| 5 |
+local function cup(x) |
|
| 6 |
+ if type(x) == "table" then |
|
| 7 |
+ return table.unpack(x) |
|
| 8 |
+ else |
|
| 9 |
+ return x |
|
| 10 |
+ end |
|
| 11 |
+end |
|
| 12 |
+ |
|
| 3 | 13 |
local TYPES = {
|
| 4 | 14 |
ln = {
|
| 5 |
- -- inputs to the operation |
|
| 6 |
- argc = 1, |
|
| 7 |
- -- bounds on inputs |
|
| 8 |
- min = {0.01},
|
|
| 9 |
- max = {1000},
|
|
| 15 |
+ -- how to generate the problem (returns problem-object) |
|
| 16 |
+ gen = function() |
|
| 17 |
+ return math.exp(6.9 * (2 * math.random() - 1)) |
|
| 18 |
+ end, |
|
| 10 | 19 |
-- how to present the problem (for string.format) |
| 11 | 20 |
disp = "ln(%.2f)", |
| 12 |
- -- transform input to output |
|
| 21 |
+ -- produce correct answer-object from problem-object |
|
| 13 | 22 |
func = math.log, |
| 14 |
- -- check guess g to canonical answer c |
|
| 15 |
- good = function(c, g) return math.abs(c - g) < 0.05 end, |
|
| 16 |
- -- transform range [0, 1) to a good distribution |
|
| 17 |
- bias = function(r) return r^2 end, |
|
| 23 |
+ -- check if guess g for problem p is correct |
|
| 24 |
+ good = function(p, g) return math.abs(math.log(p) - g) < 0.05 end, |
|
| 18 | 25 |
}, |
| 19 | 26 |
} |
| 20 | 27 |
|
| ... | ... |
@@ -22,29 +29,27 @@ local TYPES = {
|
| 22 | 29 |
local COUNT = 10 |
| 23 | 30 |
local TYPE = TYPES.ln |
| 24 | 31 |
local TRIES = 1 |
| 25 |
-local TIME = 3 |
|
| 32 |
+local TIME = 4 |
|
| 26 | 33 |
|
| 27 | 34 |
local sc = 0 |
| 28 | 35 |
local oc = 0 |
| 29 | 36 |
for i = 1,COUNT do |
| 30 |
- local rarg = {}
|
|
| 31 |
- for j = 1,TYPE.argc do |
|
| 32 |
- table.insert(rarg, TYPE.min[j] + (TYPE.max[j] - TYPE.min[j]) * (TYPE.bias or identity)(math.random())) |
|
| 33 |
- end |
|
| 34 |
- local ans = TYPE.func(table.unpack(rarg)) |
|
| 35 |
- io.write(string.format(TYPE.disp .. " ? ", table.unpack(rarg))) |
|
| 37 |
+ local rp = TYPE.gen() |
|
| 38 |
+ local ans = TYPE.func(rp) |
|
| 39 |
+ io.write(string.format(TYPE.disp .. " ? ", cup(rp))) |
|
| 36 | 40 |
io.flush() |
| 37 | 41 |
local st = os.time() |
| 38 | 42 |
local inp |
| 39 | 43 |
local fails = 0 |
| 40 |
- while os.time() - st < TIME do |
|
| 44 |
+ while true do |
|
| 41 | 45 |
inp = tonumber(io.read("l")) or 0
|
| 42 | 46 |
if os.time() - st > TIME then |
| 43 |
- if TYPE.good(ans, inp) then |
|
| 47 |
+ if TYPE.good(rp, inp) then |
|
| 44 | 48 |
oc = oc + 1 |
| 45 | 49 |
end |
| 46 | 50 |
io.write(string.format("too slow! (%.2f)\n", ans))
|
| 47 |
- elseif TYPE.good(ans, inp) then |
|
| 51 |
+ break |
|
| 52 |
+ elseif TYPE.good(rp, inp) then |
|
| 48 | 53 |
io.write("✓ correct\n")
|
| 49 | 54 |
sc = sc + 1 |
| 50 | 55 |
break |
| 51 | 56 |