コード例 #1
0
    def build_other_solution(self,
                             result_storage: ResultStorage) -> ResultStorage:
        for sol in result_storage.list_solution_fits:  # [-min(2, len(result_storage.list_solution_fits)):]:
            if "satisfy" not in sol[0].__dict__.keys():
                rb, constraints = get_ressource_breaks(
                    self.problem_calendar, self.problem_no_calendar, sol[0])
                sol[0].satisfy = not (any(len(rb[r]) > 0 for r in rb))
                sol[0].constraints = constraints
                print("Check Ressource : ", sol[0].satisfy)
            if sol[0].satisfy is False:
                if self.partial_solution is None:
                    solution = RCPSPSolution(
                        problem=self.problem_calendar,
                        rcpsp_permutation=sol[0].rcpsp_permutation,
                        rcpsp_modes=sol[0].rcpsp_modes)

                    solution.satisfy = self.check_sol(self.problem_calendar,
                                                      solution)
                    result_storage.list_solution_fits += [
                        (solution,
                         -self.problem_calendar.evaluate(solution)["makespan"])
                    ]
        # result_storage.list_solution_fits = [r for r in result_storage.list_solution_fits
        #                                      if r[0].satisfy]
        return result_storage
コード例 #2
0
 def retrieve_solutions(
     self, result, parameters_cp: ParametersCP = ParametersCP.default()):
     intermediate_solutions = parameters_cp.intermediate_solution
     best_solution = None
     best_makespan = -float("inf")
     list_solutions_fit = []
     starts = []
     mruns = []
     object_result: List[MRCPSP_Result] = []
     if intermediate_solutions:
         for i in range(len(result)):
             object_result += [result[i]]
             # print("Objective : ", result[i, "objective"])
     else:
         object_result += [result]
     for res in object_result:
         modes = []
         for j in range(len(res.mode_chosen)):
             if (self.modeindex_map[j + 1]['task'] !=
                     1) and (self.modeindex_map[j + 1]['task'] !=
                             self.rcpsp_model.n_jobs + 2):
                 modes.append(self.modeindex_map[res.mode_chosen[j]]
                              ['original_mode_index'])
             elif (self.modeindex_map[j + 1]['task']
                   == 1) or (self.modeindex_map[j + 1]['task']
                             == self.rcpsp_model.n_jobs + 2):
                 modes.append(1)
         rcpsp_schedule = {}
         start_times = res.dict["start"]
         for i in range(len(start_times)):
             rcpsp_schedule[i + 1] = {
                 'start_time':
                 start_times[i],
                 'end_time':
                 start_times[i] +
                 self.rcpsp_model.mode_details[i + 1][modes[i]]['duration']
             }
         sol = RCPSPSolution(problem=self.rcpsp_model,
                             rcpsp_schedule=rcpsp_schedule,
                             rcpsp_modes=modes[1:-1],
                             rcpsp_schedule_feasible=True)
         objective = self.aggreg_from_dict_values(
             self.rcpsp_model.evaluate(sol))
         if objective > best_makespan:
             best_makespan = objective
             best_solution = sol.copy()
         list_solutions_fit += [(sol, objective)]
     result_storage = ResultStorage(
         list_solution_fits=list_solutions_fit,
         best_solution=best_solution,
         mode_optim=self.params_objective_function.sense_function,
         limit_store=False)
     return result_storage
コード例 #3
0
 def solve_and_retrieve(self,
                        solve_models_function,
                        apriori=True,
                        aposteriori=True):
     from multiprocessing import Pool
     from functools import partial
     models = self.get_models(apriori, aposteriori)
     p = Pool(min(8, len(models)))
     l = p.map(solve_models_function, models)
     solutions = [li.best_solution for li in l]
     results = np.zeros((len(solutions), len(self.test_instance), 3))
     for index_instance in range(len(self.test_instance)):
         print("Evaluating in instance #", index_instance)
         instance = self.test_instance[index_instance]
         for index_pareto in range(len(solutions)):
             sol_ = RCPSPSolution(
                 problem=instance,
                 rcpsp_permutation=solutions[index_pareto].
                 rcpsp_permutation,
                 rcpsp_modes=solutions[index_pareto].rcpsp_modes)
             fit = instance.evaluate(sol_)
             results[index_pareto, index_instance,
                     0] = 1 if sol_.rcpsp_schedule_feasible else 0
             results[index_pareto, index_instance, 1] = fit["makespan"]
             results[index_pareto, index_instance,
                     2] = fit["mean_resource_reserve"]
     return results
