Skip to content

Commit

Permalink
Updating convergence tester
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlee94 committed Nov 22, 2023
1 parent a1ccd98 commit 7b99a10
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 77 deletions.
19 changes: 16 additions & 3 deletions idaes/core/util/model_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2962,6 +2962,14 @@ def psweep_runner_validator(val):
description="Options to pass to IPOPT.",
),
)
CACONFIG.declare(
"halt_on_error",
ConfigValue(
default=False,
domain=bool,
doc="Whether to halt execution of parameter sweep on encountering a solver error (default=False).",
),
)


class ConvergenceAnalysis:
Expand All @@ -2986,14 +2994,19 @@ def __init__(self, model, **kwargs):

self._model = model

solver = SolverFactory("ipopt")
if self.config.solver_options is not None:
solver.options = self.config.solver_options

self._psweep = self.config.workflow_runner(
input_specification=self.config.input_specification,
build_model=self._build_model,
rebuild_model=True,
run_model=self._run_model,
collect_results=self._collect_results,
failure_recourse=self._recourse,
solver="ipopt",
solver_options=self.config.solver_options,
halt_on_error=self.config.halt_on_error,
handle_solver_error=self._recourse,
solver=solver,
)

@property
Expand Down
4 changes: 4 additions & 0 deletions idaes/core/util/parameter_sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
# Set up logger
_log = idaeslog.getLogger(__name__)

# TODO: Timeouts
# TODO: Progress bar/indicator
# TODO: Re-initialize option/callback


class ParameterSweepSpecification(object):
"""Defines a set of input variables/parameters and values to be used in
Expand Down
120 changes: 46 additions & 74 deletions idaes/core/util/tests/test_model_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import math
import numpy as np
import pytest
from collections import OrderedDict
import os
from copy import deepcopy

Expand Down Expand Up @@ -1944,20 +1943,13 @@ def test_report_irreducible_degenerate_sets_none(self, model, scip_solver):

ca_dict = {
"specification": {
"inputs": OrderedDict(
[
(
"v2",
OrderedDict(
[
("pyomo_path", "v2"),
("lower", 2),
("upper", 6),
]
),
)
]
),
"inputs": {
"v2": {
"pyomo_path": "v2",
"lower": 2,
"upper": 6,
},
},
"sampling_method": "UniformSampling",
"sample_size": [2],
"samples": {
Expand All @@ -1968,31 +1960,21 @@ def test_report_irreducible_degenerate_sets_none(self, model, scip_solver):
"column_names": [None],
},
},
"results": OrderedDict(
[
(
0,
{
"solved": True,
"results": 2,
},
),
(
1,
{
"solved": True,
"results": 6,
},
),
]
),
"results": {
0: {
"solved": True,
"results": 2,
},
1: {
"solved": True,
"results": 6,
},
},
}

ca_res = {
"specification": {
"inputs": OrderedDict(
[("v2", OrderedDict([("pyomo_path", "v2"), ("lower", 2), ("upper", 6)]))]
),
"inputs": {"v2": {"pyomo_path": "v2", "lower": 2, "upper": 6}},
"sampling_method": "UniformSampling",
"sample_size": [2],
"samples": {
Expand All @@ -2003,36 +1985,28 @@ def test_report_irreducible_degenerate_sets_none(self, model, scip_solver):
"column_names": [None],
},
},
"results": OrderedDict(
[
(
0,
{
"solved": False,
"results": {
"iters": 7,
"iters_in_restoration": 4,
"iters_w_regularization": 0,
"time": 0.0,
"numerical_issues": True,
},
},
),
(
1,
{
"solved": False,
"results": {
"iters": 7,
"iters_in_restoration": 4,
"iters_w_regularization": 0,
"time": 0.0,
"numerical_issues": True,
},
},
),
]
),
"results": {
0: {
"solved": False,
"results": {
"iters": 7,
"iters_in_restoration": 4,
"iters_w_regularization": 0,
"time": 0.0,
"numerical_issues": True,
},
},
1: {
"solved": False,
"results": {
"iters": 7,
"iters_in_restoration": 4,
"iters_w_regularization": 0,
"time": 0.0,
"numerical_issues": True,
},
},
},
}


Expand All @@ -2055,7 +2029,7 @@ def test_init(self, model):

assert ca._model is model
assert isinstance(ca._psweep, SequentialSweepRunner)
assert isinstance(ca.results, OrderedDict)
assert isinstance(ca.results, dict)
assert ca.config.input_specification is None
assert ca.config.solver_options is None

Expand Down Expand Up @@ -2184,7 +2158,7 @@ def test_run_convergence_analysis(self, model):

ca.run_convergence_analysis()

assert isinstance(ca.results, OrderedDict)
assert isinstance(ca.results, dict)
assert len(ca.results) == 4

# Ignore time, as it is too noisy to test
Expand Down Expand Up @@ -2232,12 +2206,10 @@ def ca_with_results(self):
input_specification=spec,
)

ca._psweep._results = OrderedDict(
{
0: {"solved": True, "results": 2},
1: {"solved": True, "results": 6},
}
)
ca._psweep._results = {
0: {"solved": True, "results": 2},
1: {"solved": True, "results": 6},
}

return ca

Expand Down

0 comments on commit 7b99a10

Please sign in to comment.