Skip to content

Commit 7f584a0

Browse files
committed
Update metacluster package
1 parent 24a9080 commit 7f584a0

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

metacluster/metacluster.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ class MetaCluster:
1919
2020
Parameters
2121
----------
22-
list_optimizer: list, default = None
22+
list_optimizer: list, tuple, default = None
2323
List of strings that represent class optimizer or list of instance of Optimizer class from Mealpy library.
2424
Current supported optimizers, please check it here: https://github.com/thieu1995/mealpy
2525
If a custom optimizer is passed, make sure it is an instance of `Optimizer` class.
2626
Please use this to get supported optimizers: MetaCluster.get_support(name="optimizer")
2727
28-
list_paras: list, default=None
28+
list_paras: list, tuple, default=None
2929
List of dictionaries that present the parameters of each Optimizer class.
3030
You can set it to None to use all of default parameters in Mealpy library.
3131
32-
list_obj : list, default=None
32+
list_obj: list, tuple, default=None
3333
List of strings that represent objective name.
3434
Current supported objectives, please check it here: https://github.com/thieu1995/permetrics
3535
Please use this to get supported objectives: MetaCluster.get_support(name="obj")
3636
37-
n_trials : int, default=5
37+
n_trials: int, default=5
3838
The number of runs for each optimizer for each objective
3939
40-
seed : int, default=20
40+
seed: int, default=20
4141
Determines random number generation for the whole program. Use an int to make the randomness deterministic.
4242
4343
Examples
@@ -76,7 +76,7 @@ class MetaCluster:
7676
"all_mean": "get_clusters_all_mean", "all_majority": "get_clusters_all_majority"},
7777
"obj": cluster.get_all_clustering_metrics(),
7878
"metrics": cluster.get_all_clustering_metrics(),
79-
"optimizer": list(mu.get_all_optimizers().keys())
79+
"optimizer": list(mu.get_all_optimizers(verbose=False).keys())
8080
}
8181

8282
FILENAME_LABELS = "result_labels"
@@ -87,8 +87,9 @@ class MetaCluster:
8787
HYPHEN_SYMBOL = "="
8888

8989
def __init__(self, list_optimizer=None, list_paras=None, list_obj=None, n_trials=5, seed=20):
90-
self.list_optimizer, self.list_paras = self._set_list_optimizer(list_optimizer, list_paras)
91-
self.list_obj = self._set_list_function(list_obj, name="objectives")
90+
self.list_optimizer = list_optimizer
91+
self.list_paras = list_paras
92+
self.list_obj = list_obj
9293
self.n_trials = n_trials
9394
self.seed = seed
9495

@@ -118,7 +119,7 @@ def _set_list_function(self, list_obj=None, name="objectives"):
118119
list_obj0.append(obj)
119120
if len(list_obj0) > 0:
120121
print(f"MetaCluster doesn't support {name}: {list_obj0}")
121-
return list_obj1
122+
self.list_obj = list_obj1
122123

123124
def _set_list_optimizer(self, list_optimizer=None, list_paras=None):
124125
if type(list_optimizer) not in (list, tuple):
@@ -131,18 +132,21 @@ def _set_list_optimizer(self, list_optimizer=None, list_paras=None):
131132
list_opts = []
132133
for idx, opt in enumerate(list_optimizer):
133134
if type(opt) is str:
134-
opt_class = mu.get_optimizer_by_name(opt)
135+
opt_class = mu.get_optimizer_by_class(opt)
135136
if type(list_paras[idx]) is dict:
136137
list_opts.append(opt_class(**list_paras[idx]))
137138
else:
138-
list_opts.append(opt_class(epoch=100, pop_size=30))
139+
list_opts.append(opt_class(epoch=250, pop_size=20))
139140
elif isinstance(opt, mu.Optimizer):
140141
if type(list_paras[idx]) is dict:
142+
if "name" in list_paras[idx]: # Check if key exists and remove it
143+
opt.name = list_paras[idx].pop("name")
141144
opt.set_parameters(list_paras[idx])
142145
list_opts.append(opt)
143146
else:
144147
raise TypeError(f"optimizer needs to set as a string and supported by Mealpy library.")
145-
return list_opts, list_paras
148+
self.list_optimizer = list_opts
149+
self.list_paras = list_paras
146150

147151
def __run__(self, optimizer, problem, mode="single", n_workers=2, termination=None):
148152
optimizer.solve(problem, mode=mode, n_workers=n_workers, termination=termination, seed=self.seed)
@@ -194,6 +198,10 @@ def execute(self, data=None, cluster_finder="elbow", list_metric=None, save_path
194198
termination : dict or None, default = None
195199
The termination dictionary or an instance of Termination class. It is for Optimizer belongs to Mealpy library.
196200
"""
201+
## Set up optimizer and objectives
202+
self._set_list_optimizer(self.list_optimizer, self.list_paras)
203+
self._set_list_function(self.list_obj, name="objectives")
204+
197205
if data.y is not None:
198206
n_clusters = len(np.unique(data.y))
199207
else:

0 commit comments

Comments
 (0)