コード例 #4
0
 def retrieve_solutions(self, parameters_milp: ParametersMilp) -> ResultStorage:
     retrieve_all_solution = parameters_milp.retrieve_all_solution
     nb_solutions_max = parameters_milp.n_solutions_max
     nb_solution = min(nb_solutions_max, self.model.num_solutions)
     if not retrieve_all_solution:
         nb_solution = 1
     list_solution_fits = []
     print(nb_solution, " solutions found")
     for s in range(nb_solution):
         rcpsp_schedule = {}
         objective = self.model.objective_values[s]
         for (j, t) in product(self.J, self.T):
             value = self.x[j][t].xi(s)
             if value >= 0.5:
                 rcpsp_schedule[j + 1] = {'start_time': t,
                                          'end_time': t + self.rcpsp_model.mode_details[j + 1][1]['duration']}
         print("Size schedule : ", len(rcpsp_schedule.keys()))
         try:
             solution = RCPSPSolution(problem=self.rcpsp_model,
                                      rcpsp_schedule=rcpsp_schedule,
                                      rcpsp_schedule_feasible=True)
             fit = self.aggreg_from_sol(solution)
             list_solution_fits += [(solution, fit)]
         except:
             print("Problem =", rcpsp_schedule, len(rcpsp_schedule))
             pass
     return ResultStorage(list_solution_fits=list_solution_fits,
                          best_solution=min(list_solution_fits,
                                            key=lambda x: x[1])[0],
                          mode_optim=self.params_objective_function.sense_function)
コード例 #5
0
 def retrieve_solutions(self, parameters_milp: ParametersMilp) -> ResultStorage:
     retrieve_all_solution = parameters_milp.retrieve_all_solution
     nb_solutions_max = parameters_milp.n_solutions_max
     nb_solution = min(nb_solutions_max, self.model.SolCount)
     if not retrieve_all_solution:
         nb_solution = 1
     list_solution_fits = []
     for s in range(nb_solution):
         self.model.params.SolutionNumber = s
         rcpsp_schedule = {}
         modes = {}
         objective = self.model.getAttr("ObjVal")
         for (task, mode, t) in self.x:
             value = self.x[(task, mode, t)].getAttr('Xn')
             if value >= 0.5:
                 rcpsp_schedule[task] = {'start_time': t,
                                         'end_time': t + self.rcpsp_model.mode_details[task][mode]['duration']}
                 modes[task] = mode
         print("Size schedule : ", len(rcpsp_schedule.keys()))
         try:
             modes.pop(1)
             modes.pop(self.rcpsp_model.n_jobs+2)
             modes_vec = [modes[k] for k in sorted(modes)]
             solution = RCPSPSolution(problem=self.rcpsp_model,
                                      rcpsp_schedule=rcpsp_schedule,
                                      rcpsp_modes=modes_vec,
                                      rcpsp_schedule_feasible=True)
             fit = self.aggreg_from_sol(solution)
             list_solution_fits += [(solution, fit)]
         except:
             pass
     return ResultStorage(list_solution_fits=list_solution_fits,
                          best_solution=min(list_solution_fits,
                                            key=lambda x: x[1])[0],
                          mode_optim=self.params_objective_function.sense_function)
コード例 #6
0
    def retrieve_solutions(
        self, result, parameters_cp: ParametersCP = ParametersCP.default()
    ) -> ResultStorage:
        intermediate_solutions = parameters_cp.intermediate_solution
        best_solution = None
        best_makespan = -float("inf")
        list_solutions_fit = []
        starts = []
        if intermediate_solutions:
            for i in range(len(result)):
                if isinstance(result[i], RCPSPSolCP):
                    starts += [result[i].dict["s"]]
                else:
                    starts += [result[i, "s"]]
        else:
            if isinstance(result, RCPSPSolCP):
                starts += [result.dict["s"]]
            else:
                starts = [result["s"]]

        for start_times in starts:
            rcpsp_schedule = {}
            for k in range(len(start_times)):
                rcpsp_schedule[k + 1] = {
                    'start_time':
                    start_times[k],
                    'end_time':
                    start_times[k] +
                    self.rcpsp_model.mode_details[k + 1][1]['duration']
                }
            sol = RCPSPSolution(problem=self.rcpsp_model,
                                rcpsp_schedule=rcpsp_schedule,
                                rcpsp_schedule_feasible=True)
            objective = self.aggreg_from_dict_values(
                self.rcpsp_model.evaluate(sol))
            if objective > best_makespan:
                best_makespan = objective
                best_solution = sol.copy()
            list_solutions_fit += [(sol, objective)]
        result_storage = ResultStorage(
            list_solution_fits=list_solutions_fit,
            best_solution=best_solution,
            mode_optim=self.params_objective_function.sense_function,
            limit_store=False)
        return result_storage
コード例 #7
0
def from_last_state_to_solution(state: State, domain: SchedulingDomain):
    modes = [
        state.tasks_mode.get(j, 1) for j in sorted(domain.get_tasks_ids())
    ]
    modes = modes[1:-1]
    schedule = {
        j: {
            "start_time": state.tasks_details[j].start,
            "end_time": state.tasks_details[j].end
        }
        for j in state.tasks_details
    }
    return RCPSPSolution(problem=build_do_domain(domain),
                         rcpsp_permutation=None,
                         rcpsp_modes=modes,
                         rcpsp_schedule=schedule)
