def forward_prop(self, inputs): """ Calculate output from given inputs through the neural network """ layers = [inputs] for i in range(len(self.h_layers)+1): # Calculate the input * weights + bias z = np.dot(layers[i], self.weights[i]) + self.biases[i] # Apply activation function out = [] if d.sensor_mode: # Outputs are numbers for j in range(len(z)): o = sigmoid(clamp(-20, 20, z[j])) out.append(o) else: # Output are vectors for j in range(len(z)): sigm_x = sigmoid(clamp(-20, 20, z[j].x)) sigm_y = sigmoid(clamp(-20, 20, z[j].y)) out.append(Vector2(sigm_x, sigm_y)) layers.append(out) # Return the output final_output = layers[len(layers)-1][0] # Last layer only has 1 output neuron return final_output
def VectorAngle(vector1, vector2): """Returns the angle, in degrees, between two 3-D vectors Parameters: vector1 = List of 3 numbers, Point3d, or Vector3d. The first 3-D vector. vector2 = List of 3 numbers, Point3d, or Vector3d. The second 3-D vector. Returns: The angle in degrees if successfull, otherwise None Example: import rhinoscriptsyntax as rs s0 = rs.GetObject("Surface 0", rs.filter.surface) s1 = rs.GetObject("Surface 1", rs.filter.surface) du0 = rs.SurfaceDomain(s0, 0) dv0 = rs.SurfaceDomain(s0, 1) du1 = rs.SurfaceDomain(s1, 0) dv1 = rs.SurfaceDomain(s1, 1) n0 = rs.SurfaceNormal(s0, (du0[0], dv0[0])) n1 = rs.SurfaceNormal(s1, (du1[0], dv1[0])) print rs.VectorAngle(n0, n1) print rs.VectorAngle(n0, rs.VectorReverse(n1)) See Also: Angle Angle2 """ vector1 = rhutil.coerce3dvector(vector1, True) vector2 = rhutil.coerce3dvector(vector2, True) vector1 = Rhino.Geometry.Vector3d(vector1.X, vector1.Y, vector1.Z) vector2 = Rhino.Geometry.Vector3d(vector2.X, vector2.Y, vector2.Z) if not vector1.Unitize() or not vector2.Unitize(): raise ValueError("unable to unitize vector") dot = vector1 * vector2 dot = rhutil.clamp(-1, 1, dot) radians = math.acos(dot) return math.degrees(radians)
def VectorAngle(vector1, vector2): """Returns the angle, in degrees, between two 3-D vectors Parameters: vector1 = List of 3 numbers, Point3d, or Vector3d. The first 3-D vector. vector2 = List of 3 numbers, Point3d, or Vector3d. The second 3-D vector. Returns: The angle in degrees if successfull, otherwise None Example: import rhinoscriptsyntax as rs s0 = rs.GetObject("Surface 0", rs.filter.surface) s1 = rs.GetObject("Surface 1", rs.filter.surface) du0 = rs.SurfaceDomain(s0, 0) dv0 = rs.SurfaceDomain(s0, 1) du1 = rs.SurfaceDomain(s1, 0) dv1 = rs.SurfaceDomain(s1, 1) n0 = rs.SurfaceNormal(s0, (du0[0], dv0[0])) n1 = rs.SurfaceNormal(s1, (du1[0], dv1[0])) print rs.VectorAngle(n0, n1) print rs.VectorAngle(n0, rs.VectorReverse(n1)) See Also: Angle Angle2 """ vector1 = rhutil.coerce3dvector(vector1, True) vector2 = rhutil.coerce3dvector(vector2, True) vector1 = Rhino.Geometry.Vector3d(vector1.X, vector1.Y, vector1.Z) vector2 = Rhino.Geometry.Vector3d(vector2.X, vector2.Y, vector2.Z) if not vector1.Unitize() or not vector2.Unitize(): raise ValueError("unable to unitize vector") dot = vector1 * vector2 dot = rhutil.clamp(-1,1,dot) radians = math.acos(dot) return math.degrees(radians)
def history_step(self, parent): self.age += 1 if random.randint(0, 100) < IMPORTANCE_CHANGE_CHANCE: self.importance += random.randint(-IMPORTANCE_CHANGE_AMOUNT, IMPORTANCE_CHANGE_AMOUNT) self.importance = utility.clamp(self.importance, MAX_IMPORTANCE, 0) #We can't lose our last domain of course if len(self.domains) > 1 and random.randint(0, 100) < DOMAIN_LOSE_CHANCE: lost_domain = random.choice(self.domains) self.domains.remove(lost_domain) parent.event_log.add_event('ReligionDomainRemoved', {'god_a': self.name, 'religion_a': self.religion.name, 'domain_a': lost_domain}, parent.get_current_date()) if len(self.domains) <= MAX_DOMAIN_COUNT and random.randint(0, 100) < DOMAIN_GAIN_CHANCE: gained_domain = random.choice(DOMAINS.keys()) if not gained_domain in self.domains: self.domains.append(gained_domain) parent.event_log.add_event('ReligionDomainAdded', {'god_a': self.name, 'religion_a': self.religion.name, 'domain_a': gained_domain}, parent.get_current_date())
def mutate(net, strong): """ Mutate a neural network\n Parameters: \n \tnet = the neural net to mutate \tstrong = whether or not the mutation should be strong""" b = net.biases w = net.weights """ weights """ w_new = net.weights for _ in range(d.MUTATED_WEIGHTS_COUNT): # Find a random weight layer_i = random.randint(0, len(w) - 1) input_i = random.randint(0, len(w[layer_i]) - 1) weigth_i = random.randint(0, len(w[layer_i][input_i]) - 1) _w = w[layer_i][input_i][weigth_i] # How strong should the weight be mutated if (strong): mut_strength = d.MUTATION_STRENGTH_HIGH else: mut_strength = d.MUTATION_STRENGTH_LOW # Mutate the weight (weights are always clamped between -1.5 and 1.5) rd = random.uniform(-mut_strength, mut_strength) w_new[layer_i][input_i][weigth_i] = clamp(-1.5, 1.5, (_w + rd)) net.weights = w_new """ biases """ index = random.randint(0, len(b) - 1) b[index] += random.uniform(-mut_strength, mut_strength) net.biases = b return net
def VectorAngle(vector1, vector2): "Returns the angle, in degrees, between two 3-D vectors" vector1 = rhutil.coerce3dvector(vector1, True) vector2 = rhutil.coerce3dvector(vector2, True) vector1 = Rhino.Geometry.Vector3d(vector1.X, vector1.Y, vector1.Z) vector2 = Rhino.Geometry.Vector3d(vector2.X, vector2.Y, vector2.Z) if not vector1.Unitize() or not vector2.Unitize(): raise ValueError("unable to unitize vector") dot = vector1 * vector2 dot = rhutil.clamp(-1,1,dot) radians = math.acos(dot) return math.degrees(radians)
def forward_prop(self, inputs): layers = [inputs] for i in range(len(self.h_layers) + 1): # Calculate the input * weights + bias z = sum_matrix_float(np.dot(layers[i], self.weights[i]), self.biases[i]) # Apply activation function o = [sigmoid(clamp(-20, 20, z[j])) for j in range(len(z))] layers.append(o) # Return the output final_output = layers[len(layers) - 1][0] return final_output
def rough_color_match(a, b, tolerance): if tolerance == 0.0: return tuple(a) == tuple(b) a = map(float, (utility.clamp(a[0], 0.004, 1), utility.clamp( a[1], 0.004, 1), utility.clamp(a[2], 0.004, 1))) b = map(float, (utility.clamp(b[0], 0.004, 1), utility.clamp( b[1], 0.004, 1), utility.clamp(b[2], 0.004, 1))) drMatch = int(utility.rough_match(a[0], b[0], tolerance)) dgMatch = int(utility.rough_match(a[1], b[1], tolerance)) dbMatch = int(utility.rough_match(a[2], b[2], tolerance)) total = drMatch + dgMatch + dbMatch if total == 3: return True for ar, br in zip(utility.all_ratios(a), utility.all_ratios(b)): total += int(utility.rough_match(ar, br, tolerance)) if total > 4: return True return False
def mutate(net, strong): """ Mutate a neural network\n Parameters: \n \tnet = the neural net to mutate \tstrong = whether or not the mutation should be strong""" b = net.biases w = net.weights """ weights """ w_new = net.weights for _ in range(d.MUTATED_WEIGHTS_COUNT): # Find a random weight layer_i = random.randint(0, len(w) - 1) input_i = random.randint(0, len(w[layer_i]) - 1) weigth_i = random.randint(0, len(w[layer_i][input_i]) - 1) _w = w[layer_i][input_i][weigth_i] # How strong should the weight be mutated if (strong): mut_strength = d.MUTATION_STRENGTH_HIGH else: mut_strength = d.MUTATION_STRENGTH_LOW # Mutate the weight (weights are always clamped between -1 and 1) rd = random.uniform(-mut_strength, mut_strength) w_new[layer_i][input_i][weigth_i] = clamp(-1, 1, _w + rd * _w) net.weights = w_new """ biases """ # Find a random bias index = random.randint(0, len(b) - 1) # How strong should the bias be mutated rd = random.uniform(-mut_strength, mut_strength) # Mutate the bias if d.sensor_mode: # Bias is a number b[index] += rd else: # Bias is a vector b[index] += Vector2(rd, rd) net.biases = b return net