def main(): x_train_start, x_val_start, x_train_end, x_val_end = 0, 0.05, math.pi * 2, math.pi * 2 noise = True # sigma = 0.1 train_sin = sin(x_train_start, x_train_end, 0.1, noise = noise) val_sin = sin(x_val_start, x_val_end, 0.1, noise = noise) train_square = square(x_train_start, x_train_end, 0.1, noise = noise) val_square = square(x_val_start, x_val_end, 0.1, noise = noise) # batch_learning(train_square, val_square, name = "Batch Square w. Noise") # delta_learning(train_square, val_square, name = "Delta Square w. Noise") # grid_search(train_square, val_square) perceptron_learning(train_square, val_square)
def potential_move(self): """move ahead""" next_pos_x = self.current_position['x'] + cos(self.current_direction) next_pos_y = self.current_position['y'] - sin(self.current_direction) if not self.__can_move(next_pos_x, next_pos_y): self.__visited_position[str(next_pos_x) + "_" + str(next_pos_y)] = -1 return False return True
def move(self): """move ahead""" next_pos_x = self.current_position['x'] + cos(self.current_direction) next_pos_y = self.current_position['y'] - sin(self.current_direction) if not self.__can_move(next_pos_x, next_pos_y): self.__visited_position[str(next_pos_x) + "_" + str(next_pos_y)] = -1 return False self.move_count += 1 self.current_position['x'] = next_pos_x self.current_position['y'] = next_pos_y self.__visited_position[str(next_pos_x) + "_" + str(next_pos_y)] = 1 if self.loggable: self.log() return True
def move(self): """move ahead""" next_pos_x = self.current_position['x'] + cos(self.current_direction) next_pos_y = self.current_position['y'] - sin(self.current_direction) if not self.__can_move(next_pos_x, next_pos_y): self.__visited_position[str(next_pos_x) + "_" + str(next_pos_y)] = -1 return False self.move_count += 1 self.current_position['x'] = next_pos_x self.current_position['y'] = next_pos_y self.__visited_position[str(next_pos_x) + "_" + str(next_pos_y)] = 1 #print("[x,y]" + str(self.current_position['x']) + "," + str(self.current_position['y'])) self.path_history.append([self.current_position['x'], self.current_position['y']]) if self.loggable: self.log() return True
def axisAndAngle2RotMatrix(axis, angle): """ http://stackoverflow.com/questions/6802577/python-rotation-of-3d-vector Return the rotation matrix associated with counterclockwise rotation about the given axis by angle radians. """ axis = np.asarray(axis) angle = np.asarray(angle) axis = old_div(axis, utils.sqrt(np.dot(axis, axis))) a = utils.cos(old_div(angle, 2)) b, c, d = -axis * utils.sin(old_div(angle, 2)) aa, bb, cc, dd = a * a, b * b, c * c, d * d bc, ad, ac, ab, bd, cd = b * c, a * d, a * c, a * b, b * d, c * d return np.array([[aa + bb - cc - dd, 2 * (bc + ad), 2 * (bd - ac)], [2 * (bc - ad), aa + cc - bb - dd, 2 * (cd + ab)], [2 * (bd + ac), 2 * (cd - ab), aa + dd - bb - cc]])
def move(self): """move ahead""" next_pos_x = self.current_position['x'] + cos(self.current_direction) next_pos_y = self.current_position['y'] - sin(self.current_direction) if not self.__can_move(next_pos_x, next_pos_y): self.__visited_position[str(next_pos_x) + "_" + str(next_pos_y)] = -1 return False self.move_count += 1 self.current_position['x'] = next_pos_x self.current_position['y'] = next_pos_y self.__visited_position[str(next_pos_x) + "_" + str(next_pos_y)] = 1 #self.diffrobot.go_forward(distance=200, dc=100) self.diffrobot.driveGyro(10) self.shovel.moveShovel() #sleep(3) if self.loggable: self.log() return True
def train(BATCH_SIZE): # X_train = X_train.reshape((X_train.shape, 1) + X_train.shape[1:]) d = discriminator_model() g = generator_model() d.trainable = False opt = Adam(lr=0.00015, beta_1=0.5) d_on_g = generator_containing_discriminator(g, d) d_on_g.compile(loss='binary_crossentropy', optimizer=opt) d_on_g.summary() for epoch in range(2000): i = 0 print("Epoch is {}".format(epoch)) for true_sins in sin(): noise = np.random.normal(0, 1, size=(BATCH_SIZE,)+ (25, 4)) generated_sins = g.predict(noise) if i % 500 == 0: draw_sin(generated_sins, epoch, i) X = np.concatenate((true_sins, generated_sins)) real_data_Y = np.ones((BATCH_SIZE, 25, 1)) - np.random.random_sample((BATCH_SIZE, 25, 1))*0.2 fake_data_Y = np.random.random_sample((BATCH_SIZE, 25, 1))*0.2 y = np.concatenate((real_data_Y,fake_data_Y)) d.trainable = True g.trainable = False dis_metrics_real = d.train_on_batch(true_sins,real_data_Y) #training seperately on real dis_metrics_fake = d.train_on_batch(generated_sins,fake_data_Y) #training seperately on fake if i%200 == 0: print("epoch: %d, batch: %d Disc: real loss: %f fake loss: %f" % (epoch, i, dis_metrics_real, dis_metrics_fake)) #d_loss = d.train_on_batch(X, y) #print("epoch : {} batch : {} d_loss : {}".format(epoch, i, d_loss)) #one batch not ok g.trainable = True noise = np.random.normal(0, 1, size=(BATCH_SIZE,)+ (25, 4)) d.trainable = False g_loss = d_on_g.train_on_batch(noise, real_data_Y) if i%200 == 0: print("epoch : {} batch : {} g_loss : {}".format(epoch, i, g_loss)) i = i+1 g.save_weights('generator_sin') d.save_weights('discriminator_sin')
def move(self): """move ahead""" next_pos_x = self.current_position['x'] + cos(self.current_direction) next_pos_y = self.current_position['y'] - sin(self.current_direction) if not self.__can_move(next_pos_x, next_pos_y): self.__visited_position[str(next_pos_x) + "_" + str(next_pos_y)] = -1 return False self.move_count += 1 self.current_position['x'] = next_pos_x self.current_position['y'] = next_pos_y self.__visited_position[str(next_pos_x) + "_" + str(next_pos_y)] = 1 if self.loggable: self.log() #self.car.rotate(90*self.current_direction) #print("Move") #print('%d, %d' % (next_pos_x, next_pos_y)) self.car.move_step_grid(Vector(next_pos_x, next_pos_y)) time.sleep(0.1) return True
def calculate_next_pos(self): next_pos_x = self.current_position['x'] + cos(self.current_direction) next_pos_y = self.current_position['y'] - sin(self.current_direction) return {'x': next_pos_x, 'y': next_pos_y}
def is_moving_south(droid): return droid.speed > 0 and round(sin(-droid.heading + 90), 3) < 0
def paintEvent(self, e): qp = QPainter() qp.begin(self) self.resize((len(self.data) + 2) * self.interval, config.channels * config.min_channel_size) qp.setPen( QPen(config.second_channel_point_color, config. first_channel_line_spacing)) ######可以试下画刷 setBrush,10指定点的大小 qp.drawLine(0, 0, 0, config.channels * config.min_channel_size) qp.drawLine(0, config.min_channel_size, self.width(), config.min_channel_size) qp.setPen(QPen(Qt.black, 5)) ######可以试下画刷 setBrush,10指定点的大小 for index in range(0, config.min_channel_size, config.coordinate_interval): qp.drawPoint(0, config.min_channel_size - index) qp.setFont(QFont("Decorative", config.font_size)) qp.drawText( QRect(config.y_axes_distance, config.min_channel_size - index, config.coordinate_interval, config.min_channel_size - index + config.font_size), Qt.AlignLeft | Qt.AlignTop, str(index)) for index in range(0, config.min_channel_size, config.coordinate_interval): qp.drawPoint(0, 2 * config.min_channel_size - index) qp.setFont(QFont("Decorative", config.font_size)) qp.drawText( QRect(config.y_axes_distance, 2 * config.min_channel_size - index, config.coordinate_interval, 2 * config.min_channel_size - index + config.font_size), Qt.AlignLeft | Qt.AlignTop, str(index)) for index in range(0, self.width(), int(self.interval)): qp.drawPoint(index, config.min_channel_size) qp.setFont(QFont("Decorative", config.font_size)) qp.drawText( QRect(index, config.min_channel_size - config.font_size, index + config.font_size, config.min_channel_size), Qt.AlignLeft | Qt.AlignTop, str(index)) if self.my_sender: if isinstance(self.my_sender, QSlider): print("滑动了") print(self.my_sender.value()) val = self.my_sender.value() self.interval = int(config.default_interval + config.default_slider_interval * (val - config.default_slider_value)) ## 第二条线 if self.my_sender: if isinstance(self.my_sender, QAction): if self.my_sender.text() == "sin": self.new_data = utils.sin(self.data) else: self.new_data = utils.cos(self.data) # else: # new_data = utils.sin(self.data) if self.new_data: qp.setPen( QPen(config.second_channel_point_color, config.point_size)) ######可以试下画刷 setBrush,10指定点的大小 for index, x in enumerate(self.new_data): qp.drawPoint((index + 1) * self.interval, 2 * config.min_channel_size - x) qp.setPen( QPen(config.second_channel_line_color, config.second_channel_line_spacing, config.second_channel_line_type) ) ####前一个random是线条粗线,后一个random是线条类型 for index, x in enumerate(self.new_data): if index == len(self.new_data) - 1: break qp.drawLine( (index + 1) * self.interval, 2 * config.min_channel_size - x, (index + 2) * self.interval, 2 * config.min_channel_size - self.new_data[index + 1]) self.drawLines(qp) ######画线 self.drawPoints(qp) ###画点 qp.end()