Skip to content

Commit

Permalink
removed newlines, tabs and spaces at end of lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirko Dietrich committed Mar 29, 2010
1 parent 4fea8ea commit d3f08b6
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 239 deletions.
127 changes: 63 additions & 64 deletions experiments/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# create tree function.
# -start from the root
# -choose an opcode
# -fill its parameters with
# -fill its parameters with
# if max_modules not reached
# => modules
# => else constant
Expand All @@ -35,72 +35,71 @@
# addition and multiplication can always be used

class Tree:
"""a class for representing the genome tree"""
def __init__(self, code):
"""Constructor
code describes an opcode
"""
self.code = code
self.sons = []
"""a class for representing the genome tree"""

def __init__(self, code):
"""Constructor
code describes an opcode
"""
self.code = code
self.sons = []


def get_only_type(the_type, opcodes):
"""get only opcodes the have output of the_type"""
return [op for op in opcodes if op["outtype"] == the_type]
"""get only opcodes the have output of the_type"""
return [op for op in opcodes if op["outtype"] == the_type]







if __name__ == '__main__':

# get list of available opcodes from json file
opcodes = json.loads(file("opcodes.json").read())

# select random root element
# TODO maybe this has to be constrained to outtype="a" type
root = Tree(random.choice(opcodes))

todo = deque([root])

while todo:
tmp_tree = todo.popleft()

print tmp_tree.code["name"]

# if it is a math operator
if tmp_tree.code["type"] == "math":
n_sons = random.randint(2, max_sons)
for i in range(n_sons):
if random.random() > constant_prob:
random_opcode = Tree(random.choice(opcodes))
tmp_tree.sons.append(random_opcode)
todo.append(random_opcode)
else:
bla = {"name": "const", "value": random.random() * max_rand_const}
root.sons.append(Tree(bla))
else:
for param in tmp_tree.code["params"]:
if random.random() > constant_prob:
filtered = get_only_type(param["type"], opcodes)
random_opcode = Tree(random.choice(opcodes))
tmp_tree.sons.append(random_opcode)
todo.append(random_opcode)
else:
bla = {"name": "const", "value": random.random() * max_rand_const}
root.sons.append(Tree(bla))


# tree to code












# get list of available opcodes from json file
opcodes = json.loads(file("opcodes.json").read())

# select random root element
# TODO maybe this has to be constrained to outtype="a" type
root = Tree(random.choice(opcodes))

todo = deque([root])

while todo:
tmp_tree = todo.popleft()

print tmp_tree.code["name"]

# if it is a math operator
if tmp_tree.code["type"] == "math":
n_sons = random.randint(2, max_sons)
for i in range(n_sons):
if random.random() > constant_prob:
random_opcode = Tree(random.choice(opcodes))
tmp_tree.sons.append(random_opcode)
todo.append(random_opcode)
else:
bla = {"name": "const", "value": random.random() * max_rand_const}
root.sons.append(Tree(bla))
else:
for param in tmp_tree.code["params"]:
if random.random() > constant_prob:
filtered = get_only_type(param["type"], opcodes)
random_opcode = Tree(random.choice(opcodes))
tmp_tree.sons.append(random_opcode)
todo.append(random_opcode)
else:
bla = {"name": "const", "value": random.random() * max_rand_const}
root.sons.append(Tree(bla))


# tree to code










54 changes: 26 additions & 28 deletions sound_evolution/genetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@
import instrument

class Individual(object):
"""A class representing an individual."""
__metaclass__ = abc.ABCMeta

@abc.abstractmethod
def mutate(self):
"""Mutate an individual."""
return

@abc.abstractmethod
def ficken(self, individual=None):
"""Cross an individual with another one."""
return

@abc.abstractmethod
def fitness(self):
"""Score of the individual."""
return

@abc.abstractmethod
def random(params):
"""Generate a random individual."""
return

"""A class representing an individual."""
__metaclass__ = abc.ABCMeta

@abc.abstractmethod
def mutate(self):
"""Mutate an individual."""
return

@abc.abstractmethod
def ficken(self, individual=None):
"""Cross an individual with another one."""
return

@abc.abstractmethod
def fitness(self):
"""Score of the individual."""
return

