コード例 #1
0
    def rec(state, depth, stack_size):
        if clock() - start > time_limit:
            return
        
        s = state.get_score_abort()
        
        if depth <= 0 or stack_size <= 0:
            check(s)
            return

        preprocessed = preprocess_world(state)
        
        if upper_bound(preprocessed)-state.time <= best.score:
            return
        
        aggressive_preprocess(preprocessed)

        frozen_state = preprocessed.get_hash()
        old_score = visited.get(frozen_state)
        if old_score is not None and s <= old_score:
            return
        
        visited[frozen_state] = s
        
        check(s)
        
        zzz = 'LRUDW'
        num_commands = len(commands)
        next_steps = set()
        
        greedy = path_to_nearest_lambda_or_lift(state)
        if greedy is not None:
            greedy = [greedy[1]]
        else:
            greedy = []
            
        #greedy = []
        
        for cmds in chain(greedy, product(zzz, zzz)):
            new_state = state
            for cmd in cmds:
                if new_state.final_score is None:
                    new_state = new_state.apply_command(cmd)
                commands.append(cmd)
            
            if new_state.final_score is None:
                h = new_state.get_hash()
                if h not in next_steps:
                    next_steps.add(h)
                    if cmds in greedy:
                        new_depth = depth
                    else:
                        new_depth = depth-1
                    rec(new_state, new_depth, stack_size-1)
            else:
                check(new_state.final_score)
                
            for _ in cmds:
                commands.pop()
        assert num_commands == len(commands)
コード例 #2
0
def check_preprocessor(world, commands, verbose=True):

    w1 = world
    w2 = preprocess_world(w1)
    n = 0
    for cmd in commands:
        pw1 = w1
        pw2 = w2
        w1 = w1.apply_command(cmd)
        w2 = w2.apply_command(cmd)
        n += 1

        if w1.terminated or w2.terminated:
            break

    t1 = w1.terminated, w1.score
    t2 = w2.terminated, w2.score

    if t1 != t2:
        print
        pw1.show()
        pw2.show()
        print
        w1.show()
        print t1
        w2.show()
        print t2
        print commands[:n]

        assert False
コード例 #3
0
    def get_ub_and_cache_entry(self, world):
        '''
        return upper bound and CacheEntry;
        
        given world is raw (not even preprocessed);
        calls are cached
        '''
        assert not world.terminated
        
        raw_hash = world.get_hash()
        result = self.ub_ce_cache.get(raw_hash)
        if result is not None:
            self.stats['ub ce hit'] += 1
            ub, cache_entry = result
            return ub-world.time, cache_entry

        self.check_stop()

        preprocessed = preprocess_world(world)
        self.stats['preprocess'] += 1
        
        ub = upper_bound(preprocessed)
        
        aggressive_preprocess(preprocessed) #inplace
        
        key = preprocessed.get_hash()
        cache_entry = self.cache.get(key)
        if cache_entry is None:
            cache_entry = self.cache[key] = CacheEntry()
            cache_entry.time = 100000000
            cache_entry.command = None
            
        self.ub_ce_cache[raw_hash] = ub, cache_entry
        return ub-world.time, cache_entry
コード例 #4
0
    def rec(self, depth, stack_depth):
        self.stats['node'] += 1
        self.check()
        
        self.check_stop()
        
        if depth <= 0 or stack_depth <= 0 or self.state.terminated:
            return
        
        ub, cache_entry = self.get_ub_and_cache_entry(self.state)
        
        t = self.state.time
        if cache_entry.time <= t:
            self.stats['cache cut'] += 1
            return
        cache_entry.time = t

        if ub <= self.best_score:
            self.stats['ub cut'] += 1
            return
        
        current_state = self.state
        
        ia = []
        greedy_commands = []
        if cache_entry.command is not None:
            greedy_commands = [cache_entry.command]
        else:
            ia = interesting_actions(preprocess_world(current_state)) # TODO: remove preprocessing step
            
            if ia:
                greedy_commands = ia[:1]
                ia = ia[1:]
            
            #ptn = path_to_nearest_lambda_or_lift(current_state)
            #if ptn is not None:
            #    _, ptn = ptn
            #    assert len(ptn) > 0
            #    greedy_commands = [ptn]
        
        
        #ordinary_commands = list('LRUDW')
        ordinary_commands = [''.join(cmd) for cmd in product('LRUDW', repeat=1)]
        # TODO: remove those that lead to duplicates
        
        ordinary_commands.extend(ia)
        
        for g in greedy_commands:
            if g in ordinary_commands:
                ordinary_commands.remove(g)
                
        
        def visit_child(cmd, depth_delta):
            pe = PathEntry()
            pe.cache_entry = cache_entry
            pe.command = cmd
            
            self.state = current_state.apply_commands(cmd)
            self.path.append(pe)
            
            self.rec(depth-depth_delta, stack_depth-1)
            
            self.path.pop()
        
        
        for cmd in greedy_commands:
            visit_child(cmd, 0)
        
        children = {}
        for cmd in ordinary_commands:
            self.check_stop()
            t = current_state.apply_commands(cmd)
            if t.terminated:
                ub = t.score
            else:
                ub, _ = self.get_ub_and_cache_entry(t)
            children[cmd] = ub
            
        ordinary_commands = sorted(children, key=children.get, reverse=True)
        
        for cmd in ordinary_commands:
            visit_child(cmd, 1)
        
        self.state = current_state
コード例 #5
0
                xy = world.index_to_coords(idx)
                return min(dist(xy, world.index_to_coords(i[1])) for i in interesting)
            a = max(actions, key=dist_to_others)
            interesting.append(a)
            actions.remove(a)

    #print '---'
    #for a in interesting:
    #    print a, data[a[1]], world.index_to_coords(a[1])
        
    return [a[2] for a in interesting]
  
                
if __name__ == '__main__':

    from preprocessor import preprocess_world
    
    #world = World.from_file('../data/sample_maps/trampoline3.map')
    
    world = World.from_file('../data/maps_manual/simple_beard.map')
    
    world.show()
    
    #print path_to_nearest_lambda_or_lift(world)
    
    print interesting_actions(preprocess_world(world))