def run_comparaison(): import os from examples.discrete_optimization.rcpsp_parser_example import get_data_available files = get_data_available() all_single_mode = [os.path.basename(f) for f in files if "sm" in f] # training_cphh = ["j1201_"+str(i)+".sm" for i in range(2, 11)] training_cphh = ["j301_" + str(i) + ".sm" for i in range(1, 11)] # all_testing_domains_names = [f for f in all_single_mode # if not(any(g in f for g in training_cphh))] all_testing_domains_names = ["j1201_2.sm"] # all_testing_domains_names = ["j601_2.sm"] # all_testing_domains_names = random.sample(all_testing_domains_names, 1) # training_domains_names = [f for f in all_single_mode # if any(g in f for g in training_cphh)] training_domains_names = all_testing_domains_names domains_loaded = { domain_name: load_domain(get_complete_path(domain_name)) for domain_name in all_testing_domains_names } test_domain_names = all_testing_domains_names # test_domain_names = [test_domain_names[-1]] # test_domain_names = ["j1201_1.sm"] print("test_domain_names: ", test_domain_names) print("training_domains_names: ", training_domains_names) n_walks = 5 for td in training_domains_names: domains_loaded[td] = load_domain(get_complete_path(td)) all_results = {} for dom in test_domain_names: all_results[dom] = { "random_walk": [], "cp": [], "cp_sgs": [], "gphh": [], "pile": [], } # RANDOM WALK for test_domain_str in test_domain_names: domain: RCPSP = domains_loaded[test_domain_str] domain.set_inplace_environment(False) n_walks = 5 for i in range(n_walks): state = domain.get_initial_state() solver = None states, actions, values = rollout_episode( domain=domain, max_steps=1000, solver=solver, from_memory=state, verbose=False, outcome_formatter=lambda o: f"{o.observation} - cost: {o.value.cost:.2f}", ) print("One random Walk complete") print("Cost :", sum([v.cost for v in values])) all_results[test_domain_str]["random_walk"].append( sum([v.cost for v in values])) print("All random Walk complete") # CP for test_domain_str in test_domain_names: domain: RCPSP = domains_loaded[test_domain_str] do_solver = SolvingMethod.CP domain.set_inplace_environment(False) state = domain.get_initial_state() solver = DOSolver( policy_method_params=PolicyMethodParams( base_policy_method=BasePolicyMethod.FOLLOW_GANTT, delta_index_freedom=0, delta_time_freedom=0, ), method=do_solver, ) solver.solve(domain_factory=lambda: domain) print(do_solver) states, actions, values = rollout_episode( domain=domain, solver=solver, from_memory=state, max_steps=500, verbose=False, outcome_formatter=lambda o: f"{o.observation} - cost: {o.value.cost:.2f}", ) print("Cost: ", sum([v.cost for v in values])) print("CP done") all_results[test_domain_str]["cp"].append(sum([v.cost for v in values])) # CP SGS for test_domain_str in test_domain_names: domain: RCPSP = domains_loaded[test_domain_str] do_solver = SolvingMethod.CP domain.set_inplace_environment(False) state = domain.get_initial_state() solver = DOSolver( policy_method_params=PolicyMethodParams( base_policy_method=BasePolicyMethod.SGS_STRICT, delta_index_freedom=0, delta_time_freedom=0, ), method=do_solver, ) solver.solve(domain_factory=lambda: domain) print(do_solver) states, actions, values = rollout_episode( domain=domain, solver=solver, from_memory=state, max_steps=500, verbose=False, outcome_formatter=lambda o: f"{o.observation} - cost: {o.value.cost:.2f}", ) print("Cost: ", sum([v.cost for v in values])) print("CP_SGS done") all_results[test_domain_str]["cp_sgs"].append( sum([v.cost for v in values])) # PILE for test_domain_str in test_domain_names: domain: RCPSP = domains_loaded[test_domain_str] do_solver = SolvingMethod.PILE domain.set_inplace_environment(False) state = domain.get_initial_state() solver = DOSolver( policy_method_params=PolicyMethodParams( base_policy_method=BasePolicyMethod.FOLLOW_GANTT, delta_index_freedom=0, delta_time_freedom=0, ), method=do_solver, ) solver.solve(domain_factory=lambda: domain) print(do_solver) states, actions, values = rollout_episode( domain=domain, solver=solver, from_memory=state, max_steps=500, verbose=False, outcome_formatter=lambda o: f"{o.observation} - cost: {o.value.cost:.2f}", ) print("Cost: ", sum([v.cost for v in values])) print("PILE done") all_results[test_domain_str]["pile"].append( sum([v.cost for v in values])) # GPHH domain: RCPSP = load_domain(get_complete_path("j301_1.sm")) training_domains = [ domains_loaded[training_domain] for training_domain in training_domains_names ] with open("cp_reference_permutations") as json_file: cp_reference_permutations = json.load(json_file) # with open('cp_reference_makespans') as json_file: # cp_reference_makespans = json.load(json_file) for i in range(n_walks): domain.set_inplace_environment(False) set_feature = { FeatureEnum.EARLIEST_FINISH_DATE, FeatureEnum.EARLIEST_START_DATE, FeatureEnum.LATEST_FINISH_DATE, FeatureEnum.LATEST_START_DATE, FeatureEnum.N_PREDECESSORS, FeatureEnum.N_SUCCESSORS, FeatureEnum.ALL_DESCENDANTS, FeatureEnum.RESSOURCE_REQUIRED, FeatureEnum.RESSOURCE_AVG, FeatureEnum.RESSOURCE_MAX, # FeatureEnum.RESSOURCE_MIN FeatureEnum.RESSOURCE_NZ_MIN, } pset = PrimitiveSet("main", len(set_feature)) pset.addPrimitive(operator.add, 2) pset.addPrimitive(operator.sub, 2) pset.addPrimitive(operator.mul, 2) pset.addPrimitive(protected_div, 2) pset.addPrimitive(max_operator, 2) pset.addPrimitive(min_operator, 2) pset.addPrimitive(operator.neg, 1) # pset.addPrimitive(operator.pow, 2) params_gphh = ParametersGPHH( set_feature=set_feature, set_primitves=pset, tournament_ratio=0.2, pop_size=20, n_gen=7, min_tree_depth=1, max_tree_depth=3, crossover_rate=0.7, mutation_rate=0.3, base_policy_method=BasePolicyMethod.SGS_READY, delta_index_freedom=0, delta_time_freedom=0, deap_verbose=True, evaluation=EvaluationGPHH.SGS_DEVIATION, permutation_distance=PermutationDistance.KTD # permutation_distance = PermutationDistance.KTD_HAMMING ) solver = GPHH( training_domains=training_domains, weight=-1, verbose=False, reference_permutations=cp_reference_permutations, # reference_makespans=cp_reference_makespans, training_domains_names=training_domains_names, params_gphh=params_gphh, ) solver.solve(domain_factory=lambda: domain) for test_domain_str in test_domain_names: domain: RCPSP = domains_loaded[test_domain_str] domain.set_inplace_environment(False) state = domain.get_initial_state() solver.set_domain(domain) states, actions, values = rollout_episode( domain=domain, max_steps=1000, solver=solver, from_memory=state, verbose=False, outcome_formatter=lambda o: f"{o.observation} - cost: {o.value.cost:.2f}", ) print("One GPHH done") print("Best evolved heuristic: ", solver.best_heuristic) print("Cost: ", sum([v.cost for v in values])) all_results[test_domain_str]["gphh"].append( sum([v.cost for v in values])) print("All GPHH done") print("##### ALL RESULTS #####") for test_domain_str in test_domain_names: print(test_domain_str, " :") for algo_key in all_results[test_domain_str].keys(): print("\t", algo_key, ": ") print("\t\t all runs:", all_results[test_domain_str][algo_key]) print("\t\t mean:", np.mean(all_results[test_domain_str][algo_key]))
def run_comparaison_stochastic(): import random from skdecide.hub.domain.rcpsp.rcpsp_sk import ( RCPSP, build_n_determinist_from_stochastic, build_stochastic_from_deterministic, ) repeat_runs = 5 test_domain_names = [ "j301_1.sm", "j301_2.sm", "j301_3.sm", "j601_1.sm", "j601_2.sm", "j601_3.sm", ] all_results = {} for dom in test_domain_names: all_results[dom] = { "random_walk": [], "cp": [], "cp_sgs": [], "gphh": [], "pile": [], } for original_domain_name in test_domain_names: original_domain: RCPSP = load_domain( get_complete_path(original_domain_name)) task_to_noise = set( random.sample(original_domain.get_tasks_ids(), len(original_domain.get_tasks_ids()))) stochastic_domain = build_stochastic_from_deterministic( original_domain, task_to_noise=task_to_noise) deterministic_domains = build_n_determinist_from_stochastic( stochastic_domain, nb_instance=6) training_domains = deterministic_domains[0:-1] training_domains_names = [None for i in range(len(training_domains))] test_domain = deterministic_domains[-1] print("training_domains:", training_domains) # RANDOM WALK domain: RCPSP = test_domain domain.set_inplace_environment(False) # random_walk_costs = [] for i in range(repeat_runs): state = domain.get_initial_state() solver = None states, actions, values = rollout_episode( domain=domain, max_steps=1000, solver=solver, from_memory=state, verbose=False, outcome_formatter=lambda o: f"{o.observation} - cost: {o.value.cost:.2f}", ) print("One random Walk complete") print("Cost :", sum([v.cost for v in values])) all_results[original_domain_name]["random_walk"].append( sum([v.cost for v in values])) print("All random Walk complete") # CP domain = test_domain do_solver = SolvingMethod.CP domain.set_inplace_environment(False) state = domain.get_initial_state() solver = DOSolver( policy_method_params=PolicyMethodParams( base_policy_method=BasePolicyMethod.FOLLOW_GANTT, delta_index_freedom=0, delta_time_freedom=0, ), method=do_solver, ) solver.solve(domain_factory=lambda: domain) print(do_solver) states, actions, values = rollout_episode( domain=domain, solver=solver, from_memory=state, max_steps=500, verbose=False, outcome_formatter=lambda o: f"{o.observation} - cost: {o.value.cost:.2f}", ) print("Cost: ", sum([v.cost for v in values])) print("CP done") all_results[original_domain_name]["cp"].append( sum([v.cost for v in values])) # CP SGS for train_dom in training_domains: do_solver = SolvingMethod.CP train_dom.set_inplace_environment(False) state = train_dom.get_initial_state() solver = DOSolver( policy_method_params=PolicyMethodParams( base_policy_method=BasePolicyMethod.SGS_STRICT, delta_index_freedom=0, delta_time_freedom=0, ), method=do_solver, ) solver.solve(domain_factory=lambda: train_dom) print(do_solver) domain: RCPSP = test_domain domain.set_inplace_environment(False) states, actions, values = rollout_episode( domain=domain, solver=solver, from_memory=state, max_steps=500, verbose=False, outcome_formatter=lambda o: f"{o.observation} - cost: {o.value.cost:.2f}", ) print("Cost: ", sum([v.cost for v in values])) print("CP_SGS done") all_results[original_domain_name]["cp_sgs"].append( sum([v.cost for v in values])) # PILE domain: RCPSP = test_domain do_solver = SolvingMethod.PILE domain.set_inplace_environment(False) state = domain.get_initial_state() solver = DOSolver( policy_method_params=PolicyMethodParams( base_policy_method=BasePolicyMethod.FOLLOW_GANTT, delta_index_freedom=0, delta_time_freedom=0, ), method=do_solver, ) solver.solve(domain_factory=lambda: domain) print(do_solver) states, actions, values = rollout_episode( domain=domain, solver=solver, from_memory=state, max_steps=500, verbose=False, outcome_formatter=lambda o: f"{o.observation} - cost: {o.value.cost:.2f}", ) print("Cost: ", sum([v.cost for v in values])) print("PILE done") all_results[original_domain_name]["pile"].append( sum([v.cost for v in values])) # GPHH with open("cp_reference_permutations") as json_file: cp_reference_permutations = json.load(json_file) with open("cp_reference_makespans") as json_file: cp_reference_makespans = json.load(json_file) for i in range(repeat_runs): domain.set_inplace_environment(False) set_feature = { FeatureEnum.EARLIEST_FINISH_DATE, FeatureEnum.EARLIEST_START_DATE, FeatureEnum.LATEST_FINISH_DATE, FeatureEnum.LATEST_START_DATE, FeatureEnum.N_PREDECESSORS, FeatureEnum.N_SUCCESSORS, FeatureEnum.ALL_DESCENDANTS, FeatureEnum.RESSOURCE_REQUIRED, FeatureEnum.RESSOURCE_AVG, FeatureEnum.RESSOURCE_MAX, # FeatureEnum.RESSOURCE_MIN FeatureEnum.RESSOURCE_NZ_MIN, } pset = PrimitiveSet("main", len(set_feature)) pset.addPrimitive(operator.add, 2) pset.addPrimitive(operator.sub, 2) pset.addPrimitive(operator.mul, 2) pset.addPrimitive(protected_div, 2) pset.addPrimitive(max_operator, 2) pset.addPrimitive(min_operator, 2) pset.addPrimitive(operator.neg, 1) # pset.addPrimitive(operator.pow, 2) params_gphh = ParametersGPHH( set_feature=set_feature, set_primitves=pset, tournament_ratio=0.2, pop_size=40, n_gen=20, min_tree_depth=1, max_tree_depth=3, crossover_rate=0.7, mutation_rate=0.3, base_policy_method=BasePolicyMethod.SGS_READY, delta_index_freedom=0, delta_time_freedom=0, deap_verbose=True, evaluation=EvaluationGPHH.SGS_DEVIATION, permutation_distance=PermutationDistance.KTD # permutation_distance = PermutationDistance.KTD_HAMMING ) solver = GPHH( training_domains=training_domains, weight=-1, verbose=False, reference_permutations=cp_reference_permutations, # reference_makespans=cp_reference_makespans, training_domains_names=training_domains_names, params_gphh=params_gphh # set_feature=set_feature) ) solver.solve(domain_factory=lambda: domain) domain: RCPSP = test_domain domain.set_inplace_environment(False) state = domain.get_initial_state() solver.set_domain(domain) states, actions, values = rollout_episode( domain=domain, max_steps=1000, solver=solver, from_memory=state, verbose=False, outcome_formatter=lambda o: f"{o.observation} - cost: {o.value.cost:.2f}", ) print("One GPHH done") print("Best evolved heuristic: ", solver.best_heuristic) print("Cost: ", sum([v.cost for v in values])) all_results[original_domain_name]["gphh"].append( sum([v.cost for v in values])) print("##### ALL RESULTS #####") for test_domain_str in test_domain_names: print(test_domain_str, " :") for algo_key in all_results[test_domain_str].keys(): print("\t", algo_key, ": ") print("\t\t all runs:", all_results[test_domain_str][algo_key]) print("\t\t mean:", np.mean(all_results[test_domain_str][algo_key]))