Ejemplo n.º 1
0
    def allocate_register(self, register_slot):
        if not self.is_slot_available(register_slot):
            return False
        prop = self.game.proponent
        register_clean = str(prop.values[register_slot]) == 'I'
        
        terms = [goal.term for goal in self.goals]
        
        register_access = (cards.get, number_term(register_slot))
        register_cost = sequential_cost(register_access)
        sub_term, advantage = optimal_subterm(register_cost, *terms)
        
        if not register_clean:
            advantage -= 1
            # because we have to clean it up
        
        if advantage <= 0:
            return False
        
        sub_goal = Goal(sub_term, register_slot)
        
        self.introduce_get(sub_term, register_access)

        self.add_goal(sub_goal)
        
        return True
Ejemplo n.º 2
0
    def rec(cur, depth):
        global cnt
        scur = str(cur)

        if scur.isdigit() and scur not in optimized_numbers:
            t = terms.number_term(int(scur))
            cost = terms.sequential_cost(t)
            if cost > len(steps):
                print len(steps), steps_to_str(steps), "->", scur
            optimized_numbers.add(scur)

        if scur in desired:
            print len(steps), steps_to_str(steps), "->", scur
            desired.remove(scur)
            if len(desired) == 0:
                print "Done. Press enter to exit"
                raw_input()
                exit()
        if depth == 0:
            return
        cnt += 1
        if cnt % 500000 == 0:
            print cnt
        for i, (f, side) in enumerate(possible_steps):
            if len(steps) == 0 and i % num_parts != our_part:
                continue
            try:
                context.app_limit = 30
                if side == "r":
                    next = apply(cur, f, context)
                else:
                    next = apply(f, cur, context)
            except Error as e:
                continue
            # except Exception as e:
            #    print steps_to_str(steps+[(f, side)])
            #    print e
            #    raise e
            #    exit()
            steps.append((f, side))
            rec(next, depth - 1)
            steps.pop()
Ejemplo n.º 3
0
 def reuse_shit(self):
     assert self.current_goal is None
     
     prop = self.game.proponent
     terms = [goal.term for goal in self.goals]
     costs, _ = calc_costs_and_weights(*terms)
     for slot in slot_numbers_by_reachability:
         if prop.vitalities[slot] <= 0:
             continue
         value = prop.values[slot]
         if value == cards.I:
             continue # for speed
         shit = value_to_term(value)
         shit = unfold_numbers(shit)
         if shit in costs:
             access = (cards.get, number_term(slot))
             if sequential_cost(access) < costs[shit]:
                 if self.is_slot_available(slot):
                     self.introduce_get(shit, access)
             del costs[shit]
Ejemplo n.º 4
0
 def cost(self):
     return sequential_cost(self.term)
Ejemplo n.º 5
0
    
    def introduce_get(self, sub_term, register_access):
        g, register_slot = fold_numbers(register_access)
        assert isinstance(register_slot, int)
        assert g == cards.get
        
        self.gets.add(register_slot)
        
        if not is_subterm_eager(sub_term, self.term):
            self.lazy_gets.add(register_slot)
        self.term = replace_leaf_subterm(sub_term, register_access, self.term)


slot_numbers_by_reachability = sorted(
    range(rules.SLOTS), 
    key=lambda n: sequential_cost(number_term(n)))


class NetworkFail(Exception):
    pass


def global_optimize_network(network):
    best = None
    best_cost = 1e100
    orig = str(network)
    for reg_count in [1]+[3]*5:
        assert str(network) == orig
        t = network.clone()
        t.reg_count = reg_count
        t.refine()