From 912419cd7f93c141df2c92007c42d28dbd370af9 Mon Sep 17 00:00:00 2001 From: Lyubov Yamshchikova Date: Fri, 21 Jul 2023 15:48:09 +0300 Subject: [PATCH] Add n_jobs --- examples/tuning_example.py | 73 +++++++++++++++++++++++++++++++++ golem/core/tuning/iopt_tuner.py | 8 +++- 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 examples/tuning_example.py diff --git a/examples/tuning_example.py b/examples/tuning_example.py new file mode 100644 index 00000000..386f6bee --- /dev/null +++ b/examples/tuning_example.py @@ -0,0 +1,73 @@ +from golem.core.optimisers.graph import OptNode, OptGraph +from golem.core.optimisers.objective import ObjectiveEvaluate, Objective +from golem.core.tuning.iopt_tuner import IOptTuner +from golem.core.tuning.search_space import SearchSpace +from test.unit.utils import ParamsSumMetric + + +def opt_graph_with_params(): + node_a = OptNode('a') + node_b = OptNode({'name': 'b', 'params': {'b2': 0.7, 'b3': 2}}) + node_c = OptNode('c', nodes_from=[node_a]) + node_d = OptNode('d', nodes_from=[node_b]) + node_final = OptNode('e', nodes_from=[node_c, node_d]) + graph = OptGraph(node_final) + return graph + + +def get_search_space(): + params_per_operation = { + 'a': { + 'a1': { + 'sampling-scope': [2, 7], + 'type': 'discrete' + }, + 'a2': { + 'sampling-scope': [1e-3, 1], + 'type': 'continuous' + }, + 'a3': { + 'sampling-scope': [['A', 'B', 'C']], + 'type': 'categorical' + } + }, + 'b': { + 'b1': { + 'sampling-scope': [["first", "second", "third"]], + 'type': 'categorical' + }, + 'b2': { + 'sampling-scope': [0.05, 1.0], + 'type': 'continuous' + }, + }, + 'e': { + 'e1': { + 'sampling-scope': [0.05, 1.0], + 'type': 'continuous' + }, + 'e2': { + 'sampling-scope': [0.05, 1.0], + 'type': 'continuous' + } + }, + 'k': { + 'k': { + 'sampling-scope': [1e-2, 10.0], + 'type': 'continuous' + } + }} + return SearchSpace(params_per_operation) + + +if __name__ == '__main__': + search_space = get_search_space() + graph = opt_graph_with_params() + # ищем такие параметры, чтобы их сумма была максимальна + obj_eval = ObjectiveEvaluate(Objective({'sum_metric': ParamsSumMetric.get_value})) + + tuner = IOptTuner(obj_eval, search_space, iterations=20, n_jobs=1) + tuned_graph = tuner.tune(graph) + + tuner = IOptTuner(obj_eval, search_space, iterations=20, n_jobs=4) + tuned_graph = tuner.tune(graph) diff --git a/golem/core/tuning/iopt_tuner.py b/golem/core/tuning/iopt_tuner.py index 02aa2dd5..cc49fea0 100644 --- a/golem/core/tuning/iopt_tuner.py +++ b/golem/core/tuning/iopt_tuner.py @@ -10,6 +10,7 @@ from iOpt.trial import Point, FunctionValue from golem.core.adapter import BaseOptimizationAdapter +from golem.core.optimisers.genetic.evaluation import determine_n_jobs from golem.core.optimisers.graph import OptGraph from golem.core.optimisers.objective import ObjectiveEvaluate from golem.core.tuning.search_space import SearchSpace, get_node_operation_parameter_label @@ -125,12 +126,14 @@ def __init__(self, objective_evaluate: ObjectiveEvaluate, iterations=iterations, n_jobs=n_jobs, deviation=deviation, **kwargs) + self.n_jobs = determine_n_jobs(self.n_jobs) self.solver_parameters = SolverParameters(r=np.double(r), eps=np.double(eps), itersLimit=iterations, evolventDensity=evolvent_density, epsR=np.double(eps_r), - refineSolution=refine_solution) + refineSolution=refine_solution, + numberOfParallelPoints=n_jobs) def tune(self, graph: DomainGraphForTune, show_progress: bool = True) -> DomainGraphForTune: graph = self.adapter.adapt(graph) @@ -155,7 +158,8 @@ def tune(self, graph: DomainGraphForTune, show_progress: bool = True) -> DomainG console_output = ConsoleOutputListener(mode='full') solver.AddListener(console_output) - solution = solver.Solve() + solver.Solve() + solution = solver.GetResults() best_point = solution.bestTrials[0].point best_parameters = problem.get_parameters_dict_from_iopt_point(best_point) final_graph = self.set_arg_graph(graph, best_parameters)