コード例 #8
0
    def evaluate_heuristic(self, individual, domains) -> float:
        vals = []
        func_heuristic = self.toolbox.compile(expr=individual)
        # print('individual', individual)
        for domain in domains:

            ###
            initial_state = domain.get_initial_state()

            do_model = build_do_domain(domain)
            modes = [initial_state.tasks_mode.get(j, 1) for j in sorted(domain.get_tasks_ids())]
            modes = modes[1:-1]

            cpm = self.cpm_data[domain]['cpm']
            cpm_esd = self.cpm_data[domain]['cpm_esd']

            raw_values = []
            for task_id in domain.get_available_tasks(initial_state):
                input_features = [feature_function_map[lf](domain=domain,
                                                           cpm=cpm,
                                                           cpm_esd=cpm_esd,
                                                           task_id=task_id,
                                                           state=initial_state)
                                  for lf in self.list_feature]
                output_value = func_heuristic(*input_features)
                raw_values.append(output_value)

            normalized_values = [x + 1 for x in sorted(range(len(raw_values)), key=lambda k: raw_values[k],
                                                       reverse=False)]
            normalized_values_for_do = [normalized_values[i] - 2 for i in range(len(normalized_values)) if
                                        normalized_values[i] not in {1, len(normalized_values)}]

            solution = RCPSPSolution(problem=do_model,
                                     rcpsp_permutation=normalized_values_for_do,
                                     rcpsp_modes=modes,
                                     )
            last_activity = max(list(solution.rcpsp_schedule.keys()))
            do_makespan = solution.rcpsp_schedule[last_activity]['end_time']
            vals.append(do_makespan)

        fitness = [np.mean(vals)]
        # fitness = [np.max(vals)]
        return fitness
コード例 #9
0
 def solve(self, **kwargs) -> ResultStorage:
     greedy_choice = kwargs.get("greedy_choice",
                                GreedyChoice.MOST_SUCCESSORS)
     verbose = kwargs.get("verbose", False)
     current_succ = {
         k: {
             "succs": set(self.successors_map[k]["succs"]),
             "nb": self.successors_map[k]["nb"]
         }
         for k in self.successors_map
     }
     current_pred = {
         k: {
             "succs": set(self.predecessors_map[k]["succs"]),
             "nb": self.predecessors_map[k]["nb"]
         }
         for k in self.predecessors_map
     }
     schedule = {}
     current_ressource_available = self.resources.copy()
     current_ressource_non_renewable = {
         nr: self.resources[nr]
         for nr in self.non_renewable
     }
     schedule[1] = {"start_time": 0, "end_time": 0}
     available_activities = {
         n
         for n in current_pred
         if n not in schedule and current_pred[n]["nb"] == 0
     }
     available_activities.update(
         {n
          for n in self.all_activities if n not in current_pred})
     for neighbor in current_pred:
         if 1 in current_pred[neighbor]["succs"]:
             current_pred[neighbor]["succs"].remove(1)
             current_pred[neighbor]["nb"] -= 1
             if current_pred[neighbor]["nb"] == 0:
                 available_activities.add(neighbor)
     if verbose:
         print(current_pred)
     queue = []
     current_time = 0
     perm = []
     while len(schedule) < self.n_jobs + 2:
         if verbose:
             print(len(schedule))
             print("available activities : ", available_activities)
         possible_activities = [
             n for n in available_activities
             if all(self.mode_details[n][self.modes_dict[n]][r] <=
                    current_ressource_available[r]
                    for r in current_ressource_available)
         ]
         if verbose:
             print("Ressources : ", current_ressource_available)
         while len(possible_activities) > 0:
             if greedy_choice == GreedyChoice.MOST_SUCCESSORS:
                 next_activity = max(possible_activities,
                                     key=lambda x: current_succ[x]["nb"])
             if greedy_choice == GreedyChoice.SAMPLE_MOST_SUCCESSORS:
                 prob = np.array([
                     current_succ[possible_activities[i]]["nb"]
                     for i in range(len(possible_activities))
                 ])
                 s = np.sum(prob)
                 if s != 0:
                     prob = prob / s
                 else:
                     prob = 1. / len(possible_activities) * np.ones(
                         (len(possible_activities)))
                 next_activity = np.random.choice(np.arange(
                     0, len(possible_activities)),
                                                  size=1,
                                                  p=prob)[0]
                 next_activity = possible_activities[next_activity]
             if greedy_choice == GreedyChoice.FASTEST:
                 next_activity = min(possible_activities,
                                     key=lambda x: self.mode_details[x][
                                         self.modes_dict[x]]["duration"])
             if greedy_choice == GreedyChoice.TOTALLY_RANDOM:
                 next_activity = random.choice(possible_activities)
             available_activities.remove(next_activity)
             perm += [next_activity - 2]
             schedule[next_activity] = {}
             schedule[next_activity]["start_time"] = current_time
             schedule[next_activity]["end_time"] = current_time + \
                                                   self.mode_details[next_activity][self.modes_dict[next_activity]]["duration"]
             push(queue, (schedule[next_activity]["end_time"],
                          next_activity, "end_"))
             for r in self.resources:
                 current_ressource_available[r] -= self.mode_details[
                     next_activity][self.modes_dict[next_activity]][r]
                 if r in current_ressource_non_renewable:
                     current_ressource_non_renewable[
                         r] -= self.mode_details[next_activity][
                             self.modes_dict[next_activity]][r]
             if verbose:
                 print(current_time, "Current ressource available : ",
                       current_ressource_available)
             possible_activities = [
                 n for n in available_activities
                 if all(self.mode_details[n][self.modes_dict[n]][r] <=
                        current_ressource_available[r]
                        for r in current_ressource_available)
             ]
         current_time, activity, descr = pop(queue)
         for neighbor in current_pred:
             if activity in current_pred[neighbor]["succs"]:
                 current_pred[neighbor]["succs"].remove(activity)
                 current_pred[neighbor]["nb"] -= 1
                 if current_pred[neighbor]["nb"] == 0:
                     available_activities.add(neighbor)
         for r in self.resources:
             if r not in current_ressource_non_renewable:
                 current_ressource_available[r] += self.mode_details[
                     activity][self.modes_dict[activity]][r]
     if verbose:
         print("Final Time ", current_time)
     sol = RCPSPSolution(
         problem=self.rcpsp_model,
         rcpsp_permutation=perm[:-1],
         rcpsp_schedule=schedule,
         rcpsp_modes=[self.modes_dict[i + 2] for i in range(self.n_jobs)],
         rcpsp_schedule_feasible=True)
     result_storage = ResultStorage(
         list_solution_fits=[(sol, self.aggreg_from_sol(sol))],
         best_solution=sol,
         mode_optim=self.params_objective_function.sense_function)
     return result_storage
