summaryrefslogtreecommitdiff
path: root/sol.py
blob: 7b21ef497d6089395c162a06aafe892ac508fecb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import random
from itertools import product
from collections import defaultdict
from sys import argv
from fann2 import libfann

maze = [ [0, 0, 0, 0, 0, 0, 0],
         [0, 0, 1, 0, 0, 0, 0],
         [0, 1, 1, 0, 0, 0, 0],
         [0, 0, 1, 0, 0, 1, 0],
         [0, 0, 0, 0, 0, 1, 2] ]

start=(1,1)
n_episodes = 1000000
theta = 0.001
gamma=0.9 # discount
epsilon = 0.1
epsilon_reduction = 1
alpha = 0.4
alpha_reduction = 0.9998
friendlyness = 0.7  # probability that the model actually performs the action we've requested.
                    # the model will perform a random other actions with probability of (1-friendlyness)/3.
                    # putting 0.25 here will make it just random.
frameskip = 99
visu = True


def cursor_up(n):
        print("\033[%dA" % n)

def args(argv):
    result=defaultdict(lambda:None)
    ss=None
    for s in argv[1:]+["-"]:
        if s[0]=='-':
            if ss!=None:
                result[ss]=True
            ss=s
        else:
            if ss!=None:
                result[ss]=s
                ss=None
            else:
                explode
    return result

arg=args(argv)
print(arg)

mode = None
if arg['-h']:
    print("Usage: %s [OPTIONS]" % argv[0])
    print("       OPTIONS: --theta NUM     # convergence threshold\n" +
          "                                # default: %f\n" % theta +
          "                --gamma NUM     # learning discount\n" +
          "                                # default: %f\n" % gamma +
          "                --alpha NUM     # learning rate for q-learning\n" +
          "                                # default: %f\n" % alpha +
          "                --alphared NUM  # reduction of alpha per episode\n" +
          "                                # default: %f\n" % alpha_reduction +
          "                --friendly NUM  # friendlyness of the system (probability\n" +
          "                                  that the requested action is really done)\n" +
          "                                # default: %f\n" % friendlyness +
          "                --epsilon NUM   # value for the epsilon-policy used in q-learning\n" +
          "                                # default: %f\n" % epsilon +
          "                --epsred NUM    # reduction of epsilon per episode\n" +
          "                                # default: %f\n" % epsilon_reduction +
          "                --episodes NUM  # maximum number of episodes\n"+
          "                                # default: %i\n\n" % n_episodes +
          "                --qfunc TYPE    # type of the Q function's representation\n" +
          "                                  arr / array -> plain standard array\n" +
          "                                  nn          -> neural network representation\n" +
          "                                  default: array" +
          "                --frameskip NUM # frameskip for visualisation\n" +
          "                                # default: %f\n" % frameskip +
          "                --quiet         # disable visualisation\n" +
          "                --file FILE     # output file for q learning")
    exit()


if arg['-q'] or arg['--quiet']:
    visu = False

if arg['--frameskip']:
    frameskip = int(arg['--frameskip'])

if arg['--episodes']:
    n_episodes = int(arg['--episodes'])

if arg['--theta']:
    theta = float(arg['--theta'])

if arg['--gamma']:
    gamma = float(arg['--gamma'])

if arg['--epsilon']:
    epsilon = float(arg['--epsilon'])

if arg['--epsred']:
    epsilon_reduction = float(arg['--epsred'])

if arg['--alpha']:
    alpha = float(arg['--alpha'])

if arg['--alphared']:
    alpha_reduction = float(arg['--alphared'])

if arg['--friendly']:
    friendlyness = float(arg['--friendly'])

logfile = None
if arg['--file']:
    logfile = open(arg['--file'], "w")

NORTH=0
EAST=1
SOUTH=2
WEST=3

directions = [NORTH, EAST, SOUTH, WEST]
dir_coords = [(0,-1), (1,0), (0,1), (-1,0)]

def argmax(l):
    return max(range(len(l)), key=lambda i:l[i])

def draw_randomly(d):
    c = 0.
    rnd = random.random()
    for k in d:
        c += d[k]
        if rnd < c:
            return k

