示例#1
0
def get_steps_rank(grass_list):
    from main import SHOW_DETAIL_LOG

    steps = list()
    for g1 in grass_list:
        is_empty = False
        for g2 in grass_list:
            if g1.id == g2.id or g1.is_complete() or g2.is_complete():
                continue
            if is_empty and g2.is_empty():
                continue
            if g2.is_empty():
                is_empty = True

            step = StepItem(g1, g2)
            if step.can_pour_in_out():
                steps.append(step)

    ordered_steps = dict()
    for s in steps:
        score = s.evaluate(grass_list)
        ordered_steps[s] = score
    #     if SHOW_DETAIL_LOG:
    #         print '{0} into {1}, score:{2}'.format(s.original_glass.id, s.target_glass.id, score)
    # if SHOW_DETAIL_LOG:
    #     print '-'*30

    step_rank = sorted(ordered_steps.items(), key=lambda item: -item[1])
    from Utils import Stack
    step_stack = Stack()
    for s in step_rank:
        step_stack.push(s[0])

    return step_stack
示例#2
0
class Symbol_table(object):
    def __init__(self):
        self.table = []
        self.scope_stack = Stack()
        self.scope_stack.push(0)
        self.hide_upper_scopes = False

    def find(self, id_value):
        if not self.hide_upper_scopes:
            for item in reversed(self.table):
                if item.value == id_value:
                    return self.table.index(item)
        else:
            for item in self.table[self.scope_stack.top()]:
                if item.value == id_value:
                    return self.table.index(item)
        new_id = Symbol_table_row(id_value)
        self.table.append(new_id)
        return self.table.index(new_id)

    def push_scope(self):
        self.scope_stack.push(len(self.table))

    def pop_scope(self):
        while len(self.table) > self.scope_stack.top():
            self.table.pop()
        self.scope_stack.pop()
示例#3
0
    def __init__(self, file_name):
        self.symbol_table = Symbol_table()
        self.scanner = Scanner(file_name, self.symbol_table)
        self.error_handler = ErrorHandler(self.scanner)
        self.semantic_stack = Stack()
        self.last_token = None
        self.memory_manager = MemoryManager(start= 1000)
        self.semantic_analyzer = SemanticAnalyzer(semantic_stack=self.semantic_stack ,
                                                  memory_manager= self.memory_manager ,
                                                  symbol_table= self.symbol_table ,
                                                  error_handler= self.error_handler)
        self.code_generator = Code_generator(symbol_table=self.symbol_table,
                                             semantic_stack=self.semantic_stack,
                                             memory_manager=self.memory_manager)

        self.stack = [0]
        with open('parse_table.csv', 'r') as f:
            self.parse_table = [{k: v for k, v in row.items()}
                                for row in csv.DictReader(f, skipinitialspace=True)]
        self.next_token = None
        self.token_history = []
示例#4
0
 def __init__(self):
     self.table = []
     self.scope_stack = Stack()
     self.scope_stack.push(0)
     self.hide_upper_scopes = False
示例#5
0
 def __init__(self):
     self.id = generate_uid()
     self.colors = Stack()
     self.max_count = GLASS_MAX_SIZE
