Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
OptiOrch
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Zizhe Wang
OptiOrch
Commits
4ea9e608
Commit
4ea9e608
authored
1 year ago
by
Zizhe Wang
Browse files
Options
Downloads
Patches
Plain Diff
refactor import all algorithms from pymoo dynamically
parent
95c4b960
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/optimization_libraries.py
+23
-22
23 additions, 22 deletions
src/optimization_libraries.py
with
23 additions
and
22 deletions
src/optimization_libraries.py
+
23
−
22
View file @
4ea9e608
...
...
@@ -7,27 +7,28 @@
# #
############################################
from
pymoo.algorithms.moo.nsga2
import
NSGA2
from
pymoo.algorithms.moo.nsga3
import
NSGA3
from
pymoo.algorithms.soo.nonconvex.cmaes
import
CMAES
from
scipy.optimize
import
differential_evolution
,
minimize
as
scipy_minimize
import
importlib
import
pkgutil
from
pymoo.algorithms
import
moo
def
initialize_algorithm
(
library
,
algorithm_name
,
pop_size
):
if
library
==
'
pymoo
'
:
if
algorithm_
name
==
'
NSGA2
'
:
return
NSGA2
(
pop_size
=
pop_size
)
elif
algorithm_name
==
'
NSGA3
'
:
return
NSGA3
(
pop_size
=
pop_siz
e
)
elif
algorithm_name
==
'
CMAES
'
:
return
CMAES
(
pop_size
=
pop_size
)
else
:
raise
ValueError
(
f
"
Unsupported algorithm:
{
algorithm_name
}
"
)
elif
library
==
'
scipy
'
:
if
algorithm_name
==
'
de
'
:
return
differential_evolution
elif
algorithm_name
==
'
minimize
'
:
return
scipy_minimize
els
e
:
raise
ValueError
(
f
"
Unsupported
algorithm
:
{
algorithm_
name
}
"
)
def
initialize_algorithm
(
algorithm_name
,
pop_size
=
None
):
# Dynamically import all modules in pymoo.algorithms.moo
algorithm_
modules
=
{}
for
_
,
module_name
,
_
in
pkgutil
.
iter_modules
(
moo
.
__path__
):
module
=
importlib
.
import_module
(
f
"
pymoo.algorithms.moo.
{
module_name
}
"
)
for
attribute_name
in
dir
(
modul
e
)
:
attribute
=
getattr
(
module
,
attribute_name
)
if
isinstance
(
attribute
,
type
):
# Check if it's a class
algorithm_modules
[
attribute_name
.
lower
()]
=
attribute
# Check if the algorithm name is in the imported modules
if
algorithm_name
.
lower
()
not
in
algorithm_modules
:
raise
ValueError
(
f
"
Algorithm
{
algorithm_name
}
is not supported.
"
)
algorithm_class
=
algorithm_modules
[
algorithm_name
.
lower
()]
if
pop_siz
e
:
algorithm
=
algorithm_
class
(
pop_size
=
pop_size
)
else
:
raise
ValueError
(
f
"
Unsupported optimization library:
{
library
}
"
)
\ No newline at end of file
algorithm
=
algorithm_class
()
return
algorithm
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment