Пример #1
0
def generator(db, dbtype, batchsize, shuffle=False, augment=True):
    inputs = db['images']
    classes = db['class']
    targets = db['maps']
    indices = db.get_inds(dbtype)

    while True:
        if shuffle:
            np.random.shuffle(indices)

        for start_idx in range(0, len(indices) - batchsize + 1, batchsize):
            if shuffle:
                excerpt = indices[start_idx:start_idx + batchsize]
                sortind = np.sort(excerpt).tolist()
            else:
                sortind = slice(start_idx, start_idx + batchsize)

            # take subset (should be numpy array)
            batch_inputs = inputs[sortind]
            batch_targets = targets[sortind]
            batch_class = classes[sortind]
            batch_class = np_utils.to_categorical(batch_class, 20)

            if AUGMENT_DATA and augment:
                in_shape = batch_inputs.shape[0]  # number of images in batch
                ind = np.random.choice(in_shape, in_shape / 2, replace=False)

                # flip
                batch_inputs[ind] = utilities.flip(batch_inputs, ind)
                batch_targets[ind] = utilities.flip(batch_targets, ind)

            batch_targets = np.expand_dims(batch_targets, axis=1)
            yield batch_inputs, [batch_targets, batch_class]
Пример #2
0
 def select_action(self, state):
     """returns an action to carry out for the current agent
     given the state, and the q-function
     """
     if flip(self.explore):
         return random.choice(self.actions)
     else:
         return argmaxe((next_act, self.q.get((state, next_act),self.qinit))
                               for next_act in self.actions)
Пример #3
0
 def do(self, action):
     """updates the state based on the agent doing action.
     returns state,reward
     """
     if self.state == "healthy":
         if action == "party":
             self.state = "healthy" if flip(0.7) else "sick"
             reward = 10
         else:  # action=="relax"
             self.state = "healthy" if flip(0.95) else "sick"
             reward = 7
     else:  # self.state=="sick"
         if action == "party":
             self.state = "healthy" if flip(0.1) else "sick"
             reward = 2
         else:
             self.state = "healthy" if flip(0.5) else "sick"
             reward = 0
     return self.state, reward
Пример #4
0
 def mutate(self, rate=0.5):
     child_genome = BitGenome(self.length, self.rand)
     child = copy.copy(self.get_genome())
     child_list = list(child)
     num, den = utilities.convert_rate_to_fraction(rate)
     for i in range(len(child_list)):
         loc = self.rand.randint(1, den + 1)
         if loc <= num:
             child_list[i] = utilities.flip(child_list[i])
     child_genome.initialize("".join(child_list))
     return child_genome
Пример #5
0
 def select_action(self, state):
     """returns an action to carry out for the current agent
     given the state, and the q-function.
     This implements an epsilon-greedy approach
     where self.explore is the probability of exploring.
     """
     if flip(self.explore):
         return random.choice(self.actions)
     else:
         return argmax((next_act, dot_product(self.weights,
                                              self.get_features(state,next_act)))
                               for next_act in self.actions)
Пример #6
0
gui = Tk()
gui.title('Bubbles from file')
window_width = gui.winfo_screenwidth()
window_height = gui.winfo_screenheight()
canvas = Canvas(gui,
                width=window_width,
                height=window_height,
                background='white')
canvas.pack()
##################################################################################
point_list = []
f = open(utilities.get_file_path('florida.csv'))
for line in f.readlines():
    line = line.replace('\n', '')
    items = line.split(',')
    x = float(items[0])
    y = float(items[1])
    point_list.append((x, y))

canvas.create_polygon(point_list, fill='white', outline='black', tag='florida')
gui.update()

while True:
    time.sleep(2)
    utilities.flip(canvas, 'florida')
    gui.update()

# Your code above this line
##################################################################################
canvas.mainloop()
Пример #7
0
    def do(self,action):
        """updates the state based on the agent doing action.
        returns state,reward
        """
        reward = 0.0
        # A prize can appear:
        if self.prize is None and flip(self.prize_apears_prob):
                self.prize = random.choice(self.prize_locs)
        # Actions can be noisy
        if flip(0.4):
            actual_direction = random.choice(self.actions)
        else:
            actual_direction = action
        # Modeling the actions given the actual direction
        if actual_direction == "right":
            if self.x==self.xdim-1 or (self.x,self.y) in self.vwalls:
                reward += self.crashed_reward
            else:
                self.x += 1
        elif actual_direction == "left":
            if self.x==0 or (self.x-1,self.y) in self.vwalls:
                reward += self.crashed_reward
            else:
                self.x += -1
        elif actual_direction == "up":
            if self.y==self.ydim-1:
                reward += self.crashed_reward
            else:
                self.y += 1
        elif actual_direction == "down":
            if self.y==0:
                reward += self.crashed_reward
            else:
                self.y += -1
        else:
            raise RuntimeError("unknown_direction "+str(direction))

        # Monsters
        if (self.x,self.y) in self.monster_locs and flip(self.monster_appears_prob):
            if self.damaged:
                reward += self.monster_reward_when_damaged
            else:
                self.damaged = True
        if (self.x,self.y) in self.repair_stations:
            self.damaged = False

        # Prizes
        if (self.x,self.y) == self.prize:
            reward += self.prize_reward
            self.prize = None

        # Statistics
        self.number_steps += 1
        self.total_reward += reward
        if self.total_reward < self.min_reward:
            self.min_reward = self.total_reward
            self.min_step = self.number_steps
        if self.total_reward>0 and reward>self.total_reward:
            self.zero_crossing = self.number_steps
        self.display(2,"",self.number_steps,self.total_reward,
                      self.total_reward/self.number_steps,sep="\t")

        return (self.x, self.y, self.damaged, self.prize), reward