示例#6
0
class Glass(object):
    def __init__(self):
        self.id = generate_uid()
        self.colors = Stack()
        self.max_count = GLASS_MAX_SIZE

    @property
    def empty_count(self):
        size = 0
        for color in self.colors.get_all():
            size += color.count
        return self.max_count - size

    def show(self):
        print '\nglass_id', self.id
        is_empty = self.is_empty()
        if not is_empty:
            for c in self.colors.get_all():
                print 'color_id: {0}, count: {1}'.format(c.id, c.get_count())
        else:
            print 'is_empty'

    def is_empty(self):
        return self.empty_count == self.max_count

    def is_single_color(self):
        return self.colors.size == 1

    def is_full(self):
        return self.empty_count <= 0

    def is_complete(self):
        return self.empty_count == 0 and self.colors.size == 1

    def set_colors(self, colors):
        self.colors = colors

    def get_colors(self):
        return self.colors

    def get_color_by_index(self, index=0):
        """index 0为栈顶"""
        color = self.colors.get_data_by_index(index)
        return color

    def get_index_by_color_id(self, color_id):
        for idx, c in enumerate(self.colors.get_all()):
            if c.id == color_id:
                return idx
        return -1

    def can_pour_in(self, new_color_id):
        if self.is_empty():
            return True
        if self.is_full():
            return False

        top_color = self.get_color_by_index()
        return top_color.id == new_color_id

    def pour_in(self, new_color_id, count):
        if not self.can_pour_in(new_color_id):
            return False
        add_count = min(self.empty_count, count)
        top_color = self.get_color_by_index()
        if top_color is not None:
            top_color.add_count(add_count)
        else:
            c = Color(new_color_id, count)
            self.colors.push(c)
        return True

    def can_pour_out(self):
        return not self.is_empty()

    def pour_out(self, count):
        if not self.can_pour_out():
            return False
        top_color = self.get_color_by_index()
        if top_color is not None:
            top_color.sub_count(count)
            if top_color.count <= 0:
                self.colors.pop()
            return True
        return False
示例#7
0
def search_steps():
    global glass_dict
    steps_rank = get_steps_rank(glass_dict.values())
    if steps_rank.is_empty():
        return False
    step_stack = Stack()
    step_stack.push(steps_rank)

    total_bit = step_stack.stack[0].size
    old_bit = -1
    while not step_stack.is_empty():
        count = ''
        new_bit = step_stack.stack[0].size
        if new_bit != old_bit:
            old_bit = new_bit
            print 'calculating... {:.2f}%'.format(
                100.0 * (total_bit - new_bit) / total_bit)

        cur_steps = step_stack.get_top()
        if cur_steps.is_empty():
            step_stack.pop()
            revert()

        while not cur_steps.is_empty():
            step = cur_steps.pop()
            pour_res = glass_pour_to(step.original_glass.id,
                                     step.target_glass.id)
            if not pour_res:
                print 'pour fail', step.original_glass.id, step.target_glass.id
                return False

            if check_win(glass_dict.values()):
                return True

            steps_rank = get_steps_rank(glass_dict.values())
            if not steps_rank.is_empty():
                step_stack.push(steps_rank)
                break
            else:
                revert()
            if cur_steps.is_empty():
                step_stack.pop()
                revert()

    return False
示例#8
0
def create_glass(colors):
    global glass_dict
    glass = Glass()
    glass.set_colors(Stack(colors))
    glass_dict[glass.id] = glass
示例#9
0
            steps_rank = get_steps_rank(glass_dict.values())
            if not steps_rank.is_empty():
                step_stack.push(steps_rank)
                break
            else:
                revert()
            if cur_steps.is_empty():
                step_stack.pop()
                revert()

    return False


glass_dict = dict()
history = Stack()


def create_glass(colors):
    global glass_dict
    glass = Glass()
    glass.set_colors(Stack(colors))
    glass_dict[glass.id] = glass


if __name__ == '__main__':
    create_glass([Color(1, 2), Color(2), Color(3)])
    create_glass([Color(4), Color(5), Color(6), Color(7)])
    create_glass([Color(8), Color(9), Color(7), Color(10)])
    create_glass([Color(10), Color(9), Color(6), Color(10)])
    create_glass([Color(3), Color(11), Color(9), Color(2)])
示例#10
0
        print("0.1.0")
        exit()

    FLAGS = flags.FLAGS
    FLAGS(['run_sc2'])
    p = Path.cwd()

    i = 1
    while i < args.max_runs:
        print('RUN {}'.format(1))
        i += 1
        steps = 0
        x = 0
        qt = None
        states = None
        stack = Stack()
        stack_score = Stack()
        total_start_time = time.time()

        with sc2_env.SC2Env(players=[sc2_env.Agent(sc2_env.Race.terran)],
                            map_name=maps.get(args.map),
                            realtime=False,
                            agent_interface_format=[
                                features.parse_agent_interface_format(
                                    feature_screen=16,
                                    feature_minimap=16,
                                    rgb_screen=None,
                                    rgb_minimap=None)
                            ],
                            visualize=args.visualize) as env: