From d6f3a0e7859cc5ac16979a29b2ce0151b5da6385 Mon Sep 17 00:00:00 2001 From: fangop Date: Wed, 7 Jun 2023 16:55:18 +0800 Subject: [PATCH 1/5] fix(solvers): Add solvers list for testing all the solvers. --- pystoned/utils/tools.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/pystoned/utils/tools.py b/pystoned/utils/tools.py index 9dcc034..b3f541d 100644 --- a/pystoned/utils/tools.py +++ b/pystoned/utils/tools.py @@ -6,7 +6,6 @@ from ..constant import CET_ADDI, CET_MULT, CET_Model_Categories, OPT_LOCAL, OPT_DEFAULT, RTS_CRS __email_re = compile(r'([^@]+@[^@]+\.[a-zA-Z0-9]+)$') - def set_neos_email(address): """pass email address to NEOS server @@ -23,6 +22,7 @@ def set_neos_email(address): def optimize_model(model, email, cet, solver=OPT_DEFAULT): + optimization_status = 0 if not set_neos_email(email): if solver is not OPT_DEFAULT: assert_solver_available_locally(solver) @@ -37,13 +37,27 @@ def optimize_model(model, email, cet, solver=OPT_DEFAULT): return solver_instance.solve(model, tee=True), 1 else: if solver is OPT_DEFAULT and cet is CET_ADDI: - solver = "mosek" + solvers = ["cplex","cbc","mosek"] elif solver is OPT_DEFAULT and cet == CET_MULT: - solver = "knitro" - solver_instance = SolverManagerFactory('neos') + solvers = ["knitro"] + else: + solvers = [solver] + for solver in solvers: + model, optimization_status = __try_remote_solver(model, cet, solver) + if optimization_status == 1: + return model, optimization_status + raise Exception("Remote solvers are temporarily not available.") + + +def __try_remote_solver(model, cet, solver): + solver_instance = SolverManagerFactory('neos') + try: print("Estimating the {} remotely with {} solver.".format( CET_Model_Categories[cet], solver), flush=True) return solver_instance.solve(model, tee=True, opt=solver), 1 + except: + print("Remote {} solver is not available now.".format(solver)) + return model, 0 def trans_list(li): From b204194e550b9894621ff2b3925262b6c837ccfd Mon Sep 17 00:00:00 2001 From: fangop Date: Wed, 7 Jun 2023 16:58:12 +0800 Subject: [PATCH 2/5] style(tools): Align with autopep8 --- pystoned/utils/tools.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pystoned/utils/tools.py b/pystoned/utils/tools.py index b3f541d..84431bd 100644 --- a/pystoned/utils/tools.py +++ b/pystoned/utils/tools.py @@ -6,6 +6,7 @@ from ..constant import CET_ADDI, CET_MULT, CET_Model_Categories, OPT_LOCAL, OPT_DEFAULT, RTS_CRS __email_re = compile(r'([^@]+@[^@]+\.[a-zA-Z0-9]+)$') + def set_neos_email(address): """pass email address to NEOS server @@ -37,13 +38,14 @@ def optimize_model(model, email, cet, solver=OPT_DEFAULT): return solver_instance.solve(model, tee=True), 1 else: if solver is OPT_DEFAULT and cet is CET_ADDI: - solvers = ["cplex","cbc","mosek"] + solvers = ["cplex", "cbc", "mosek"] elif solver is OPT_DEFAULT and cet == CET_MULT: solvers = ["knitro"] else: solvers = [solver] for solver in solvers: - model, optimization_status = __try_remote_solver(model, cet, solver) + model, optimization_status = __try_remote_solver( + model, cet, solver) if optimization_status == 1: return model, optimization_status raise Exception("Remote solvers are temporarily not available.") From fe5ee5b7742e27578b14fcfc97d1e29b82fd80b2 Mon Sep 17 00:00:00 2001 From: Fang Yu Hsueh Date: Thu, 16 Nov 2023 20:13:40 +0800 Subject: [PATCH 3/5] feat(utils): Add check local solver --- pystoned/utils/tools.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pystoned/utils/tools.py b/pystoned/utils/tools.py index 84431bd..8401f7c 100644 --- a/pystoned/utils/tools.py +++ b/pystoned/utils/tools.py @@ -2,10 +2,14 @@ from re import compile from os import environ import numpy as np -from pyomo.opt import SolverFactory, SolverManagerFactory +from pyomo.opt import SolverFactory, SolverManagerFactory, check_available_solvers + from ..constant import CET_ADDI, CET_MULT, CET_Model_Categories, OPT_LOCAL, OPT_DEFAULT, RTS_CRS __email_re = compile(r'([^@]+@[^@]+\.[a-zA-Z0-9]+)$') +def check_local_solver(solver="mosek"): + return bool(check_available_solvers(solver)) + def set_neos_email(address): """pass email address to NEOS server From b9555aae53e43760aa71ab7ebbe6ddc6442c2382 Mon Sep 17 00:00:00 2001 From: Fang Yu Hsueh Date: Thu, 16 Nov 2023 20:14:16 +0800 Subject: [PATCH 4/5] feat(utils): Add get remote solvers list --- pystoned/utils/tools.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pystoned/utils/tools.py b/pystoned/utils/tools.py index 8401f7c..d357863 100644 --- a/pystoned/utils/tools.py +++ b/pystoned/utils/tools.py @@ -7,10 +7,21 @@ from ..constant import CET_ADDI, CET_MULT, CET_Model_Categories, OPT_LOCAL, OPT_DEFAULT, RTS_CRS __email_re = compile(r'([^@]+@[^@]+\.[a-zA-Z0-9]+)$') +def get_remote_solvers(): + import pyomo.neos.kestrel + kestrel = pyomo.neos.kestrel.kestrelAMPL() + return list( + set( + [ + name.split(":")[0].lower() + for name in kestrel.solvers() + ] + ) + ) + def check_local_solver(solver="mosek"): return bool(check_available_solvers(solver)) - def set_neos_email(address): """pass email address to NEOS server From 452293b6478a544cbfd7c267cf607c666aa18896 Mon Sep 17 00:00:00 2001 From: Fang Yu Hsueh Date: Thu, 16 Nov 2023 20:14:45 +0800 Subject: [PATCH 5/5] feat(utils): Add check remote solver --- pystoned/utils/tools.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pystoned/utils/tools.py b/pystoned/utils/tools.py index d357863..7c14ffe 100644 --- a/pystoned/utils/tools.py +++ b/pystoned/utils/tools.py @@ -19,6 +19,10 @@ def get_remote_solvers(): ) ) +def check_remote_solver(solver="mosek"): + solver_list = get_remote_solvers() + return bool(solver in solver_list) + def check_local_solver(solver="mosek"): return bool(check_available_solvers(solver))