コード例 #10
0
 def compute_schedule_from_priority_list(self, permutation_jobs: List[int],
                                         modes_dict: Dict[int, int]):
     current_pred = {
         k: {
             "succs": set(self.immediate_predecessors[k]),
             "nb": len(self.immediate_predecessors[k])
         }
         for k in self.immediate_predecessors
     }
     schedule = {}
     current_ressource_available = self.resources.copy()
     current_ressource_non_renewable = {
         nr: self.resources[nr]
         for nr in self.non_renewable
     }
     schedule[1] = {"start_time": 0, "end_time": 0}
     available_activities = {
         n
         for n in current_pred
         if n not in schedule and current_pred[n]["nb"] == 0
     }
     available_activities.update(
         {n
          for n in self.all_activities if n not in current_pred})
     for neighbor in self.immediate_successors[1]:
         if 1 in current_pred[neighbor]["succs"]:
             current_pred[neighbor]["succs"].remove(1)
             current_pred[neighbor]["nb"] -= 1
             if current_pred[neighbor]["nb"] == 0:
                 available_activities.add(neighbor)
     permutation_jobs.remove(1)
     queue = []
     current_time = 0
     perm = []
     while len(schedule) < self.n_jobs + 2:
         possible_activities = [
             n for n in available_activities
             if all(self.mode_details[n][modes_dict[n]][r] <=
                    current_ressource_available[r]
                    for r in current_ressource_available)
         ]
         while len(possible_activities) > 0:
             next_activity = min(possible_activities,
                                 key=lambda x: permutation_jobs.index(x))
             available_activities.remove(next_activity)
             perm += [next_activity - 2]
             schedule[next_activity] = {}
             schedule[next_activity]["start_time"] = current_time
             schedule[next_activity]["end_time"] = \
                 current_time + self.mode_details[next_activity][modes_dict[next_activity]]["duration"]
             permutation_jobs.remove(next_activity)
             push(queue, (schedule[next_activity]["end_time"],
                          next_activity, "end_"))
             for r in self.resources:
                 current_ressource_available[r] -= self.mode_details[
                     next_activity][modes_dict[next_activity]][r]
                 if r in current_ressource_non_renewable:
                     current_ressource_non_renewable[
                         r] -= self.mode_details[next_activity][
                             modes_dict[next_activity]][r]
             possible_activities = [
                 n for n in available_activities
                 if all(self.mode_details[n][modes_dict[n]][r] <=
                        current_ressource_available[r]
                        for r in current_ressource_available)
             ]
         # print(current_ressource_available, self.rcpsp_model.non_renewable_resources)
         current_time, activity, descr = pop(queue)
         for neighbor in self.immediate_successors[activity]:
             if activity in current_pred[neighbor]["succs"]:
                 current_pred[neighbor]["succs"].remove(activity)
                 current_pred[neighbor]["nb"] -= 1
                 if current_pred[neighbor]["nb"] == 0:
                     available_activities.add(neighbor)
         for r in self.resources:
             if r not in current_ressource_non_renewable:
                 current_ressource_available[r] += self.mode_details[
                     activity][modes_dict[activity]][r]
     # print("Final Time ", current_time)
     sol = RCPSPSolution(
         problem=self.rcpsp_model,
         rcpsp_permutation=perm[:-1],
         rcpsp_schedule=schedule,
         rcpsp_modes=[modes_dict[i + 1] for i in range(self.n_jobs)],
         rcpsp_schedule_feasible=True)
     result_storage = ResultStorage(
         list_solution_fits=[(sol, self.aggreg_from_sol(sol))],
         best_solution=sol,
         mode_optim=self.params_objective_function.sense_function)
     return result_storage
