-
Notifications
You must be signed in to change notification settings - Fork 276
Implement linear tree surrogate #1657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bammari
wants to merge
11
commits into
IDAES:main
Choose a base branch
from
bammari:implement-linear-tree-surrogate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9f8ec5a
Implemented Linear Tree Surrogate Class. Refactored messages in OMLT …
bammari bb4149e
Black
bammari 5a89544
Merge branch 'IDAES:main' into implement-linear-tree-surrogate
bammari fa64a90
Docstrings cleanup
bammari 71f8fea
Merge branch 'implement-linear-tree-surrogate' of https://github.com/…
bammari 897d168
Merge branch 'main' into implement-linear-tree-surrogate
bpaul4 6e86366
Update setup.py
bammari 1217ce2
Update setup.py
bammari 65a51e0
Update setup.py
bammari 5f5bc4c
Update setup.py
bammari f78c640
Merge branch 'main' into implement-linear-tree-surrogate
ksbeattie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,232 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ################################################################################# | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # The Institute for the Design of Advanced Energy Systems Integrated Platform | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Framework (IDAES IP) was produced under the DOE Institute for the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Design of Advanced Energy Systems (IDAES). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Copyright (c) 2018-2024 by the software owners: The Regents of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # University of California, through Lawrence Berkeley National Laboratory, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # National Technology & Engineering Solutions of Sandia, LLC, Carnegie Mellon | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # University, West Virginia University Research Corporation, et al. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # All rights reserved. Please see the files COPYRIGHT.md and LICENSE.md | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # for full copyright and license information. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ################################################################################# | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Interface for importing Linear-tree models into IDAES | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # pylint: disable=missing-class-docstring | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # pylint: disable=missing-function-docstring | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import json | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import os.path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import pickle | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import pandas as pd | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from pyomo.common.dependencies import attempt_import | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from idaes.core.surrogate.sampling.scaling import OffsetScaler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from idaes.core.surrogate.omlt_base_surrogate_class import OMLTSurrogate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lt, lt_available = attempt_import("lineartree") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| omlt, omlt_available = attempt_import("omlt") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if omlt_available: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from omlt.linear_tree import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| LinearTreeDefinition, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| LinearTreeGDPFormulation, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| LinearTreeHybridBigMFormulation, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class LinearTreeSurrogate(OMLTSurrogate): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lt_model, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input_labels, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output_labels, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input_bounds, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input_scaler=None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output_scaler=None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Standard SurrogateObject for surrogates based on Linear Tree models. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Utilizes the OMLT framework for importing Linear Tree models to IDAES. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Contains methods to both populate a Pyomo Block with constraints | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| representing the surrogate and to evaluate the surrogate a set of user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| provided points. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| This constructor should only be used when first creating the surrogate within IDAES. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Once created, this object can be stored to disk using save_to_folder and loaded | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with load_from_folder | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lt_model: Linear-tree model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| This is the Linear-tree model that will be loaded. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input_labels: list of str | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The ordered list of labels corresponding to the inputs in the linear-tree model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output_labels: list of str | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The ordered list of labels corresponding to the outputs in the linear-tree model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input_bounds: None of dict of tuples | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Keys correspond to each of the input labels and values are the tuples of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bounds (lb, ub) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input_scaler: None or OffsetScaler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The scaler to be used for the inputs. If None, then no scaler is used | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output_scaler: None of OffsetScaler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The scaler to be used for the outputs. If None, then no scaler is used | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+64
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| super().__init__( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input_labels=input_labels, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output_labels=output_labels, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input_bounds=input_bounds, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input_scaler=input_scaler, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output_scaler=output_scaler, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self._lt_model = lt_model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def populate_block(self, block, additional_options=None): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Method to populate a Pyomo Block with the linear-tree model constraints. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| block: Pyomo Block component | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The block to be populated with variables and/or constraints. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| additional_options: dict or None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| If not None, then should be a dict with the following keys; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'formulation': LinearTreeSurrogate.Formulation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The formulation to use with OMLT. Possible values are LINEAR_TREE_GDP_BIGM, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| LINEAR_TREE_GDP_HULL, LINEAR_TREE_GDP_MBIGM, or LINEAR_TREE_HYBRID_BIGM | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (default is LINEAR_TREE_GDP_BIGM) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| formulation = additional_options.pop( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "formulation", LinearTreeSurrogate.Formulation.LINEAR_TREE_GDP_BIGM | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| omlt_scaling, scaled_input_bounds = self.generate_omlt_scaling_objecets() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scaled_keys = list(scaled_input_bounds.keys()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| unscaled_keys = list(self.input_bounds().keys()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| unscaled_input_bounds = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scaled_keys[idx]: self.input_bounds()[unscaled_keys[idx]] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for idx, _ in enumerate(scaled_keys) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lt = LinearTreeDefinition( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self._lt_model, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scaling_object=omlt_scaling, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scaled_input_bounds=scaled_input_bounds, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| unscaled_input_bounds=unscaled_input_bounds, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if formulation == LinearTreeSurrogate.Formulation.LINEAR_TREE_GDP_BIGM: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| formulation_object = LinearTreeGDPFormulation(lt, transformation="bigm") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elif formulation == LinearTreeSurrogate.Formulation.LINEAR_TREE_GDP_HULL: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| formulation_object = LinearTreeGDPFormulation(lt, transformation="hull") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elif formulation == LinearTreeSurrogate.Formulation.LINEAR_TREE_HYBRID_BIGM: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| formulation_object = LinearTreeHybridBigMFormulation(lt) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'An unrecognized formulation "{}" was passed to ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "LinearTreeSurrogate.populate_block. Please pass a valid " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "formulation.".format(formulation) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.populate_block_with_model(block, formulation_object) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def evaluate_surrogate(self, inputs): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Method to evaluate linear-tree model at a set of input values. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inputs: numpy array of input values. First dimension of array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| must match the number of input variables. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| outputs: numpy array of values for all outputs evaluated at input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| points. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| x = inputs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if self._input_scaler is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| x = self._input_scaler.scale(x) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| y = self._lt_model.predict(x.to_numpy()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # y is a numpy array, make it a dataframe | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| y = pd.DataFrame( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data=y, columns=self.output_labels(), index=inputs.index, dtype="float64" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if self._output_scaler is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| y = self._output_scaler.unscale(y) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return y | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def save_to_folder(self, lt_folder_name, lt_model_name="idaes_linear_tree_model"): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Save the surrogate object to disk by providing the name of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| folder to contain the linear-tree model and additional IDAES metadata | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| folder_name: str | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The name of the folder to contain the linear-tree model and additional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| IDAES metadata | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with open(os.path.join(lt_folder_name, lt_model_name + ".pkl"), "wb") as FILE: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pickle.dump(self._lt_model, FILE) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| info = dict() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| info["input_scaler"] = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if self._input_scaler is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| info["input_scaler"] = self._input_scaler.to_dict() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| info["output_scaler"] = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if self._output_scaler is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| info["output_scaler"] = self._output_scaler.to_dict() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # serialize information from the base class | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| info["input_labels"] = self.input_labels() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| info["output_labels"] = self.output_labels() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| info["input_bounds"] = self.input_bounds() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with open(os.path.join(lt_folder_name, "idaes_info.json"), "w") as fd: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| json.dump(info, fd) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def load_from_folder(cls, lt_folder_name, lt_model_name="idaes_linear_tree_model"): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Load the surrogate object from disk by providing the name of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| folder holding the linear-tree model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| folder_name: str | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The name of the folder containing the Linear-tree model and additional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| IDAES metadata | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns: an instance of LinearTreeSurrogate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with open(os.path.join(lt_folder_name, lt_model_name + ".pkl"), "rb") as FILE: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lt_model = pickle.load(FILE) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with open(os.path.join(lt_folder_name, "idaes_info.json")) as fd: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| info = json.load(fd) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input_scaler = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if info["input_scaler"] is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input_scaler = OffsetScaler.from_dict(info["input_scaler"]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output_scaler = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if info["output_scaler"] is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output_scaler = OffsetScaler.from_dict(info["output_scaler"]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return LinearTreeSurrogate( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lt_model=lt_model, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input_labels=info["input_labels"], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output_labels=info["output_labels"], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input_bounds=info["input_bounds"], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input_scaler=input_scaler, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output_scaler=output_scaler, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def save_linear_tree_pickle(lt, path, name): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with open(os.path.join(path, "{}.pickle".format(name)), "wb") as file: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pickle.dump(lt, file) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def load_linear_tree_pickle(path, name): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with open(os.path.join(path, "{}.pickle".format(name)), "rb") as file: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lt = pickle.load(file) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return lt | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.