Scenario Creation
In addition to model-based parameter estimation, parmest can create
scenarios for use in optimization under uncertainty. To do this, one
first creates an Estimator
object, then a ScenarioCreator
object, which has methods to add ParmestScen
scenario objects to a
ScenarioSet
object, which can write them to a csv file or output them
via an iterator method.
This example is in the semibatch subdirectory of the examples directory in
the file scenario_example.py
. It creates a csv file with scenarios that
correspond one-to-one with the experiments used as input data. It also
creates a few scenarios using the bootstrap methods and outputs prints the
scenarios to the screen, accessing them via the ScensItator
a print
# ___________________________________________________________________________
#
# Pyomo: Python Optimization Modeling Objects
# Copyright (c) 2008-2024
# 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.
# ___________________________________________________________________________
import json
from os.path import join, abspath, dirname
import pyomo.contrib.parmest.parmest as parmest
from pyomo.contrib.parmest.examples.semibatch.semibatch import generate_model
import pyomo.contrib.parmest.scenariocreator as sc
def main():
# Vars to estimate in parmest
theta_names = ['k1', 'k2', 'E1', 'E2']
# Data: list of dictionaries
data = []
file_dirname = dirname(abspath(str(__file__)))
for exp_num in range(10):
fname = join(file_dirname, 'exp' + str(exp_num + 1) + '.out')
with open(fname, 'r') as infile:
d = json.load(infile)
data.append(d)
pest = parmest.Estimator(generate_model, data, theta_names)
scenmaker = sc.ScenarioCreator(pest, "ipopt")
# Make one scenario per experiment and write to a csv file
output_file = "scenarios.csv"
experimentscens = sc.ScenarioSet("Experiments")
scenmaker.ScenariosFromExperiments(experimentscens)
experimentscens.write_csv(output_file)
# Use the bootstrap to make 3 scenarios and print
bootscens = sc.ScenarioSet("Bootstrap")
scenmaker.ScenariosFromBootstrap(bootscens, 3)
for s in bootscens.ScensIterator():
print("{}, {}".format(s.name, s.probability))
for n, v in s.ThetaVals.items():
print(" {}={}".format(n, v))
if __name__ == "__main__":
main()
Note
This example may produce an error message your version of Ipopt is not based on a good linear solver.