コード例 #11
0
    def solve(self, **kwargs) -> ResultStorage:
        greedy_choice = kwargs.get("greedy_choice",
                                   GreedyChoice.MOST_SUCCESSORS)
        verbose = kwargs.get("verbose", False)
        current_succ = {
            k: {
                "succs": set(self.successors_map[k]["succs"]),
                "nb": self.successors_map[k]["nb"]
            }
            for k in self.successors_map
        }
        current_pred = {
            k: {
                "succs": set(self.predecessors_map[k]["succs"]),
                "nb": self.predecessors_map[k]["nb"]
            }
            for k in self.predecessors_map
        }
        schedule = {}
        partial_solution: PartialSolution = kwargs.get("partial_solution",
                                                       None)
        # TODO continue on this... the mode partial solution will probaby override the self.modes_dict at some point.
        if partial_solution is not None:
            if partial_solution.start_times is not None:
                starting_time = 0
                for task in partial_solution.start_times:
                    schedule[task] = {
                        "start_time": partial_solution.start_times[task]
                    }
                    starting_time = max(starting_time,
                                        partial_solution.start_times[task])
            if partial_solution.end_times is not None:
                for task in partial_solution.end_times:
                    if task in schedule:
                        schedule[task][
                            "end_time"] = partial_solution.end_times[task]

        current_ressource_available = {
            r: list(self.resources[r]) + [self.resources[r][-1]] * 100
            for r in self.resources
        }
        # if self.with_calendar:
        #    current_ressource_available = {r: current_ressource_available[r][0]
        #                                   for r in current_ressource_available}

        current_ressource_non_renewable = {
            nr: list(self.resources[nr]) + [self.resources[nr][-1]] * 100
            for nr in self.non_renewable
        }
        # if self.with_calendar:
        #     current_ressource_non_renewable = {r: current_ressource_non_renewable[r][0]
        #                                        for r in current_ressource_non_renewable}
        if 1 not in schedule:
            schedule[1] = {"start_time": 0, "end_time": 0}

        available_activities = {
            n
            for n in current_pred
            if n not in schedule and current_pred[n]["nb"] == 0
        }
        available_activities.update(
            {n
             for n in self.all_activities if n not in current_pred})
        for neighbor in current_pred:
            if 1 in current_pred[neighbor]["succs"]:
                current_pred[neighbor]["succs"].remove(1)
                current_pred[neighbor]["nb"] -= 1
                if current_pred[neighbor]["nb"] == 0:
                    available_activities.add(neighbor)
        if verbose:
            print(current_pred)
        queue = []
        current_time = 0
        perm = []
        while len(schedule) < self.n_jobs + 2:
            if True:
                print(len(schedule), current_time)
                print("available activities : ", available_activities)
            #print(len(available_activities), "available activities")
            possible_activities = [
                n for n in available_activities if all(
                    self.mode_details[n][self.modes_dict[n]][r] <=
                    current_ressource_available[r][min(
                        time,
                        len(current_ressource_available[r]) - 1)]
                    for r in current_ressource_available for time in range(
                        current_time, current_time +
                        self.mode_details[n][self.modes_dict[n]]["duration"]))
            ]
            if current_time >= 10000:
                print("hey")

            #print(len(possible_activities))
            # for n in available_activities:
            #     for time in range(current_time, current_time
            #                       + self.mode_details[n][self.modes_dict[n]]["duration"]+1):
            #         for r in current_ressource_available:
            #             print("task ", n)
            #             print("time ", time)
            #             print("res ", r)
            #             print("Need ", self.mode_details[n][self.modes_dict[n]][r],
            #                   "Avail ", current_ressource_available[r][time])
            #             print(self.mode_details[n][self.modes_dict[n]][r]
            #                   <= current_ressource_available[r][time])
            if verbose:
                print("Ressources : ", current_ressource_available)
            while len(possible_activities) > 0:
                #print(len(possible_activities), current_time)
                if greedy_choice == GreedyChoice.MOST_SUCCESSORS:
                    next_activity = max(possible_activities,
                                        key=lambda x: current_succ[x]["nb"])
                if greedy_choice == GreedyChoice.SAMPLE_MOST_SUCCESSORS:
                    prob = np.array([
                        current_succ[possible_activities[i]]["nb"]
                        for i in range(len(possible_activities))
                    ])
                    s = np.sum(prob)
                    if s != 0:
                        prob = prob / s
                    else:
                        prob = 1. / len(possible_activities) * np.ones(
                            (len(possible_activities)))
                    next_activity = np.random.choice(np.arange(
                        0, len(possible_activities)),
                                                     size=1,
                                                     p=prob)[0]
                    next_activity = possible_activities[next_activity]
                if greedy_choice == GreedyChoice.FASTEST:
                    next_activity = min(possible_activities,
                                        key=lambda x: self.mode_details[x][
                                            self.modes_dict[x]]["duration"])
                if greedy_choice == GreedyChoice.TOTALLY_RANDOM:
                    next_activity = random.choice(possible_activities)
                available_activities.remove(next_activity)
                perm += [next_activity - 2]
                schedule[next_activity] = {}
                schedule[next_activity]["start_time"] = current_time
                schedule[next_activity]["end_time"] = current_time + \
                                        self.mode_details[next_activity][self.modes_dict[next_activity]]["duration"]
                push(queue, (schedule[next_activity]["end_time"],
                             next_activity, "end_"))
                mode = self.modes_dict[next_activity]
                duration = self.mode_details[next_activity][mode]["duration"]
                for r in self.resources:
                    for t in range(current_time, current_time + duration):
                        current_ressource_available[r][t] -= self.mode_details[
                            next_activity][mode][r]
                        if r in current_ressource_non_renewable:
                            current_ressource_non_renewable[r][
                                t] -= self.mode_details[next_activity][mode][r]
                #if verbose:
                #    print(current_time)
                #          #"Current ressource available : ", current_ressource_available)
                possible_activities = [
                    n for n in available_activities if all(
                        self.mode_details[n][self.modes_dict[n]][r] <=
                        current_ressource_available[r][time]
                        for r in current_ressource_available for time in range(
                            current_time, current_time + self.mode_details[n][
                                self.modes_dict[n]]["duration"] + 1))
                ]
            if len(queue) > 0:
                current_time, activity, descr = pop(queue)
                for neighbor in current_pred:
                    if activity in current_pred[neighbor]["succs"]:
                        current_pred[neighbor]["succs"].remove(activity)
                        current_pred[neighbor]["nb"] -= 1
                        if current_pred[neighbor]["nb"] == 0:
                            available_activities.add(neighbor)
            else:
                current_time += 1
        if verbose:
            print("Final Time ", current_time)
        # print("Final Time ", current_time)
        sol = RCPSPSolution(
            problem=self.rcpsp_model,
            rcpsp_permutation=perm[:-1],
            rcpsp_schedule=schedule,
            rcpsp_modes=[self.modes_dict[i + 2] for i in range(self.n_jobs)],
            rcpsp_schedule_feasible=True)
        result_storage = ResultStorage(
            list_solution_fits=[(sol, self.aggreg_from_sol(sol))],
            best_solution=sol,
            mode_optim=self.params_objective_function.sense_function)
        return result_storage
