-
Notifications
You must be signed in to change notification settings - Fork 538
Add time interpolation to mpc data #3559
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
eslickj
wants to merge
19
commits into
Pyomo:main
Choose a base branch
from
eslickj:mpc_time_interp
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.
+184
−1
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0c339db
Add time interpolation to mpc data
c286a0a
Run black
58c60c4
Fix formatting
dab4a8c
Split interpolate to a seperate function.
e888fbb
Fix function call
fa88cfb
formatting
bbc4f8b
Merge branch 'main' into mpc_time_interp
jsiirola 60ca7c6
Merge branch 'main' of https://github.com/pyomo/pyomo into mpc_time_i…
789a795
Update function name and range check
d63dea3
Merge branch 'mpc_time_interp' of https://github.com/eslickj/pyomo in…
c673122
Fix ineq
19e9142
undo change
53e5af6
Merge branch 'main' of https://github.com/pyomo/pyomo into mpc_time_i…
3a4b9c5
Add test at inter pendpoints
b11f7d0
Merge branch 'main' into mpc_time_interp
blnicho 8230e5b
Merge branch 'main' into mpc_time_interp
blnicho 09124e4
Update pyomo/contrib/mpc/data/interpolation.py
eslickj 7eb0e97
Update pyomo/contrib/mpc/data/interpolation.py
eslickj 0e31c8d
Update doc string to include indexes
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# ___________________________________________________________________________ | ||
# | ||
# Pyomo: Python Optimization Modeling Objects | ||
# Copyright (c) 2008-2025 | ||
# National Technology and Engineering Solutions of Sandia, LLC | ||
# Under the terms of Contract DE-NA0003525 with National Technology and | ||
# Engineering Solutions of Sandia, LLC, the U.S. Government retains certain | ||
# rights in this software. | ||
# This software is distributed under the 3-clause BSD License. | ||
# ___________________________________________________________________________ | ||
|
||
from bisect import bisect_right | ||
|
||
|
||
def _get_time_index_vec(time_set, time_data): | ||
"""Get the position index of time_data above and below the times in | ||
time_set. This can be used to find positions of points to interpolate | ||
between. | ||
Parameters | ||
---------- | ||
time_set: iterable | ||
Time points to locate | ||
time_data: iterable | ||
Sorted time points to locate time_set in | ||
Returns | ||
------- | ||
numpy.array | ||
Position index of the first time in time_data greater than the | ||
corresponding points time_set. If a time is less than all the times | ||
in time_data return 1. If a time is greater than all times time_data | ||
set return the last index of time_data. | ||
""" | ||
pos = [None] * len(time_set) | ||
for i, t in enumerate(time_set): | ||
pos[i] = bisect_right(time_data, t) | ||
if pos[i] == 0: | ||
pos[i] = 1 | ||
elif pos[i] == len(time_data): | ||
pos[i] = len(time_data) - 1 | ||
return pos | ||
|
||
|
||
def _get_interp_expr_vec(time_set, time_data, data, indexes=None): | ||
"""Return an array of floats interpolated at the time points in time_set | ||
from data defined at time_data. | ||
Parameters | ||
---------- | ||
time_set: iterable | ||
Time points to locate | ||
time_data: iterable | ||
Sorted time points to locate time_set in | ||
data: iterable | ||
Data corresponding to times in time_data, must have the same | ||
length as time data. | ||
indexes: numpy.array | ||
Numpy array of position indexes of the time points to interpolate in the | ||
time data. The format is the same as returned by ``_get_time_index_vec()``. | ||
If this is None, ``_get_time_index_vec()`` is called. The reason to pass | ||
this is to avoid multiple position searches when interpolating multiple | ||
outputs with the same time points. | ||
Returns | ||
------- | ||
list | ||
If data are Pyomo components, this will return Pyomo expressions. | ||
If data are floats, this will return floats. | ||
""" | ||
if indexes is None: | ||
indexes = _get_time_index_vec(time_set, time_data) | ||
expr = [None] * len(time_set) | ||
for i, (h, t) in enumerate(zip(indexes, time_set)): | ||
l = h - 1 | ||
expr[i] = data[l] + (data[h] - data[l]) / (time_data[h] - time_data[l]) * ( | ||
t - time_data[l] | ||
) | ||
return expr |
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.
indexes
isn't described in the docstringThere 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.
Is
indexes
used? If not, can it be removed?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.
It is used. Updated doc string.