Beispiel #1
0
    def get_plan_for_sample(self,
                            inflow,
                            curr_phase,
                            curr_phase_duration,
                            output_file=None):

        inflow_id = 0
        self.phase_intervals = {}
        self.cluster_intervals = {}

        self.model = docplex.cp.model.CpoModel()

        self.model.parameters = CpoParameters(LogVerbosity='Quiet',
                                              Workers=self.threads,
                                              TimeLimit=self.timelimit,
                                              WarningLevel=0,
                                              TimeMode='CPUTime')

        self.create_phase_intervals(curr_phase, curr_phase_duration)

        self.create_cluster_intervals(inflow)

        self.constrain_phase_order()
        self.constrain_cycle_order()

        self.constrain_cluster_departure(inflow)
        self.constrain_cluster_precedence(inflow)

        self.add_objective([inflow], [1])

        if output_file is not None:
            self.model.export_model(output_file)

        try:
            solution = self.model.solve()
        except LocalSolverException:
            solution = None
            self.model.export_model('localsolverexception.cpo')
            print('LocalSolverException raised!')

        if solution:
            plan = {}

            for cycle in range(self.cycle_count):
                for phase in self.phases:
                    var_sol = solution.get_var_solution(
                        self.phase_intervals[(phase, cycle)])
                    plan[(cycle, phase)] = (var_sol.get_start(),
                                            var_sol.get_end())

        else:
            self.model.export_model('nosolution.cpo')
            print('No solution')
            plan = None

        return plan
Beispiel #2
0
def resolve_exact(filename):
    searchtype = "Restart"
    interference = "Low"
    timelimit = 500000
    params = CpoParameters(TimeLimit=10, LogPeriod=100000)

    sol, time_cplex = cplex_solve_instance2(filename, 4, searchtype,
                                            interference, timelimit)

    return sol.get_value("makespan"), time_cplex
Beispiel #3
0
    def schedule_inflow_according_to_plan(self,
                                          inflow,
                                          plan,
                                          output_file=None):

        inflow_id = 0
        self.phase_intervals = {}
        self.cluster_intervals = {}

        self.model = docplex.cp.model.CpoModel()

        self.model.parameters = CpoParameters(LogVerbosity='Quiet',
                                              Workers=self.threads,
                                              TimeLimit=self.timelimit,
                                              WarningLevel=0,
                                              TimeMode='CPUTime')

        self.initialize_phase_schedule(plan)

        self.create_cluster_intervals(inflow)

        self.constrain_phase_order()
        self.constrain_cycle_order()

        self.constrain_cluster_departure(inflow)
        self.constrain_cluster_precedence(inflow)

        self.add_objective([inflow], [1])

        if output_file is not None:
            self.model.export_model(output_file)

        try:
            solution = self.model.solve()
        except LocalSolverException:
            solution = None
            self.model.export_model('localsolverexception.cpo')
            print('LocalSolverException raised!')

        if solution:
            (delay, ) = solution.get_objective_values()
        else:
            self.model.export_model('nosolution.cpo')
            print('No solution')
            delay = float('inf')

        return delay
Beispiel #4
0
context.model.cache = Context()
context.model.cache.size = 10000
context.model.cache.active = True

#-----------------------------------------------------------------------------
# Parsing context

context.parser = Context()

# Indicate to FZN parser to reduce model when possible
context.parser.fzn_reduce = False

#-----------------------------------------------------------------------------
# Solving parameters

context.params = CpoParameters()

# Default time limit in seconds (None for no limit)
context.params.TimeLimit = None

# Workers count (None for number of cores)
context.params.Workers = None

#-----------------------------------------------------------------------------
# Solving context

context.solver = Context()

# Indicate to trace CPO model before solving
context.solver.trace_cpo = False
Beispiel #5
0
                           DefaultInferenceLevel=interference).solve()
    end = time.time()
    #sol.print_solution()
    #display_sol (sol)

    return sol, end - start


#test PPC solver (CPLEX)
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
filename = ROOT_DIR + '/../instances/ft10'

searchtype = "Restart"
interference = "Low"
timelimit = 500000
params = CpoParameters(TimeLimit=10, LogPeriod=100000)

sol, time_cplex = cplex_solve_instance2(filename, 4, searchtype, interference,
                                        timelimit)

print("instance:", filename)
print("time: ", time_cplex)
print("makespan: ", sol.get_value("makespan"))