コード例 #12
0
    def _get_next_action(self, observation: D.T_agent[D.T_observation]) -> D.T_agent[D.T_concurrency[D.T_event]]:
        run_sgs = True
        cheat_mode = False

        do_model = build_do_domain(self.domain_model)
        modes = [observation.tasks_mode.get(j, 1) for j in sorted(self.domain.get_tasks_ids())]
        modes = modes[1:-1]

        if run_sgs:
            scheduled_tasks_start_times = {}
            for j in observation.tasks_details.keys():
                if observation.tasks_details[j].start is not None:
                    scheduled_tasks_start_times[j] = observation.tasks_details[j].start
                    do_model.mode_details[j][1]['duration'] = observation.tasks_details[j].sampled_duration

        # do_model = build_do_domain(self.domain)
        # modes = [observation.tasks_mode.get(j, 1) for j in sorted(self.domain.get_tasks_ids())]
        # modes = modes[1:-1]
        #
        # if run_sgs:
        #     scheduled_tasks_start_times = {}
        #     for j in observation.tasks_details.keys():
        #         # schedule[j] = {}
        #         if observation.tasks_details[j].start is not None:
        #             # schedule[j]["start_time"] = observation.tasks_details[j].start
        #             scheduled_tasks_start_times[j] = observation.tasks_details[j].start
        #         # if observation.tasks_details[j].end is not None:
        #         #     schedule[j]["end_time"] = observation.tasks_details[j].end
        #         else:
        #             if not cheat_mode:
        #                 # print('do_model: ', do_model)
        #                 do_model.mode_details[j][1]['duration'] = self.domain_model.sample_task_duration(j, 1, 0.)

        normalized_values = self.fixed_perm

        normalized_values_for_do = [normalized_values[i] - 2 for i in range(len(normalized_values)) if
                                    normalized_values[i] not in {1, len(normalized_values)}]

        # print('normalized_values: ', normalized_values)
        # print('normalized_values_for_do: ', normalized_values_for_do)
        t = observation.t

        modes_dictionnary = {}
        for i in range(len(normalized_values)):
            modes_dictionnary[i+1] = 1

        if run_sgs:

            solution = RCPSPSolution(problem=do_model,
                                     rcpsp_permutation=normalized_values_for_do,
                                     rcpsp_modes=modes,
                                     )

            solution.generate_schedule_from_permutation_serial_sgs_2(current_t=t,
                                                                     completed_tasks=
                                                                     {j: observation.tasks_details[j]
                                                                      for j in observation.tasks_complete},
                                                                     scheduled_tasks_start_times=scheduled_tasks_start_times)

            schedule = solution.rcpsp_schedule
        else:
            schedule = None

        sgs_policy = PolicyRCPSP(domain=self.domain,
                                 schedule=schedule,
                                 policy_method_params=PolicyMethodParams(
                                     # base_policy_method=BasePolicyMethod.SGS_PRECEDENCE,
                                     # base_policy_method=BasePolicyMethod.SGS_READY,
                                     base_policy_method=BasePolicyMethod.FOLLOW_GANTT,
                                     # delta_index_freedom=self.params_gphh.delta_index_freedom,
                                     # delta_time_freedom=self.params_gphh.delta_time_freedom
                                 ),
                                 permutation_task=normalized_values,
                                 modes_dictionnary=modes_dictionnary)
        action: SchedulingAction = sgs_policy.sample_action(observation)
        # print('action_2: ', action.action)
        return action
