def idleDecision(self, seen): candidates_walk = self.objectsSeen(seen) if len(candidates_walk) > 0: if all(candidates_walk[0] == self.position): return ['PICK_UP', []] return ['WALK_TOWARDS', [candidates_walk[0]]] return tools.weighted_choice(self.idleChoices)
def generate_obj_list(self, count): sentence_list = [] counter = 0 firstWord = tools.generateFirstWord(self.pair_tb) # Adds the first word to the sentence_list for i in range (self.level): sentence_list.append(firstWord[i]) # Adds every word after that for remaining number of necessary times for i in range (count - self.level): sentence_list.append(tools.weighted_choice(self.pair_tb, tuple(sentence_list[i:i+self.level]))) return sentence_list #TODO generate a random object from the original list(using random) #note, to make it truly random, you'll need to use a set of the original #list as there might be repeats #TODO look up in the probability table generated earlier for the entry of #that object. If there are more than one object that pairs with this entry, #apply a weighted algorithm based on the count recording(this is why keeping #track of count rather than percentage is easier #TODO repeat until you from a count length list. NOTE: for an nth level #markov chain you will need to look up on the probability an n-length tuple #once you've generated enough words!
def generate_rhythm_pattern(rhythm_prob, chords_per_measure): """ Generates a rhythm pattern based of the "hit-vector" given. Args: rhythm_prob (list of ints [size=8]): A list where each index contains how many times a chord started on that index. Returns: An array that contains a rhythmic pattern, where 0 is no hit and 1 is a hit. This array represents one segment of a song. """ #print(rhythm_prob) #TODO #Account for time signatures other than 4/4 #Add ability to skip measures or add more than one hit per measures, #based off of the corpus #1 Segment = 8 measures #1 Measure = 8 beats #8*8=64 beats per segment beats_per_segment = 64 generated_rhythm_array = [0] * beats_per_segment #Always hit on the first beat (for now) generated_rhythm_array[0] = 1 #Flesh out the rest based on probability for i in range(0, 8): rhythm_copy = list(rhythm_prob) chords_this_measure = tools.weighted_choice(chords_per_measure) #Generate how ever many chords we have to for j in range(0, chords_this_measure): #Figure out what beat to place them on start_beat = tools.weighted_choice(rhythm_copy) beat_to_place = (i * 8) + start_beat #shouldn't be greater than 64 if beat_to_place < 64: generated_rhythm_array[beat_to_place] = 1 #When a beat is placed, set it's probability to zero #So we don't place it again rhythm_copy[start_beat] = (start_beat, 0) else: print("Beat greater than 64!") return generated_rhythm_array
def generate_obj_list(self, count: int = 10): start = random.randrange(len(self.obj_list) - self.level) output = self.obj_list[start : start + self.level] state = self.obj_list[start : start + self.level] for i in range(count - self.level): choice = tools.weighted_choice(self.transitions[tuple(state)]) output.append(choice) state.append(choice) state.pop(0) return output
def add_edge(self): if len(self.nodes) >= 2: tries = 5 while tries > 0: [n1, n2] = random.sample(self.nodes, 2) if n1.depth > n2.depth: n1, n2 = n2, n1 # only if this is a feed-forward edge that does exist if n1.depth == n2.depth or [n1.id, n2.id ] in [[e.id_in, e.id_out] for e in self.genes]: tries -= 1 continue id = self.next_id() new_edge = weighted_choice([KernelGene, PoolGene, DenseGene], [1, 1, 1])(id, n1.id, n2.id) self.genes += [new_edge] self.genes_by_id[id] = new_edge break
def generate_obj_list(self, count=10): cache = queue.Queue(self.level) obj_list = [] # randomly generate the first word start = random.choice(self.obj_list) obj_list.append(start) cache.put(start) for x in range(1, count): pattern = [obj for obj in cache.queue] # returns longest pattern from history while not tuple(pattern) in self.pair_tb: pattern.pop() new_obj = tools.weighted_choice(self.pair_tb[tuple(pattern)]) obj_list.append(new_obj) # updates cache if cache.full(): cache.get() cache.put(new_obj) return obj_list
def init_optimizer(self): return weighted_choice([SGDGene, ADAMGene], [0.15, 0.85])()
def note_no(): chosen = tools.weighted_choice(tools.rhythm_choices) return int(chosen)
def add_after(self, id, id_in, id_out): return weighted_choice(*self.mutate_to)(id, id_in, id_out)
def mutate_random(self, exception=1): return weighted_choice(*self.mutate_to)(self.id, self.id_in, self.id_out)
def init_depth_mult(self): return weighted_choice([1, 2, 3], [0.7, 0.2, 0.1])
def init_depth_size_change(self): return weighted_choice([0, 1, 2, 5], [0.4, 0.3, 0.2, 0.1])