Beispiel #1
0
    def generate_seed(self):
        MAX_ATTEMPTS = 1000
        success = False

        for attempts in range(MAX_ATTEMPTS):
            self.shuffle()
            analyzer = Analyzer(self.data, self.allocation)
            if analyzer.success:
                if not self.settings.egg_goals:
                    success = True
                    break
                shift_success = self.shift_eggs_to_hard_to_reach(analyzer.reachable, analyzer.hard_to_reach_items)
                if shift_success:
                    analyzer = Analyzer(self.data, self.allocation)
                    if analyzer.success:
                        success = True
                        break

        if not success:
            fail('Unable to generate a valid seed after %d attempts.' % MAX_ATTEMPTS)

        print('Seed generated after %d attempts' % (attempts+1))
        
        # Generate Visualization and Print Output:
        if True:
            Analyzer(self.data, self.allocation, visualize=True)
            #self.allocation.print_important_item_locations()

        return self.allocation, analyzer
Beispiel #2
0
    def _compute_level_score(self, base_config_level):
        analyzer = self.analyzer
        goals = self.goals

        variables = self.data.generate_pessimistic_variables()

        score_multiplier = 1
        score = 0
        #print_ln('== Base %d ==' % base_config_level)
        for config_level in range(base_config_level, MAX_CONFIG_LEVEL):
            configure_variables(config_level, variables)
            reachable, unreachable, levels, new_variables = analyzer.analyze_with_variable_set(variables)
            #print_ln('level %d' % config_level)
            if goals.issubset(reachable):
                #print_ln('  pass - levels: %d' % len(levels))
                #print_ln('  average goal level: %f' % compute_average_goal_level(goals, levels))
                #for en, level in enumerate(levels):
                    #print_ln('LEVEL %.1f' % (en/2))
                    #print_ln(level)
                score += compute_average_goal_level(goals, levels)*score_multiplier
                break
            #print_ln('  fail - levels: %d' % len(levels))
            
            if config_level >= MAX_CONFIG_LEVEL-1: fail('Unable to reach goals at max config level')
            score += len(levels)/2 * score_multiplier
            score_multiplier += DIFFICULTY_CONFIGS[config_level+1].score_multiplier_diff
            variables = new_variables
        #print_ln('Final Score: %f' % score)

        return score
Beispiel #3
0
    def generate_seed(self):
        MAX_ATTEMPTS = self.settings.max_attempts
        success = False

        start_time = time.time()
        for attempts in range(MAX_ATTEMPTS):

            self.shuffle()
            analyzer = Analyzer(self.data, self.settings, self.allocation)
            if analyzer.success:
                if not self.settings.egg_goals:
                    success = True
                else:
                    shift_success, goal_eggs = self.shift_eggs_to_hard_to_reach(
                        analyzer.reachable, analyzer.hard_to_reach_items)
                    if shift_success:
                        analyzer = Analyzer(self.data,
                                            self.settings,
                                            self.allocation,
                                            goals=goal_eggs)
                        if analyzer.success:
                            success = True

            if success:
                difficulty_analysis = None
                if not self.settings.hide_difficulty or self.settings.min_difficulty > 0 or self.settings.max_sequence_breakability != None:
                    # Run difficulty analysis
                    if self.settings.egg_goals: goals = analyzer.goals
                    else: goals = analyzer.hard_to_reach_items
                    difficulty_analysis = DifficultyAnalysis(
                        self.data, analyzer, goals)

                if self.settings.min_difficulty > 0:
                    if difficulty_analysis.difficulty_score < self.settings.min_difficulty:
                        success = False

                if self.settings.max_sequence_breakability != None:
                    if difficulty_analysis.breakability_score > self.settings.max_sequence_breakability:
                        success = False

            if success:
                break

        if not success:
            fail('Unable to generate a valid seed after %d attempts.' %
                 MAX_ATTEMPTS)

        time_taken = time.time() - start_time
        time_string = '%.2f seconds' % (time_taken)

        print_ln('Seed generated after %d attempts in %s' %
                 (attempts + 1, time_string))

        # Generate Visualization and Print Output:
        if False:
            Analyzer(self.data, self.settings, self.allocation, visualize=True)
            self.allocation.print_important_item_locations()

        return self.allocation, analyzer, difficulty_analysis
Beispiel #4
0
def experiment(number_of_types,
               servers_in_each_type,
               sigma,
               number_of_time_steps,
               folder,
               test_num):
    subfolder = get_subfolder_name(number_of_types,
                                   servers_in_each_type,
                                   sigma,
                                   number_of_time_steps)
    folder_path = os.path.join(folder, subfolder)

    server_file = get_server_file_name(folder_path, test_num)
    server_types = ServerType.read(server_file)
    demand_file = get_demand_profile_name(folder_path, test_num)
    demand_profile = DemandProfile.read(demand_file)

    schedule, fractional = flow_based_algorithm(
        server_types, demand_profile, use_flow=False)
    print(server_file)
    print(f'  Fractional optimum: {fractional}')
    print(f'  Approximation:      {schedule.total_energy}')

    isolver = DPMSolver(server_types, demand_profile, variable_type='I')
    isolver.silent()
    integral = isolver.solve()
    isolver.verify_feasibility()
    print(f'  Integral optimum:   {integral}')

    if fractional > integral:
        print(fail('Values exceed the limit!'))
        sys.exit(-1)

    return schedule.total_energy / integral
Beispiel #5
0
def configure_variables(config_level, variables):
    config_flags = DIFFICULTY_CONFIGS[config_level].config_flags
    length_check_before = len(variables)
    variables.update(config_flags)
    if len(variables) != length_check_before:
        fail('Unknown config flags detected!')