コード例 #13
0
    def _get_next_action(self, observation: D.T_agent[D.T_observation]) -> D.T_agent[D.T_concurrency[D.T_event]]:

        run_sgs = True
        cheat_mode = False
        regenerate_cpm = True

        do_model = build_do_domain(self.domain_model)
        modes = [observation.tasks_mode.get(j, 1) for j in sorted(self.domain.get_tasks_ids())]
        modes = modes[1:-1]

        if run_sgs:
            scheduled_tasks_start_times = {}
            for j in observation.tasks_details.keys():
                if observation.tasks_details[j].start is not None:
                    scheduled_tasks_start_times[j] = observation.tasks_details[j].start
                    do_model.mode_details[j][1]['duration'] = observation.tasks_details[j].sampled_duration

        # do_model = build_do_domain(self.domain)
        # modes = [observation.tasks_mode.get(j, 1) for j in sorted(self.domain.get_tasks_ids())]
        # modes = modes[1:-1]
        #
        # if run_sgs:
        #     scheduled_tasks_start_times = {}
        #     for j in observation.tasks_details.keys():
        #         # schedule[j] = {}
        #         if observation.tasks_details[j].start is not None:
        #             # schedule[j]["start_time"] = observation.tasks_details[j].start
        #             scheduled_tasks_start_times[j] = observation.tasks_details[j].start
        #         # if observation.tasks_details[j].end is not None:
        #         #     schedule[j]["end_time"] = observation.tasks_details[j].end
        #         else:
        #             if not cheat_mode:
        #                 do_model.mode_details[j][1]['duration'] = self.domain_model.sample_task_duration(j, 1, 0.)

        if regenerate_cpm:
            cpm, cpm_esd = compute_cpm(do_model)

        t = observation.t
        raw_values = []
        for task_id in self.domain.get_available_tasks(observation):
            input_features = [feature_function_map[lf](
                                                    domain=self.domain,
                                                       cpm = cpm,
                                                        cpm_esd=cpm_esd,
                                                       task_id=task_id,
                                                       state=observation)
                              for lf in self.list_feature]
            output_values = []
            for f in self.func_heuristics:
                output_value = f(*input_features)
                output_values.append(output_value)

            # print('output_values: ', output_values)
            if self.remove_extremes_values > 0:
                the_median = float(np.median(output_values))
                tmp = {}
                for i in range(len(output_values)):
                    tmp[i] = abs(output_values[i] - the_median)
                tmp = sorted(tmp.items(), key=lambda x: x[1], reverse=True)
                to_remove = [tmp[i][0] for i in range(self.remove_extremes_values)]
                output_values = list(np.delete(output_values, to_remove))

            # print('output_values filtered: ', output_values)
            if self.pool_aggregation_method == PoolAggregationMethod.MEAN:
                agg_value = np.mean(output_values)
            elif self.pool_aggregation_method == PoolAggregationMethod.MEDIAN:
                agg_value = np.median(output_values)
            elif self.pool_aggregation_method == PoolAggregationMethod.RANDOM:
                index = random.randint(len(output_values))
                agg_value = output_values[index]

            # print('agg_value: ', agg_value)
            raw_values.append(agg_value)

        normalized_values = [x+1 for x in sorted(range(len(raw_values)), key=lambda k: raw_values[k],
                                                 reverse=False)]
        normalized_values_for_do = [normalized_values[i] - 2 for i in range(len(normalized_values)) if
                                    normalized_values[i] not in {1, len(normalized_values)}]


        # print('normalized_values: ', normalized_values)
        # print('normalized_values_for_do: ', normalized_values_for_do)

        modes_dictionnary = {}
        for i in range(len(normalized_values)):
            modes_dictionnary[i+1] = 1

        if run_sgs:

            solution = RCPSPSolution(problem=do_model,
                                     rcpsp_permutation=normalized_values_for_do,
                                     rcpsp_modes=modes,
                                     )

            solution.generate_schedule_from_permutation_serial_sgs_2(current_t=t,
                                                                     completed_tasks=
                                                                     {j: observation.tasks_details[j]
                                                                      for j in observation.tasks_complete},
                                                                     scheduled_tasks_start_times=scheduled_tasks_start_times)

            schedule = solution.rcpsp_schedule
        else:
            schedule = None

        sgs_policy = PolicyRCPSP(domain=self.domain,
                                 schedule=schedule,
                                 policy_method_params=PolicyMethodParams(
                                     # base_policy_method=BasePolicyMethod.SGS_PRECEDENCE,
                                     # base_policy_method=BasePolicyMethod.SGS_READY,
                                     base_policy_method=self.params_gphh.base_policy_method,
                                     delta_index_freedom=self.params_gphh.delta_index_freedom,
                                     delta_time_freedom=self.params_gphh.delta_time_freedom),
                                 permutation_task=normalized_values,
                                 modes_dictionnary=modes_dictionnary)
        action: SchedulingAction = sgs_policy.sample_action(observation)
        # print('action_2: ', action.action)
        return action