def visualize(maze, Q):
    n=0
    for y in range(len(maze)):
        line1=""
        line2=""
        line3=""
        line4=""
        line5=""
        for x in range(len(maze[0])):
            if maze[y][x] == 1:
                f = lambda s : s.replace(" ","@")
            elif maze[y][x] == 2:
                f = lambda s : s.replace(" ","+")
            else:
                f = lambda s : s

            Qev = Q.eval(x,y)
            maxdir = argmax(Qev)
            line3 += f("'       " + ("^" if maxdir == NORTH else " ") + "       ")
            line5 += f("        " + ("v" if maxdir == SOUTH else " ") + "       ")
            line1 += f("     %06.2f     " % Qev[NORTH])
            line2 += f("%s%06.2f  %06.2f%s" % ("<" if maxdir == WEST else " ",Qev[WEST], Qev[EAST], ">" if maxdir == EAST else " "))
            line4 += f("     %06.2f     " % Qev[SOUTH])
        print(line3)
        print(line1)
        print(line2)
        print(line4)
        print(line5)
        n+=5
    return n

class World:
    def __init__(self, maze, pos):
        self.x,self.y = pos
        self.maze = maze
        self.xlen = len(maze[0])
        self.ylen = len(maze)

    def possible_next_states(self, s):
        # must return at least all possible states.
        # must only return valid states.
        x,y = s
        return filter(lambda s : s[0]>=0 and s[1]>=0 and s[0] < self.xlen and s[1] < self.ylen, [(x,y),(x+1,y),(x-1,y),(x,y-1),(x,y+1)])


    # definitely walks from (x,y) into direction.
    # returns the neighboring coordinate on success,
    # or the old one if there was a wall
    def walk(self, x,y, direction):
        dx,dy=dir_coords[direction]
        nx,ny = x+dx, y+dy

        if 0 <= nx and nx < self.xlen and \
           0 <= ny and ny < self.ylen and \
           self.maze[y][x] == 0 and \
           self.maze[ny][nx] != 1:
            return nx,ny
        else:
            return x,y


    # gives probabilities for new states, given
    # the command "direction".
    def action(self, x,y , direction):
        newstates = defaultdict(lambda:0.)
        for i in range(4):
            newstates[ self.walk(x,y, (direction+i)%4 ) ] += friendlyness if i == 0 else (1-friendlyness)/3. #[1.0,0.,0.,0.][i] # [0.7,0.1,0.1,0.1][i]
        return newstates

    def take_action(self, x,y, direction):
        newstates = self.action(x,y,direction)
        ss = draw_randomly(newstates)
        return self.R((x,y),ss, None), ss


    def R(self, s, ss, pi):
        if s!=ss and self.maze[ss[1]][ss[0]] == 2: # goal
            #return 1.0
            return 0.5
        else:
            return 0.

    def is_final(self,s):
        return self.maze[s[1]][s[0]] == 2


class QCommon:
    def __init__(self):
        self.alpha = alpha
        self.gamma = gamma
        self.maxdiff = 0.0

    # returns a pair of (diff, self.eval(oldstate)).
    # diff is meant to be added to self.eval(oldstate)[action]
    def value_update(self, oldstate, action, newstate, reward):
        eval_newstate = self.eval(newstate)
        eval_oldstate = self.eval(oldstate)
        diff = self.alpha * (reward + self.gamma * max( [ eval_newstate[aa] for aa in directions ] ) - eval_oldstate[action])
        
        if self.maxdiff < diff: self.maxdiff = diff
        
        return diff, eval_oldstate

    def episode(self):
        maxdiff = self.maxdiff
        self.maxdiff = 0.0
        return maxdiff
        

# abstracts the Q-array. semantics of .eval(x,y) is `Q[y][x]`. semantics of .change((x,y),ac,diff) is `Q[y][x][ac]+=diff`
class QArray(QCommon):
    def __init__(self):
        super().__init__()
        self.Q = [ [ [0. for k in range(4)] for i in range(a.xlen) ] for j in range(a.ylen) ]
        self.learnbuffer = []

    # calculates Q(x,y)
    def eval(self,x,y = None):
        if y==None: x,y = x

        return self.Q[y][x]
    
    def learn(self, oldstate, action, newstate, reward):
        self.learnbuffer += [(oldstate,action,newstate,reward)]
        # self.flush_learnbuffer() # TODO TRYME

    def flush_learnbuffer(self):
        for oldstate, action, newstate, reward in reversed(self.learnbuffer):
            diff,_ = self.value_update(oldstate,action,newstate,reward)
            self.Q[oldstate[1]][oldstate[0]][action] += diff
        self.learnbuffer = []

    def episode(self):
        self.flush_learnbuffer()
        return super().episode()

# implements the Q function not through an array, but through a neuronal network instead.
class QNN (QCommon):
    def __init__(self):
        super().__init__()
        self.learnbuffer = []
        self.dumbtraining = False
        connection_rate = 1
        num_input = 2
        #hidden = (20,20)
        hidden = (20,10,7)
        num_output = 4
        learning_rate = 0.7

        self.NN = libfann.neural_net()
        #self.NN.set_training_algorithm(libfann.TRAIN_BATCH)
        self.NN.set_training_algorithm(libfann.TRAIN_RPROP)
        #self.NN.set_training_algorithm(libfann.TRAIN_QUICKPROP)
        self.NN.create_sparse_array(connection_rate, (num_input,)+hidden+(num_output,))
        self.NN.set_learning_rate(learning_rate)
        self.NN.set_activation_function_hidden(libfann.SIGMOID_SYMMETRIC_STEPWISE)
        self.NN.set_activation_function_output(libfann.SIGMOID_SYMMETRIC_STEPWISE)
        #self.NN.set_activation_function_output(libfann.LINEAR)
    
    def eval(self,x,y = None):
        if y==None: x,y = x

        return self.NN.run([x,y])
    
    def change(self, s, action, diff):
        oldval = self.eval(s)
        newval = list(oldval) # copy list
        newval[action] += diff

        self.NN.train(list(s), newval)

    # learn a transition "from oldstate by action into newstate with reward `reward`"
    # this does not necessarily mean that the action is instantly trained into the function 
    # representation. It may be held back in a list, to be batch-trained lated.
    def learn(self, oldstate, action, newstate, reward):
        diff,_ = self.value_update(oldstate,action,newstate,reward)
        if self.dumbtraining == True:
            self.change(oldstate,action,diff)
        else:
            self.learnbuffer += [(oldstate, action, newstate, reward)]
            self.learnbuffer = self.learnbuffer[-20000:]
            
            self.train_on_minibatch()

    def train_on_minibatch(self):
        n = min(30, len(self.learnbuffer))
        minibatch = random.sample(self.learnbuffer, n)

        inputs = []
        outputs = []
        for oldstate, action, newstate, reward in minibatch:
            diff, val = self.value_update(oldstate, action, newstate, reward)
            val[action] += diff
            inputs += [ list(oldstate) ]
            outputs += [ val ]

        #print("training minibatch of size %i:\n%s\n%s\n\n"%(n, str(inputs), str(outputs)))

        training_data = libfann.training_data()
        training_data.set_train_data(inputs, outputs)
        #self.NN.train_epoch(training_data)
        self.NN.train_on_data(training_data, 5, 0, 0)
        #print(".")

    # must be called on every end-of-episode. might trigger batch-training or whatever.
    #def episode(self):
    #    pass

a = World(maze, start)

Q = None
if arg['--qfunc'] == "nn":
    Q = QNN()
else:
    Q = QArray()

i=0
stopstate = -1
total_reward = 0.

for i in range(n_episodes):
    s = start
    for j in range(100):
        # epsilon-greedy
        greedy = argmax(Q.eval(s))
        rnd = random.random()
        action = None
        if rnd < epsilon:
            action = ( greedy + int(1 + 3 * rnd / epsilon) ) % 4
        else:
            action = greedy

        r,ss = a.take_action(s[0],s[1], action)
        
        Q.learn(s,action,ss,r)
        
        total_reward += r
        s = ss
        if a.is_final(ss):
            break
    maxdiff = Q.episode()

    if (i % (frameskip+1) == 0):
        print("iteration %.3d, alpha=%.3e, epsilon=%.3e maxdiff=%.7f"%(i,Q.alpha,epsilon,maxdiff))
        n = 0
        if visu:
            n = visualize(maze,Q)
        cursor_up(n+2)

    if (logfile != None):
        print("%d\t%f" % (i, total_reward), file=logfile)

    # Wikipedia says on this: "When the problem is stochastic [which it is!],
    # the algorithm still converges under some technical conditions on the
    # learning rate, that require it to decrease to zero.
    # So let's sloooowly decrease our learning rate here. Otherwise it won't
    # converge, but instead oscillate plus/minus 0.5.
    # However, if we set the friendlyness of our system to 1.0, then it would
    # also converge without this learning rate reduction, because we have a
    # non-stochastic but a deterministic system now.
    Q.alpha *= alpha_reduction
    epsilon *= epsilon_reduction

    
    # stop once we're below theta for at least 100 episodes. But not before we went above theta at least once.
    if maxdiff < theta:
        stopstate -= 1
        if stopstate == 0:
            break
    else:
        stopstate = 1000

print("finished after %.3d iterations, alpha=%.3e, epsilon=%.3e"%(i,Q.alpha,epsilon))
visualize(maze,Q)
if logfile != None:
    logfile.close()