-
Notifications
You must be signed in to change notification settings - Fork 544
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
Merged
Merged
Changes from 16 commits
Commits
Show all changes
20 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
0a25e00
Run black
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,73 @@ | ||
# ___________________________________________________________________________ | ||
# | ||
# 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 floats interpolated from data at times_data in | ||
time_set. | ||
eslickj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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. | ||
|
||
Returns | ||
------- | ||
list | ||
If data are Pyomo components, this will return Pyomo expressions | ||
interpolation if data are floats, this will return floats. | ||
eslickj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
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.
Uh oh!
There was an error while loading. Please reload this page.