def resolve_exact(filename):
    searchtype = "Restart"
    interference = "Low"
    timelimit = 500000
    params = CpoParameters(TimeLimit=10, LogPeriod=100000)
    model.start_eval(task_int, energy_intervals_array[task['duration'] - 1]) *
    task['power_consumption'] for task, task_int in task_intervals
])

# Add resource capacity constraints
for machine in data['machines']:
    m_id = machine['id']
    for j in range(NUM_RESOURCES):
        model.add(
            machine_resources[m_id][j] <= machine['resource_capacities'][j])

# Add minimize constraint
model.add(model.minimize(cost_tasks))

msol = model.solve(params=CpoParameters(TimeLimit=300,
                                        SearchType='IterativeDiving',
                                        LogVerbosity='Terse'),
                   trace_log=True)
msol.print_solution()

# Phase 2
#
# This phase starts from a minimal (or relatively minimal,
# depending on the timeout of the first phase) solution
# that will be further minimized with the additional machine
# costs.

# This solution is consistent with the second phase,
# set it as starting point
model.set_starting_point(msol.get_solution())
bounds = msol.get_objective_bounds()
Beispiel #7
0
    def schedule_over_all_samples(self,
                                  inflows,
                                  weights,
                                  curr_phase,
                                  curr_phase_duration,
                                  output_file=None,
                                  status_file=None):

        self.phase_intervals = {}
        self.cluster_intervals = {}

        self.model = docplex.cp.model.CpoModel()

        self.model.parameters = CpoParameters(LogVerbosity='Terse',
                                              Workers=self.threads,
                                              TimeLimit=self.timelimit,
                                              WarningLevel=0,
                                              TimeMode='CPUTime')

        self.create_phase_intervals(curr_phase, curr_phase_duration)

        for inflow_id, inflow in enumerate(inflows):
            self.create_cluster_intervals(inflow, inflow_id)

        self.constrain_phase_order()
        self.constrain_cycle_order()

        for inflow_id, inflow in enumerate(inflows):
            self.constrain_cluster_departure(inflow, inflow_id)
            self.constrain_cluster_precedence(inflow, inflow_id)

        self.add_objective(inflows, weights)

        if output_file is not None:
            self.model.export_model(output_file)

        try:
            solution = self.model.solve()
        except LocalSolverException:
            solution = None
            self.model.export_model('localsolverexception.cpo')
            print('LocalSolverException raised!')

        if solution:
            # TODO(srishti): Simplify it
            departures = []
            (delay, ) = solution.get_objective_values()
            (gap, ) = solution.get_objective_gaps()

            for inflow_id, inflow in enumerate(inflows):
                sample_departures = {}

                for phase, sequence in inflow.items():
                    if phase not in sample_departures:
                        sample_departures[phase] = []

                    for cluster_id, cluster in enumerate(sequence):
                        cluster_departures = []

                        for cycle in range(self.cycle_count):
                            var_sol = solution.get_var_solution(
                                self.cluster_intervals[(inflow_id, phase,
                                                        cluster_id, cycle)])
                            if var_sol.get_start(
                            ) is not None and var_sol.get_size() > 0:
                                departure = SliceDeparture(
                                    departure=var_sol.get_start(),
                                    ratio=var_sol.get_size() /
                                    cluster.duration)
                                cluster_departures.append(departure)

                        sample_departures[phase].append(cluster_departures)

                departures.append(sample_departures)

            if status_file:
                with open(status_file, 'w') as fp:
                    json.dump(
                        {
                            'status': solution.solve_status,
                            'delay': delay,
                            'gap': gap
                        },
                        fp,
                        indent=4)

            curr_phase_solution = solution.get_var_solution(
                self.phase_intervals[(curr_phase, 0)])
            curr_phase_end = curr_phase_solution.get_end()
            extension = curr_phase_end

        else:
            self.model.export_model('nosolution.cpo')
            print('No solution')
            extension = None
            departures = None

        return extension, departures
Beispiel #8
0
except:
    ENVIRONMENT_PRESENT = False

EXE_EXTENSION = ".exe" if platform.system() == 'Windows' else ""


##############################################################################
## Define default context for DOcloud solving
##############################################################################

#-----------------------------------------------------------------------------
# Global context

# Create default context infrastructure
DOCLOUD_CONTEXT = Context(model=Context(),
                          params=CpoParameters(),
                          solver=Context())
context = DOCLOUD_CONTEXT

# Default log output
context.log_output = sys.stdout

# Default log verbosity
context.verbose = 0

# Visu enable indicator (internal, can be disabled for testing purpose)
context.visu_enabled = True


#-----------------------------------------------------------------------------
# Modeling context