def eval_regression_model(model: NeuralNetwork, X_test: np.ndarray, y_test: np.ndarray): ''' Compute mae and rmse for a neural network. ''' preds = model.forward(X_test) preds = preds.reshape(-1, 1) print("Mean absolute error: {:.2f}".format(mae(preds, y_test))) print() print("Root mean squared error {:.2f}".format(rmse(preds, y_test)))
import numpy as np from neuralnetwork import NeuralNetwork from layers.linear import LinearLayer inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) """ Results for the AND function for each line from the inputs array The first column is False, the second Column is for True """ labels = np.array([[0, 1], [1, 0], [1, 0], [0, 1]]) nn = NeuralNetwork() nn.add_layer(LinearLayer(2, 2)) nn.train(inputs, labels, epochs=5000) for index, row in enumerate(inputs): prediction = nn.forward(row) print row, prediction, labels[index]
class VariablePendulum(): def __init__(self, space, configFile, numActions=500): self.num_actions = numActions self.config = configFile self.space = space self.colour = (np.random.uniform(0, 255), np.random.uniform(0, 255), np.random.uniform(0, 255), 255) self.objects = self.generateModel() self.initial_amplitude = abs(self.angle()) self.maximum_amplitude = abs(self.angle()) self.fitness = 0 self.action_step = 0 self.num_reversals = 0 self.last_action = 0 self.prev_angle = self.angle() self.prev_dir = 2 self.steps_to_done = 0 self.done = False self.reached_max = False self.reached_max_step = 0 self.movable = True self.energydata = [] self.phasedata = [] self.pumpdata = [] self.neuralnetwork = NeuralNetwork(4, 2, *self.config['hiddenLayers']) def update(self): if not self.done: self.dtheta = self.angle() - self.prev_angle self.ddtheta = self.angle() - self.dtheta action = self.neuralnetwork.forward([ abs(self.angle()), abs(self.dtheta), abs(self.ddtheta), self.maximum_amplitude ]) self.step(action) self.checkMovable() self.energydata.append( (self.action_step, self.body.kinetic_energy)) self.phasedata.append((self.angle(), self.dtheta)) self.pumpdata.append( (self.action_step, self.angle(), self.prev_dir)) def resist(self): fx = self.dtheta * self.config['dampingCoefficient'] fy = self.dtheta * self.config['dampingCoefficient'] self.body.apply_impulse_at_local_point((fx, fy)) def generateModel(self): # Create objects moment_of_inertia = 0.25 * self.config["flywheelMass"] * self.config[ "flywheelRadius"]**2 self.body = pymunk.Body(mass=self.config["flywheelMass"], moment=moment_of_inertia) self.body.position = self.config["flywheelInitialPosition"] self.circle = pymunk.Circle(self.body, radius=self.config["flywheelRadius"]) self.circle.filter = pymunk.ShapeFilter( categories=0b1, mask=pymunk.ShapeFilter.ALL_MASKS() ^ 0b1) self.circle.friction = 90000 # Create joints self.joint = pymunk.PinJoint(self.space.static_body, self.body, self.config["pivotPosition"]) self.top = self.body.position[1] + self.config["flywheelRadius"] self.space.add(self.body, self.circle, self.joint) def angle(self): y = self.config['pivotPosition'][1] - self.body.position.y x = self.body.position.x - self.config['pivotPosition'][0] angle = np.arctan(2 * x / y) return angle def extendRope(self, direction): if direction != self.prev_dir: self.num_reversals += 1 self.prev_dir = direction if direction == 0: if self.movable: self.joint.distance = self.config["minPendulumLength"] self.last_action = self.action_step self.movable = False elif direction == 1: if self.movable: self.joint.distance = self.config["maxPendulumLength"] self.last_action = self.action_step self.movable = False def checkMovable(self): if self.action_step - self.last_action > self.config['actionDelay']: self.movable = True def step(self, action): self.action_step += 1 self.prev_angle = self.angle() amplitude = abs(self.angle()) if amplitude > self.maximum_amplitude: self.maximum_amplitude = amplitude if self.body.position.y < self.config['pivotPosition'][1]: self.reached_max = True self.reached_max_step = self.action_step self.done = True self.extendRope(np.argmax(action)) if self.action_step >= self.num_actions: self.done = True def calculateFitness(self): return ((self.maximum_amplitude - self.initial_amplitude) * 180 / np.pi)**2 / (self.num_reversals)**0.5 def destroyModel(self): self.space.remove(self.body, self.circle, self.joint)
class SmoothVariablePendulum(): def __init__(self, space, configFile, numActions=500): self.num_actions = numActions self.config = configFile self.space = space self.colour = (np.random.uniform(0, 255), np.random.uniform(0, 255), np.random.uniform(0, 255), 255) self.objects = self.generateModel() self.initial_amplitude = abs(self.angle()) self.maximum_amplitude = abs(self.angle()) self.fitness = 0 self.action_step = 0 self.num_reversals = 0 self.last_action = 0 self.prev_angle = 0 self.prev_dir = 2 self.steps_to_done = 0 self.done = False self.complete = False self.movable = True self.neuralnetwork = NeuralNetwork(6, 5, *self.config['hiddenLayers']) def update(self): if not self.done: dtheta = self.angle() - self.prev_angle ddtheta = self.angle() - dtheta action = self.neuralnetwork.forward([ abs(self.angle()), abs(dtheta), abs(ddtheta), self.maximum_amplitude, self.body.angular_velocity, self.joint.distance ]) self.step(action) self.checkMovable() def generateModel(self): #Create objects moment_of_inertia = 0.25 * self.config["flywheelMass"] * self.config[ "flywheelRadius"]**2 self.body = pymunk.Body(mass=self.config["flywheelMass"], moment=moment_of_inertia) self.body.position = self.config["flywheelInitialPosition"] self.circle = pymunk.Circle(self.body, radius=self.config["flywheelRadius"]) self.circle.filter = pymunk.ShapeFilter( categories=0b1, mask=pymunk.ShapeFilter.ALL_MASKS() ^ 0b1) self.circle.friction = 90000 self.circle.color = self.colour #Create joints self.joint = pymunk.PinJoint(self.space.static_body, self.body, self.config["pivotPosition"]) self.top = self.body.position[1] + self.config["flywheelRadius"] self.space.add(self.body, self.circle, self.joint) def angle(self): y = self.config['pivotPosition'][1] - self.body.position.y x = self.body.position.x - self.config['pivotPosition'][0] angle = np.arctan(x / y) return angle def extendRope(self, direction): if direction != self.prev_dir and direction != 2: self.num_reversals += 1 self.prev_dir = direction if direction == 0: if self.movable: self.joint.distance -= self.config['squattingSpeed'] if self.joint.distance < self.config['minPendulumLength']: self.joint.distance = self.config["minPendulumLength"] elif direction == 1: if self.movable: self.joint.distance += self.config['squattingSpeed'] if self.joint.distance > self.config['maxPendulumLength']: self.joint.distance = self.config["maxPendulumLength"] elif direction == 2: self.body.angular_velocity += 1 if self.body.angular_velocity > 5: self.body.angular_velocity = 5 elif direction == 3: self.body.angular_velocity -= 1 if self.body.angular_velocity < -5: self.body.angular_velocity = -5 else: pass def checkMovable(self): if self.action_step - self.last_action > self.config['actionDelay']: self.movable = True def step(self, action): self.action_step += 1 self.prev_angle = self.angle() amplitude = abs(self.angle()) if amplitude > self.maximum_amplitude: self.maximum_amplitude = amplitude self.extendRope(np.argmax(action)) if self.action_step >= self.num_actions: self.done = True if self.body.position.y < self.config['pivotPosition'][1]: self.done = True self.complete = True def calculateFitness(self): if self.complete: return (self.maximum_amplitude - self.initial_amplitude) + 200 / self.num_reversals else: return (self.maximum_amplitude - self.initial_amplitude) def destroyModel(self): self.space.remove(self.body, self.circle, self.joint)