コード例 #14
0
    def _get_next_action(self, observation: D.T_agent[D.T_observation]) -> D.T_agent[D.T_concurrency[D.T_event]]:
        run_sgs = True
        cheat_mode = False

        do_model = build_do_domain(self.domain_model)
        modes = [observation.tasks_mode.get(j, 1) for j in sorted(self.domain.get_tasks_ids())]
        modes = modes[1:-1]

        if run_sgs:
            scheduled_tasks_start_times = {}
            for j in observation.tasks_details.keys():
                if observation.tasks_details[j].start is not None:
                    scheduled_tasks_start_times[j] = observation.tasks_details[j].start
                    do_model.mode_details[j][1]['duration'] = observation.tasks_details[j].sampled_duration

        # do_model = build_do_domain(self.domain)
        # modes = [observation.tasks_mode.get(j, 1) for j in sorted(self.domain.get_tasks_ids())]
        # modes = modes[1:-1]
        #
        # if run_sgs:
        #     scheduled_tasks_start_times = {}
        #     for j in observation.tasks_details.keys():
        #         if observation.tasks_details[j].start is not None:
        #             scheduled_tasks_start_times[j] = observation.tasks_details[j].start
        #         else:
        #             if not cheat_mode:
        #                 do_model.mode_details[j][1]['duration'] = self.domain_model.sample_task_duration(j, 1, 0.)

        if self.recompute_cpm:
            cpm, cpm_esd = compute_cpm(do_model)
        else:
            cpm = self.cpm_data[self.domain]['cpm']
            cpm_esd = self.cpm_data[self.domain]['cpm_esd']

        t = observation.t
        raw_values = []
        for task_id in self.domain.get_available_tasks(observation):
            input_features = [feature_function_map[lf](domain=self.domain,
                                                       cpm = cpm,
                                                        cpm_esd=cpm_esd,
                                                       task_id=task_id,
                                                       state=observation)
                              for lf in self.list_feature]
            output_value = self.func_heuristic(*input_features)
            raw_values.append(output_value)

        normalized_values = [x+1 for x in sorted(range(len(raw_values)), key=lambda k: raw_values[k],
                                                 reverse=False)]
        normalized_values_for_do = [normalized_values[i] - 2 for i in range(len(normalized_values)) if
                                    normalized_values[i] not in {1, len(normalized_values)}]


        # print(t, ': ', normalized_values)
        # print('normalized_values_for_do: ', normalized_values_for_do)

        modes_dictionnary = {}
        for i in range(len(normalized_values)):
            modes_dictionnary[i+1] = 1

        if run_sgs:

            solution = RCPSPSolution(problem=do_model,
                                     rcpsp_permutation=normalized_values_for_do,
                                     rcpsp_modes=modes,
                                     )

            solution.generate_schedule_from_permutation_serial_sgs_2(current_t=t,
                                                                     completed_tasks=
                                                                     {j: observation.tasks_details[j]
                                                                      for j in observation.tasks_complete},
                                                                     scheduled_tasks_start_times=scheduled_tasks_start_times)

            schedule = solution.rcpsp_schedule
        else:
            schedule = None

        sgs_policy = PolicyRCPSP(domain=self.domain,
                                 schedule=schedule,
                                 policy_method_params=PolicyMethodParams(
                                     # base_policy_method=BasePolicyMethod.SGS_PRECEDENCE,
                                     # base_policy_method=BasePolicyMethod.SGS_READY,
                                     base_policy_method=self.params_gphh.base_policy_method,
                                     delta_index_freedom=self.params_gphh.delta_index_freedom,
                                     delta_time_freedom=self.params_gphh.delta_time_freedom),
                                 permutation_task=normalized_values,
                                 modes_dictionnary=modes_dictionnary)
        action: SchedulingAction = sgs_policy.sample_action(observation)
        # print('action_2: ', action.action)
        return action