random dilemma stuff, mazes

This commit is contained in:
osmarks 2021-02-18 23:33:56 +00:00
parent 83085b45e5
commit a6fd4bce7f
7 changed files with 471 additions and 2 deletions

View File

@ -76,12 +76,15 @@
(define actually-forgiving-grudge
(lambda (x y) (let* (
(defection-count (length (filter (lambda (m) (= m 1)) x)))
; TODO: tune 1.8 there, or maybe entirely switch out formula
(lookback (+ 1 (inexact->exact (floor (expt 1.8 defection-count)))))
; should maybe be (zip (append x '(-1)) (cons -1 y)) - current version compares actions at the same time, but it may not be "betrayal" unless the defecting by the opponent comes AFTER our cooperating
; this does seem to worsen its score so maybe not
(result (if (member '(1 0) (take lookback (zip x y))) 1 0))
) result)))
(define apiomemetics
(lambda (x y) (random-seed 334278294) ; NOTE TO SELF: 3227883998 (0/1)
(lambda (x y) (random-seed 334278294) ; NOTE TO SELF: 3227883998 (0/2) or 2324865786 (48/50)
(if (null? x)
(begin 0)
(if (> (length x) 93)

BIN
itpd3 Executable file

Binary file not shown.

134
itpd3.scm Normal file
View File

@ -0,0 +1,134 @@
;; pd2.scm - iterated prisoner's dilemma simulation but you know your opponent's strategy
;; by matt
;; this program is in the public domain
(import (chicken random))
(define strategies '())
(define iters 0)
(define add-strategy
(lambda (x y)
(set! strategies (cons (cons x y) strategies))))
(define angel
(lambda (x y z)
0))
(define devil
(lambda (x y z)
1))
(define tit-for-tat
(lambda (x y z)
(if (null? x)
0
(car x))))
(define mean-tit-for-tat
(lambda (x y z)
(if (null? x)
1
(car x))))
(define grudger
(lambda (x y z)
(if (memq 1 x)
1
0)))
(define gollariosity
(lambda (x y z)
(if (= (z y x z) 0) 0 1)))
(define reflector
(lambda (x y z)
(if (eq? z reflector) 0
(z x y z))))
(define alt
(lambda (x y z)
(if (= 0 (remainder (length x) 2)) 0 1)))
(define maybe-tit-for-tat-or-grudger
(lambda (x y z)
(if (= (pseudo-random-integer 2) 1)
(tit-for-tat x y z)
(grudger x y z))))
(define prisond
(lambda (x y)
(if (= x y)
(if (= x 1)
'(1 1)
'(2 2))
(if (= x 1)
'(3 0)
'(0 3)))))
(define metagollariosity
(lambda (x y z)
(define opponent-next-move (z y x z))
(display "about to be gollarious\n")
(write z)
(display "simulating...\n")
(define simulate (lambda (n) (z (cons n y) (cons opponent-next-move x) z)))
(define if-defect (simulate 1))
(display "simulated to depth 1")
(define if-cooperate (simulate 0))
(write if-cooperate)
(if (> (car (prisond 1 if-defect)) (car (prisond 0 if-cooperate))) 1 0)))
(define iter-prisond
(lambda (x y z)
(define scores '(0 0))
(define moves-x '())
(define moves-y '())
(define current-moves '())
(define helper
(lambda (x y z)
(if (= z 0)
scores
(begin
(set! current-moves (list (x moves-x moves-y y) (y moves-y moves-x x)))
(set! moves-x (cons (cadr current-moves) moves-x))
(set! moves-y (cons (car current-moves) moves-y))
(set! scores (map + scores (prisond (car current-moves) (cadr current-moves))))
(helper x y (- z 1)))))) (helper x y z)))
(define get-strategy-scores
(lambda (x)
(define score 0)
(define helper
(lambda (y)
(if (eqv? (car x) (car y))
0
(set! score (+ score (car (iter-prisond (cdr x) (cdr y) (+ 100 iters))))))))
(map helper strategies)
score))
(define get-all-scores
(lambda ()
(define helper
(lambda (x)
(write (list (car x) (get-strategy-scores x)))
(newline)))
(map helper strategies)))
(add-strategy 'angel angel)
(add-strategy 'tit-for-tat tit-for-tat)
(add-strategy 'mean-tit-for-tat mean-tit-for-tat)
(add-strategy 'devil devil)
(add-strategy 'grudger grudger)
(add-strategy 'gollariosity gollariosity)
(add-strategy 'reflector reflector)
(add-strategy 'metagollariosity metagollariosity)
(add-strategy 'alt alt)
(add-strategy 'maybe-tit-for-tat-or-grudger maybe-tit-for-tat-or-grudger)
(set-pseudo-random-seed! (random-bytes))
(set! iters (pseudo-random-integer 50))
(get-all-scores)
(exit)

149
maze.py Executable file
View File

@ -0,0 +1,149 @@
#!/usr/bin/env python3
from z3 import *
def chunks(lst, n):
for i in range(0, len(lst), n):
yield lst[i:i + n]
def main_solver(maze):
MAX_PATH_LENGTH = 80
walls = [ix for ix, cell in enumerate(maze) if cell]
#path_components = [ Int("path" + str(i)) for i in range(MAX_PATH_LENGTH) ]
#path_component_constraints = [ Or(c == -10, c == 10, c == -1, c == 1, c == 0) for c in path_components ]
#print(path_component_constraints)
#print(solve(path_component_constraints))
"""
positions = [Int("pos" + str(i)) for i in range(MAX_PATH_LENGTH)]
pos_constraints = []
solver = Solver()
for ix, pos in enumerate(positions):
if ix == 0:
pos_constraints.append(pos == 0)
else:
last = positions[ix - 1]
pos_constraints.append(Or(pos == (last + 10), pos == (last - 10), If(pos % 10 != 0, pos == (last + 1), False), If(pos % 10 != 9, pos == (last - 1), False)))
pos == (last + 1), pos == (last - 1)))
pos_constraints.append(pos < 100)
pos_constraints.append(pos >= 0)
for cell in walls:
pos_constraints.append(pos != cell)
pos_constraints.append(positions[-1] == 99)
print(pos_constraints)
for c in pos_constraints: constraints.append(c)"""
solver = Solver()
xs = [Int(f"x{i}") for i in range(MAX_PATH_LENGTH)]
ys = [Int(f"y{i}") for i in range(MAX_PATH_LENGTH)]
things = list(zip(xs, ys))
constraints = []
for ix, (x, y) in enumerate(things):
if ix == 0:
constraints.append(x == 0)
constraints.append(y == 0)
else:
last_x, last_y = things[ix - 1]
constraints.append(Or(
And(x == last_x + 1, y == last_y),
And(x == last_x - 1, y == last_y),
And(x == last_x, y == last_y + 1),
And(x == last_x, y == last_y - 1),
And(x == last_x, y == last_y)
))
constraints.append(x >= 0)
constraints.append(x <= 9)
constraints.append(y >= 0)
constraints.append(y <= 9)
for wall_pos in walls:
constraints.append(Not(And(x == (wall_pos % 10), y == (wall_pos // 10))))
constraints.append(xs[-1] == 9)
constraints.append(ys[-1] == 9)
#print(constraints)
for constraint in constraints: solver.add(constraint)
print(solver.check())
model = solver.model()
out = []
for ix, (x, y) in enumerate(zip(xs, ys)):
xp, yp = model.evaluate(x), model.evaluate(y)
#print(ix, xp, yp)
out.append(xp.as_long() + yp.as_long() * 10)
return out
def fail(e): raise Exception(e)
def print_maze(m, p):
out = [ [ "" if x else "_" for x in row ] for row in chunks(m, 10) ]
for ix, pc in enumerate(p):
out[pc // 10][pc % 10] = chr(ix % 26 + 97) if m[pc] == 0 else fail("all is bees")
print("\n".join([ " ".join(row) for row in out ]))
assert p[-1] == 99
def entry(maze):
p = main_solver(maze)
print_maze(maze, p)
print([{-10:1,1:2,10:3,-1:4}[y-x] for x,y in zip(p,p[1:]) if x != y])
print(entry([
0,1,0,0,0,1,0,0,0,1,
0,1,0,1,0,1,0,1,0,0,
0,1,0,1,0,1,0,1,1,0,
0,1,0,1,0,1,0,1,0,0,
0,1,0,1,0,1,0,1,0,1,
0,1,0,1,0,1,0,1,0,0,
0,1,0,1,0,1,0,1,1,0,
0,1,0,1,0,1,0,1,0,0,
0,1,0,1,0,1,0,1,0,1,
0,0,0,1,0,0,0,1,0,0]))
print(entry([
0,1,0,0,0,1,0,0,0,1,
0,1,0,1,0,1,0,1,0,0,
0,0,0,1,0,1,0,1,1,0,
0,1,0,1,0,1,0,1,0,0,
0,1,0,1,0,1,0,1,0,1,
0,1,0,1,0,1,0,1,0,0,
0,1,0,0,0,1,0,1,1,0,
0,1,0,0,0,1,0,1,0,0,
0,1,0,1,0,1,0,1,1,0,
0,0,0,1,0,0,0,0,1,0
]))
print(entry([
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0
]))
print(entry([
0,1,0,0,0,0,0,0,1,0,
0,0,0,0,0,1,1,0,0,0,
1,1,1,0,1,1,0,0,1,0,
0,1,1,0,0,0,1,0,0,1,
0,0,0,0,1,0,1,1,0,0,
1,0,0,0,0,1,0,0,0,1,
0,0,1,1,1,0,1,0,1,0,
1,0,0,0,1,0,1,0,0,0,
0,0,0,0,1,0,0,1,1,1,
1,0,1,0,0,0,0,0,0,0
]))
print(entry([
0,0,0,0,0,0,1,0,0,0,
0,0,1,0,1,0,0,0,1,0,
0,0,1,1,0,0,1,1,1,0,
0,0,0,0,1,0,0,0,0,0,
0,1,0,0,1,0,1,0,0,0,
0,0,1,0,0,0,0,0,0,0,
0,1,0,0,0,0,1,0,1,0,
0,0,0,1,0,0,0,1,0,0,
0,0,0,1,0,0,0,0,0,0,
1,0,0,0,0,1,0,0,0,0
]))

94
maze2.py Executable file
View File

@ -0,0 +1,94 @@
#!/usr/bin/env python3
T=10
def R(m,p=[0]):
*_,c=p
if c==99:return p
a=[c+T,c-T]
if e:=c%T:a+=~-c,
if e!=9:a+=-~c,
for b in a:
if 99>=b>=0and b not in p and m[b]==0and (r:=R(m,p+[b])):return r
def entry(m):
p=R(m)
return [{-T:1,1:2,T:3,-1:4}[y-x] for x,y in zip(p,p[1:])]
def divide_chunks(l, n):
# looping till length l
for i in range(0, len(l), n):
yield l[i:i + n]
def fail(e): raise Exception(e)
def print_maze(m, p):
out = [ [ "" if x else "_" for x in row ] for row in divide_chunks(m, 10) ]
for ix, pc in enumerate(p):
out[pc // 10][pc % 10] = chr(ix % 26 + 97) if m[pc] == 0 else fail("all is bees")
print("\n".join([ " ".join(row) for row in out ]))
assert p[-1] == 99
def directions(x): return list(map({1: "up", 3: "down", 2: "right", 4: "left" }.get, x))
print(entry([
0,1,0,0,0,1,0,0,0,1,
0,1,0,1,0,1,0,1,0,0,
0,1,0,1,0,1,0,1,1,0,
0,1,0,1,0,1,0,1,0,0,
0,1,0,1,0,1,0,1,0,1,
0,1,0,1,0,1,0,1,0,0,
0,1,0,1,0,1,0,1,1,0,
0,1,0,1,0,1,0,1,0,0,
0,1,0,1,0,1,0,1,0,1,
0,0,0,1,0,0,0,1,0,0]))
print(entry([
0,1,0,0,0,1,0,0,0,1,
0,1,0,1,0,1,0,1,0,0,
0,0,0,1,0,1,0,1,1,0,
0,1,0,1,0,1,0,1,0,0,
0,1,0,1,0,1,0,1,0,1,
0,1,0,1,0,1,0,1,0,0,
0,1,0,0,0,1,0,1,1,0,
0,1,0,0,0,1,0,1,0,0,
0,1,0,1,0,1,0,1,1,0,
0,0,0,1,0,0,0,0,1,0
]))
print(entry([
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0
]))
print(entry([
0,1,0,0,0,0,0,0,1,0,
0,0,0,0,0,1,1,0,0,0,
1,1,1,0,1,1,0,0,1,0,
0,1,1,0,0,0,1,0,0,1,
0,0,0,0,1,0,1,1,0,0,
1,0,0,0,0,1,0,0,0,1,
0,0,1,1,1,0,1,0,1,0,
1,0,0,0,1,0,1,0,0,0,
0,0,0,0,1,0,0,1,1,1,
1,0,1,0,0,0,0,0,0,0
]))
print(entry([
0,0,0,0,0,0,1,0,0,0,
0,0,1,0,1,0,0,0,1,0,
0,0,1,1,0,0,1,1,1,0,
0,0,0,0,1,0,0,0,0,0,
0,1,0,0,1,0,1,0,0,0,
0,0,1,0,0,0,0,0,0,0,
0,1,0,0,0,0,1,0,1,0,
0,0,0,1,0,0,0,1,0,0,
0,0,0,1,0,0,0,0,0,0,
1,0,0,0,0,1,0,0,0,0
]))

89
pd2.scm Normal file
View File

@ -0,0 +1,89 @@
;; pd2.scm - iterated prisoner's dilemma simulation but you know your opponent's strategy
;; by matt
;; this program is in the public domain
(import (chicken random))
(define strategies '())
(define iters 0)
(define add-strategy
(lambda (x y)
(set! strategies (cons (cons x y) strategies))))
(define prisond
(lambda (x y)
(if (= x y)
(if (= x 1)
'(-2 -2)
'(-1 -1))
(if (= x 1)
'(0 -3)
'(-3 0)))))
(define iter-prisond
(lambda (x y z)
(define scores '(0 0))
(define moves-x '())
(define moves-y '())
(define current-moves '())
(define helper
(lambda (x y z)
(if (= z 0)
scores
(begin
(set! current-moves (list (x moves-x moves-y y) (y moves-y moves-x x)))
(set! moves-x (cons (cadr current-moves) moves-x))
(set! moves-y (cons (car current-moves) moves-y))
(set! scores (map + scores (prisond (car current-moves) (cadr current-moves))))
(helper x y (- z 1)))))) (helper x y z)))
(define get-strategy-scores
(lambda (x)
(define score 0)
(define helper
(lambda (y)
(if (eqv? (car x) (car y))
0
(set! score (+ score (car (iter-prisond (cdr x) (cdr y) (+ 100 iters))))))))
(map helper strategies)
score))
(define get-all-scores
(lambda ()
(define helper
(lambda (x)
(write (list (car x) (get-strategy-scores x)))
(newline)))
(map helper strategies)))
(define angel
(lambda (x y z)
0))
(define devil
(lambda (x y z)
1))
(define tit-for-tat
(lambda (x y z)
(if (null? x)
0
(car x))))
(define grudger
(lambda (x y z)
(if (memq 1 y)
1
0)))
(add-strategy 'angel angel)
(add-strategy 'devil devil)
(add-strategy 'tit-for-tat tit-for-tat)
(add-strategy 'grudger grudger)
(set-pseudo-random-seed! (random-bytes))
(set! iters (pseudo-random-integer 50))
(get-all-scores)

View File

@ -1,5 +1,5 @@
(define count-zeros
(lambda (x) (if (= (random 50) 0) (count-zeros (+ 1 x)) x)))
(lambda (x) (if (= (random 50) 48) (count-zeros (+ 1 x)) x)))
(define try-sequentially (lambda (x max)
(random-seed x)
(let ((zeros (count-zeros 0))) (if (> zeros max)