def _calc_info(self, genes, expr_ptn, bound=False): assert len(genes) == len(expr_ptn) data = Expression(*self.expression.subset(genes), binarize=False) p_c = [float(data.num_samples(c)) / data.num_samples() for c in self.C] assert sum(p_c) == 1 num_c_given_f = [0 for c in self.C] for c in self.C: num_f = 0 equality = (expr_ptn == data.sample_gene).all(axis=1) for i, equal in enumerate(equality): if equal: num_f += 1 if data.labels[i] == c: num_c_given_f[c] += 1 if num_f == 0: return 0 num_f = float(num_f) s = 0 if bound: s = max((num_c_given_f[c] / num_f) * np.log(1 / p_c[c]) if p_c[c] != 0 else 0 for c in self.C) else: s = sum((num_c_given_f[c] / num_f) * np.log((num_c_given_f[c] / num_f) / p_c[c]) if num_c_given_f[c] != 0 and p_c[c] != 0 else 0 for c in self.C) p_f = num_f / data.num_samples() retval = p_f * s assert retval >= 0.0 return retval
def main(): engine = create_engine("sqlite:///doyouevenmath.db") Base.metadata.create_all(engine) session = Session(bind=engine) expression = Expression() manager = DBManager(session) print(''' Welcome to the "Do you even math?" game! Here are your options: - start - highscores''') command = input("Enter> ") command = command.split(" ") while command[0] != 'end': if command[0] == "start": username = input("Enter your name: ") while True: result = expression.generate_math_expr() answer = float(input("Your answer is: ")) if expression.solve_math_expr(result, answer) is False: manager.commit_in_db(username, expression.return_result()) break if command[0] == "highscores": manager.show_highscores() command = input("Enter> ") command = command.split(" ")
def main(): x_array = linspace(0, 2*math.pi, 100) t_array = array([0, 1, 2, 3]) data = zeros((len(x_array), len(t_array))) def func(x, t): return math.sin(x*t) # expression_factory = FunctionExpressionFactory() # y = expression_factory.create(x_array=x_array, t_array=t_array, func=func) # # # expression_factory = DataExpressionFactory() # # y = expression_factory.create(x_array=x_array, t_array=t_array, data=data) # # y = Expression(x_array, t_array, data) # y.plot() from expression import Expression y = Expression(x_array=x_array, t_array=t_array, data=data) y = Expression.from_func(x_array=x_array, t_array=t_array, func=func) y.plot(t=1) y.plot(x=0)
def test_implicit_multiplication(self): """ The query "(x^k)/k! exp(-x)" fails because the implicit multplication function in the preparser doesn't handle the "!" """ expression = Expression("(x^k)/k! exp(-x)") expression.taylor_series()
def ask_question(self): expression = Expression() print("Question #%s" % (str(self.correct_answers + 1))) print("What is the answer to %s?" % (expression)) answer = input("Answer: ") if int(answer) != expression.calculate(): print("You stupid m**********r!") return False else: self.correct_answers += 1 return True
def series(): """ Endpoint to get the taylor series expansion of a function """ query = request.args.get('query') if query: # TODO later we're going to want to return some JSON to the frontend # TODO user input might not be something we can actually process, we # should handle that case (ideally help the user figure out what we # don't understand) expression = Expression(query) return expression.taylor_series() else: return ""
def start(session): print("Dividing numbers rounds them to second decimal!") points = 0 username = input("Enter your playername>") user = User(name=username, score=points) print("Welcome {}! Let the game begin!".format(username)) last_answer_correct = True while(last_answer_correct): expression = Expression.generate_expression() print("What is the answer to {} {} {}".format( expression[0], expression[2], expression[1])) answer = input("?>") if(float(answer) == expression[3]): print("?>Correct!") points += 1 else: score = calculate_score(points) print("Incorrect! Ending game. You score is: {}".format(score)) last_answer_correct = False if user.score < score: user.score = score session.query(User).filter(User.name==username).update({"score": score}) if(session.query(exists().where(User.name == username)).scalar() == 0): session.add(user) session.commit()
class TemplateString: """Template string converter""" pattern = re.compile(r"{{\s*(.*?)\s*}}") def __init__(self, output): self.output = output self.expression = None def _match(self, match): self.expression = Expression(match.group(1), output=self.output) self.expression.run() def run(self, data): self.expression = None self.__class__.pattern.sub(self._match, data) if self.expression is None: self.output.write('"' + b(data) + '"')
def setUp(self): root = Node(Operations.PLUS, Node(Operations.MULTIPLICATION, left=Node(Operations.IDENTITY, value='x'), right=Node(Operations.NUMBER, value=4)), Node(Operations.MULTIPLICATION, left=Node(Operations.IDENTITY, value='y'), right=Node(Operations.NUMBER, value=2))) self.f = Expression(root=root, variables=['x', 'y'])
def _calc_mutual_info(self): data = Expression(*self.expression.subset(self.genes), binarize=False) counts_C = np.histogram(data.labels, bins=[0,1,2])[0] num_genes = data.num_genes() bins = [[0,1,2] for i in xrange(num_genes)] counts_E, _ = np.histogramdd(data.sample_gene, bins=bins) counts_E = counts_E.ravel() # hack using binary to decimal conversion samples_as_decimals = [int(''.join(sample.astype('str')), base=2) for sample in data.sample_gene] counts_CE = np.histogram2d(data.labels, samples_as_decimals, bins=([0,1,2], range(2**num_genes + 1)))[0] H_CE = self._entropy(counts_CE) H_C = self._entropy(counts_C) H_E = self._entropy(counts_E) return H_C + H_E - H_CE
def __init__(self, ast_node, func): self.func = func self.ast_node = ast_node self.asm = "" # Assignment if ast_node.token.text == "ASSIGN": # Get the variable and expression var, expr = ast_node.children var = func.getVar(var.text) # Calculate the answer expr = Expression(expr, func) factor = expr.evaluate() # Add the calculations to the function body self.asm += str(expr) self.asm += self.header() self.asm += var.assign(factor, func.prog.next_loop.next()) if ast_node.token.text == "IF": cond, true_body, false_body = ast_node.children idnum = self.func.prog.next_loop.next() self.true = ".truebranch%s" % idnum self.false = ".falsebranch%s" % idnum self.asm += IF_ASM.format( cond = Condition(cond, self), id = idnum, true_body = ''.join(map(str, [Statement(s, self.func) for s in true_body.children])), false_body = ''.join(map(str, [Statement(s, self.func) for s in false_body.children])) ) if ast_node.token.text == "WHILE": cond, body = ast_node.children idnum = self.func.prog.next_loop.next() self.true = ".loopbegin%s" % idnum self.false = ".loopexit%s" % idnum self.asm += WHILE_ASM.format( id = idnum, cond = Condition(cond, self), body = ''.join(map(str, [Statement(s, self.func) for s in body.children])) )
def expression_value(self, expression:Expression): """ Returns value of the fitness function for given expression. The less the value - the closer expression to the unknown function. """ sum = 0 for (variables, value) in self.exact_values: sum += ((expression.value_in_point(variables) - value) * (expression.value_in_point(variables) - value)) return math.sqrt(sum)
def subtree_mutation(self): """ Changes one randomly selected node to the randomly generated subtree. The height of the tree isn't changed. """ nodes = self._get_all_nodes_by_filter(lambda n: n.height() > 1 and n != self.expression.root) if not nodes: return selected_node = random.choice(nodes) max_height = self.expression.root.height() - selected_node.height() new_subtree = Expression.generate_random(max_height, self.expression.variables) selected_node.operation = new_subtree.root.operation selected_node.value = new_subtree.root.value selected_node.left = new_subtree.root.left selected_node.right = new_subtree.root.right
class ExpressionMutatorTest(unittest.TestCase): def setUp(self): root = Node(Operations.PLUS, Node(Operations.MULTIPLICATION, left=Node(Operations.IDENTITY, value='x'), right=Node(Operations.NUMBER, value=4)), Node(Operations.MULTIPLICATION, left=Node(Operations.IDENTITY, value='y'), right=Node(Operations.NUMBER, value=2))) self.f = Expression(root=root, variables=['x', 'y']) def test_number_mutation(self): mutator = ExpressionMutator(expression=self.f) mutator.number_mutation() point = {'x': 1, 'y': 2} original_value = self.f.value_in_point(point) mutated_value = mutator.expression.value_in_point(point) self.assertNotEqual(original_value, mutated_value)
def ask_questions(name, session): question_number = 1 while True: print("Question #{}".format(question_number)) expression = Expression.generate_expression() print("What is the answer to {}".format(expression)) answer = expression.solve_expression() while True: user_answer = input("answer> ") if user_answer != "": user_answer_to_number = float(user_answer) break print("Please enter an answer!") if check_answer(user_answer_to_number, answer) is True: print("Correct") question_number += 1 else: break score = (question_number - 1) * (question_number - 1) print("Incorrect! Ending game. You score is: {}".format(score)) save_result(name, score, session)
def test_solve_is_not_crashing(self): values = [] for i in range(0,5): x = i values.append(({'x': x}, x * x )) f = FitnessFunction(values) exchanger = SimpleRandomExchanger( lambda: [Expression.generate_random(max_height=2, variables=['x']) for i in range(0, 5)]) config = ExpressionsImmuneSystemConfig() config.number_of_lymphocytes = 10 config.number_of_iterations = 5 immuneSystem = ExpressionsImmuneSystem(exact_values=values, variables=['x'], exchanger=exchanger, config=config) best = immuneSystem.solve() self.assertGreaterEqual(f.expression_value(best), 0)
def __init__(self, exact_values, variables, exchanger, config): """ Initializes the immune system with the exact_values, list of variables, exchanger object and config object. lymphocytes - list that stores current value of the whole system. """ self.exact_values = exact_values self.variables = variables self.fitness_function = FitnessFunction(exact_values) self.exchanger = exchanger # config self.config = config self.lymphocytes = [] for i in range(0, self.config.number_of_lymphocytes): self.lymphocytes.append(Expression.generate_random(self.config.maximal_height, variables)) # Initialize Exchanger with the first generated lymphocytes self.exchanger.set_lymphocytes_to_exchange(self.lymphocytes[:]) random.seed()
def test_expr_with_right_parenthesis_followed_by_an_operator(self): expr = Expression("(12+34)*10") self.assertEquals(460, expr.eval())
def test_expr_ends_with_right_parenthesis(self): expr = Expression("10*(12+34)") self.assertEquals(460, expr.eval())
def main(): exp = Expression("2 / 3 * 4") print exp.solve(None)
def test_expr_with_right_parenthesis_followed_by_right_parenthesis(self): expr = Expression("(48-(12+34))*10") self.assertEquals(20, expr.eval()) expr = Expression("10*(48-(12+34))") self.assertEquals(20, expr.eval())
def test_expr_with_nested_parentheses(self): expr = Expression("((10+21)+34)*20") self.assertEquals(1300, expr.eval()) expr = Expression("(50-(12+34))*20") self.assertEquals(80, expr.eval()) expr = Expression("((11+23)%11)*20") self.assertEquals(20, expr.eval()) expr = Expression("(12-(15-12))*10/(24-(12+10))/3") self.assertEquals(15, expr.eval()) expr = Expression("(100-(1920-1900))*2/(24-(12+10))/2") self.assertEquals(40, expr.eval())
def test_expr_has_multiple_operators(self): expr = Expression("12+34*10") self.assertEquals(352, expr.eval())
def test_num(self): expr = Expression("12") self.assertEquals(12, expr.eval())
from expression import Expression, Context ctx = Context() expr_in = 0 while expr_in != 'exit': expr_in = input() if expr_in == 'exit': break eq_pos = expr_in.find('=') if eq_pos == -1: expr = Expression(expr_in, ctx) if str(expr) != 'error': print(expr) else: func = expr_in[0:eq_pos - 1] name = func[:-3] symbol = func[-2] ctx.add_func(name, expr_in[eq_pos + 2:], symbol) # remember our new symbol # then try to plot it: # Expression: # try to parse it in f_parser # create new Expression # simplify # plot
Shows progress bar. Progress is passed in percent. """ print '\r[{0}] {1}%'.format('#' * (progress // 10), progress) if __name__ == "__main__": number_of_lymphocytes = 100 max_height = 4 DataFileStorageHelper.save_to_file('test_x_y.txt', ['x', 'y'], lambda x, y: x * x + x * y * math.sin(x * y), 100) variables, values = DataFileStorageHelper.load_from_file('test_x_y.txt') f = FitnessFunction(values) exchanger = SimpleRandomExchanger( lambda: [Expression.generate_random(max_height=max_height, variables=variables) for i in range(0, number_of_lymphocytes // 2)]) config = ExpressionsImmuneSystemConfig() results = [] iterations = 5 start = time.clock() for i in range(0, iterations): immuneSystem = ExpressionsImmuneSystem(exact_values=values, variables=variables, exchanger=exchanger, config=config) best = immuneSystem.solve() results.append((f(best), str(best))) update_progress(int((i + 1) / iterations * 100))
def _match(self, match): self.expression = Expression(match.group(1), output=self.output) self.expression.run()
def make_pmd_model(raw): """ Create an instance of PMDModel from raw data parsed from .pmd file by pymio.parser.pmd_parser.PMDParser. """ model = PMDModel() from vertex import Vertex from pymio.vector3 import Vector3 from pymio.uv import Uv for i in xrange(len(raw["Vertex"])): vertex_info = raw["Vertex"][i] weight_info = raw["Weight"][i] position = Point3(vertex_info[0], vertex_info[1], vertex_info[2]) normal = Vector3(vertex_info[3], vertex_info[4], vertex_info[5]) tex_coord = Uv(vertex_info[6], vertex_info[7]) model.append_vertex(Vertex(position, normal, tex_coord, weight_info[0], weight_info[1], weight_info[2]/100.0)) model.triangle_count = len(raw["Poly"]) / 3 for index in raw["Poly"]: model.append_triangle_vertex_index(index) from material import Material from pymio.rgba import Rgba for i in xrange(len(raw["Material"])): material_info = raw["Material"][i] diffuse = Rgba(material_info[0], material_info[1], material_info[2], material_info[3]) shininess = material_info[4] specular = Rgba(material_info[5], material_info[6], material_info[7], diffuse.a) ambient = Rgba(material_info[8], material_info[9], material_info[10], diffuse.a) shader_number = material_info[11] shader_edge_number = material_info[12] triangle_count = material_info[13] / 3 texture_filename = material_info[14] model.append_material(Material(ambient, diffuse, specular, shininess, shader_number, shader_edge_number, triangle_count, texture_filename)) from bone import Bone for i in xrange(len(raw["Bone"])): bone_info = raw["Bone"][i] name = bone_info[0] parent_index = bone_info[1] tip_index = bone_info[2] bone_type = bone_info[3] ik_bone_index = bone_info[4] position = Point3(bone_info[5], bone_info[6], bone_info[7]) model.append_bone(Bone(name, bone_type, position, parent_index, tip_index, ik_bone_index)) from ik_chain import IKChain # [ ( ik,target,p1,p2,[node]) ] for i in xrange(len(raw["IK"])): ik_chain_info = raw["IK"][i] ik_bone_index = ik_chain_info[0] end_effector_index = ik_chain_info[1] affected_bone_indices = ik_chain_info[4] constant_0 = ik_chain_info[2] constant_1 = ik_chain_info[3] model.append_ik_chain(IKChain(ik_bone_index, end_effector_index, affected_bone_indices, constant_0, constant_1)) from expression import Expression from vertex_modification import VertexModification # [ ( name,type,[vertex]) ] for i in xrange(len(raw["Skin"])): skin_info = raw["Skin"][i] name = skin_info[0] vertices = skin_info[2] expression = Expression(name) for vertex in vertices: expression.append_vertex_modification(VertexModification(vertex[0], Vector3(vertex[1], vertex[2], vertex[3]))) if i > 0: model.append_expression(expression) else: model.base_expression = expression return model
def get_queries(self, question): """ Given `question` in natural language, it returns :type self: object three things: - the target of the query in string format - the query - metadata given by the regex programmer (defaults to None) The queries returned corresponds to the regexes that match in weight order. """ """ parse the question so that we split it if it hase "or" or "and" in it *( maybe on "," also at a later date) """ question = encoding_flexible_conversion(question) questions = question.split(" and ", (-1)) expr = Expression() first_time = True index = 0 #print questions toBeReturned = ReturnModel(None, None) for question in questions: for expression, userdata in self._iter_compiled_forms(question): if first_time: #print expression.rule_used toBeReturned.rule_used = expression.rule_used #print userdata """ -- it wont work for all the question or it will but we need more parrsing -- base on the type of the expression.rule_used we can take actions to connect the next expressions to the curent one if they are more """ if len(questions) > (index + 1): if expression.rule_used == "WhoAreChildrensOfQuestion": print "**************=Next query=**************************" temp_data = question.split(" ", (-1)) print temp_data questions[index+1] = _new_query_string(temp_data, questions[index+1], userdata.i, userdata.j) print(questions[index+1]) message = u"Interpretation {1}: {0}" print(message.format(str(expression), expression.rule_used)) first_time = False expr = expression expr.rule_used = expression else: """ will have to see if there is more to parse form question if there are mor conditions in order to make the next question base on the first query ! """ expr += expression index += 1 target, query = generation.get_code(expr, self.language) toBeReturned.query = query yield toBeReturned print(u"Query generated: {0}".format(query))
def mutate_expression(expression, rate, functions, variables): # create list of possible mutations WRAP, REPLACE, MUTATE_LITERAL, MUTATE_OP, PULL_OUT_OP, CHANGE_F = range(6) # expression's can always be wrapped or replaced possible_mutations = [WRAP, REPLACE] # what type of thing does this expression need to return after mutation desired_type = None # is this a literal, variable ref or bigger expression expression_type = type(expression.func) # work out what type of expression we're mutating and what sorts of # mutations are valid dependant on expression type if expression_type is Variable: desired_type = expression.func.typ elif expression_type is Literal: desired_type = expression.func.typ possible_mutations += [MUTATE_LITERAL] elif expression_type is Function: desired_type = expression.func.type_sig.return_type possible_mutations += [CHANGE_F, MUTATE_OP, PULL_OUT_OP] mutated_exp = deepcopy(expression) while mutated_exp == expression: # choose the mutation type chosen_mutation = choice(possible_mutations) if chosen_mutation == WRAP: # Find functions that take expression type we're wrapping as input # and retunr something of same type that wraps it def has_input_and_return_type(f): return (len(f.type_sig.input_types) > 0 and f.type_sig.input_types[0] == desired_type and f.type_sig.return_type == desired_type) function = choice(filter(has_input_and_return_type, functions)) operands = [expression] for input_type in function.type_sig.input_types[1:]: operands.append( grow_random_expression_tree( functions, variables, input_type, 3)) mutated_exp = Expression(function, *operands) if chosen_mutation == REPLACE: mutated_exp = grow_random_expression_tree( functions, variables, desired_type, 3) if chosen_mutation == MUTATE_LITERAL: mutated_exp.mutate(rate) if chosen_mutation == MUTATE_OP: op_to_mutate = randrange(len(expression.operands)) expression.operands[op_to_mutate] = mutate_expression( expression.operands[op_to_mutate], rate, functions, variables) mutated_exp = expression if chosen_mutation == PULL_OUT_OP: op_to_pull_out = randrange(len(expression.operands)) mutated_exp = expression.operands[op_to_pull_out] if chosen_mutation == CHANGE_F: mutated_exp.func = choice(filter( lambda f: f.type_sig == expression.func.type_sig, functions)) return mutated_exp
def test_expr_has_one_operator(self): expr = Expression("12+34") self.assertEquals(46, expr.eval())
def __init__(self): Expression.__init__(self)