@abc.abstractmethod
def random(params):
"""Generate a random individual."""
return


class Population(object):

def __init__(self, size, cls, params):
Expand All @@ -35,7 +35,5 @@ def __init__(self, size, cls, params):
self.individuals = [cls.random(params) for i in range(size)]

def next_generation(self):
"""Create the next generation from current population."""
pass


"""Create the next generation from current population."""
pass
70 changes: 35 additions & 35 deletions sound_evolution/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
import simplejson as json
import random
from collections import deque

from genetics import Individual

class Instrument(object):
"""A class representing the genome tree."""

def __init__(self, instrument_tree=None):
""" Create a new Instrument from a json string """
if type(instrument_tree) is str:
self.instrument_tree = json.loads(instrument_tree)
else:
self.instrument_tree = instrument_tree


def to_instr(self):
"""Generate csound ocr code."""
n = 0
(c, d, n) = self.__class__.__to_instr(self.instrument_tree, n)
return c + "\n" + "out\ta%d" % n

@staticmethod
def __to_instr(node, n):
csound_code = ""
Expand All @@ -35,9 +35,8 @@ def __to_instr(node, n):
data += (d,)
tmp_n += n
(c, d, n) = Instrument.__render(node, data, tmp_n)
return (csound_code + "\n" + c, d, n)


return (csound_code + "\n" + c, d, n)

@staticmethod
def __render(node, data, n):
"""render the code for a node"""
Expand All @@ -61,34 +60,34 @@ def mutate(self):
return

def ficken(self, individual=None):
"""Cross a tree-instrument with another one."""
return
"""Cross a tree-instrument with another one."""
return

def fitness(self):
"""Score of the instrument."""
return

@staticmethod
def random(params):
"""create a random instrument"""

const_probability = params.get("const_prob")
max_children = params.get("max_children")
max_children = params.get("max_children")

def get_only_type(the_type, opcodes):
"""get only opcodes the have output of the_type"""
return [op for op in opcodes if op["outtype"] == the_type]
"""get only opcodes the have output of the_type"""
return [op for op in opcodes if op["outtype"] == the_type]


# get list of available opcodes from json file_
opcodes = json.loads(file(os.path.join(os.path.dirname(__file__), "opcodes.json")).read())

# select random root element
# select random root element
# TODO maybe this has to be constrained to outtype="a" type
root = Instrument.__make_node(random.choice(opcodes))
todo = deque([root])
# TODO this number has to be replaced by the may value of the opcode with

# TODO this number has to be replaced by the may value of the opcode with
# which it is used
max_rand_const = 100

Expand All @@ -97,7 +96,7 @@ def get_only_type(the_type, opcodes):

# if it is a math operator
if tmp_tree["code"]["type"] == "math":

n_children = random.randint(2, max_children)
for i in range(n_children):
if random.random() > const_probability:
Expand All @@ -108,7 +107,7 @@ def get_only_type(the_type, opcodes):
random_node = Instrument.__make_node(const_code)

tmp_tree["children"].append(random_node)

else:
for param in tmp_tree["code"]["params"]:
if random.random() > const_probability:
Expand All @@ -120,35 +119,36 @@ def get_only_type(the_type, opcodes):
random_node = Instrument.__make_node(const_code)

tmp_tree["children"].append(random_node)

inst = Instrument()
inst.instrument_tree = root
return inst
@staticmethod

@staticmethod
def __make_node(code):
"""Make a node with no children."""
return { "code": code, "children": []}

@staticmethod
def __make_const_code(val):
"""make a new constant"""
return {"name": "const", "type": "const", "value": str(val)}

def mutate(self):
"""Mutate an instrument."""
return
def mutate(self):
"""Mutate an instrument."""
return

def ficken(self, individual=None):
"""Cross a tree-instrument with another one."""
return
def ficken(self, individual=None):
"""Cross a tree-instrument with another one."""
return

def fitness(self):
"""Score of the instrument."""
return

def fitness(self):
"""Score of the instrument."""
return

Individual.register(Instrument)



if __name__ == '__main__':
comp = open("../tests/fixtures/complex_instrument.json").read()
i = Instrument(comp)
Expand Down
Loading

0 comments on commit d3f08b6

Please sign in to comment.