Skip to content

Commit

Permalink
Add n_jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
YamLyubov committed Aug 1, 2023
1 parent 4bb6ebd commit 89802fd
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
73 changes: 73 additions & 0 deletions examples/tuning_example.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 6 additions & 2 deletions golem/core/tuning/iopt_tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 89802fd

Please sign in to comment.