def inst_lite(): '''Model instance with full year NEM wide sets and input parameters only''' with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as tmp: shutil.copyfileobj(gzip.open('tests/inst_lite_data.json.gz'), tmp) options = model_options(nem_emit_limit=True, nem_ret_ratio=True, nem_ret_gwh=True) model = CreateModel('lite', options).create_model(test=True) data = DataPortal() data.load(filename=tmp.name) return model.create_instance(data)
def test_save_inputs_as_dat(self): import switch_mod.solve from pyomo.environ import DataPortal from testfixtures import compare (model, instance) = switch_mod.solve.load("test_dat") # To do: create a temporary file name for dat_path and delete it # when the test is complete. dat_path = "test_dat/complete_inputs.dat" utilities.save_inputs_as_dat(model, instance, save_path=dat_path) reloaded_data = DataPortal(model=model) reloaded_data.load(filename=dat_path) compare(reloaded_data.data(), instance.DataPortal.data())
def create_temoa_instance(self): """Create a single instance of Temoa.""" try: if self.options.keepPyomoLP: yield '\nSolver will write file: {}\n\n'.format( self.options.scenario + '.lp') SE.write('\nSolver will write file: {}\n\n'.format( self.options.scenario + '.lp')) self.txt_file.write('\nSolver will write file: {}\n\n'.format( self.options.scenario + '.lp')) yield 'Reading data files.' SE.write('[ ] Reading data files.') SE.flush() self.txt_file.write('Reading data files.') begin = time() duration = lambda: time() - begin modeldata = DataPortal(model=self.model) # Recreate the pyomo command's ability to specify multiple "dot dat" files # on the command lin for fname in self.options.dot_dat: if fname[-4:] != '.dat': msg = "InputError: expecting a dot dat (e.g., data.dat) file, found '{}'\n" raise Exception(msg.format(fname)) modeldata.load(filename=fname) yield '\t\t\t\t\t[%8.2f]\n' % duration() SE.write('\r[%8.2f]\n' % duration()) self.txt_file.write('[%8.2f]\n' % duration()) yield 'Creating Temoa model instance.' SE.write('[ ] Creating Temoa model instance.') SE.flush() self.txt_file.write('Creating Temoa model instance.') self.model.dual = Suffix(direction=Suffix.IMPORT) #self.model.rc = Suffix(direction=Suffix.IMPORT) #self.model.slack = Suffix(direction=Suffix.IMPORT) self.instance = self.model.create_instance(modeldata) yield '\t\t\t\t[%8.2f]\n' % duration() SE.write('\r[%8.2f]\n' % duration()) self.txt_file.write('[%8.2f]\n' % duration()) except Exception as model_exc: yield "Exception found in create_temoa_instance\n" SE.write("Exeception found in create_temoa_instance\n") self.txt_file.write("Exception found in create_temoa_instance\n") yield str(model_exc) SE.write(str(model_exc)) self.txt_file.write(str(model_exc)) raise model_exc
def test_save_inputs_as_dat(self): (model, instance) = switch_model.solve.main( args=["--inputs-dir", os.path.join('examples', '3zone_toy', 'inputs')], return_model=True, return_instance=True ) temp_dir = tempfile.mkdtemp(prefix="switch_test_") try: dat_path = os.path.join(temp_dir, "complete_inputs.dat") utilities.save_inputs_as_dat(model, instance, save_path=dat_path) reloaded_data = DataPortal(model=model) reloaded_data.load(filename=dat_path) compare(reloaded_data.data(), instance.DataPortal.data()) finally: shutil.rmtree(temp_dir)
def test_save_inputs_as_dat(self): (model, instance) = switch_model.solve.main(args=[ "--inputs-dir", os.path.join('examples', '3zone_toy', 'inputs') ], return_model=True, return_instance=True) temp_dir = tempfile.mkdtemp(prefix="switch_test_") try: dat_path = os.path.join(temp_dir, "complete_inputs.dat") utilities.save_inputs_as_dat(model, instance, save_path=dat_path) reloaded_data = DataPortal(model=model) reloaded_data.load(filename=dat_path) compare(reloaded_data.data(), instance.DataPortal.data()) finally: shutil.rmtree(temp_dir)
def test_save_inputs_as_dat(self): import switch_mod.solve from pyomo.environ import DataPortal from testfixtures import compare (model, instance) = switch_mod.solve.main(args=["--inputs-dir", "test_dat"], return_model=True, return_instance=True) temp_dir = tempfile.mkdtemp(prefix="switch_test_") try: dat_path = os.path.join(temp_dir, "complete_inputs.dat") utilities.save_inputs_as_dat(model, instance, save_path=dat_path) reloaded_data = DataPortal(model=model) reloaded_data.load(filename=dat_path) compare(reloaded_data.data(), instance.DataPortal.data()) finally: shutil.rmtree(temp_dir)
def convert_dat_to_csv(old_path, new_path): # define a dummy "model" where every "parameter" reports a dimension of 0. # otherwise Pyomo assumes they have dim=1 and looks for index values. class DummyModel(): def __getattr__(self, pname): return DummyParam() class DummyParam(): def dim(self): return 0 try: data = DataPortal(model=DummyModel()) data.load(filename=old_path) # this happens to be in a pandas-friendly format df = pandas.DataFrame(data.data()) df.to_csv(new_path, sep=',', na_rep='.', index=False) os.remove(old_path) except Exception as e: print('\nERROR converting {} to {}:\n{}'.format( old_path, new_path, e.message)) raise
def run_optimization(path_out): # Select Solver opt = SolverFactory('cbc') # Create DataPortal data = DataPortal() # Read Timeseries data.load(filename='input/timeseries_gas.csv', index='t', param='gas') data.load(filename='input/timeseries_heat_demand.csv', index='t', param='dem') data.load(filename='input/timeseries_spot.csv', index='t', param='spot') # Create Instanz instance = m.create_instance(data) # Solve Optimization Problem results = opt.solve(instance, symbolic_solver_labels=True, tee=True, load_solutions=True) # Write Timeseries df_output = pd.DataFrame() for t in instance.t.value: df_output.loc[t, 'gas_chp_old'] = instance.In_chp_old[t].value df_output.loc[t, 'power_chp_old'] = instance.Out1_chp_old[t].value df_output.loc[t, 'heat_chp_old'] = instance.Out2_chp_old[t].value df_output.loc[t, 'on_chp_old'] = instance.on_chp_old[t].value df_output.loc[t, 'StartCost_chp_old'] = instance.StartCost_chp_old[t].value df_output.loc[t, 'gas_chp_new'] = instance.In_chp_new[t].value df_output.loc[t, 'power_chp_new'] = instance.Out1_chp_new[t].value df_output.loc[t, 'heat_chp_new'] = instance.Out2_chp_new[t].value df_output.loc[t, 'on_chp_new'] = instance.on_chp_new[t].value df_output.loc[t, 'StartCost_chp_new'] = instance.StartCost_chp_new[t].value df_output.loc[t, 'gas_heat_plant'] = instance.In_heat_plant[t].value df_output.loc[t, 'power_heat_plant'] = instance.Out1_heat_plant[t].value df_output.loc[t, 'heat_heat_plant'] = instance.Out2_heat_plant[t].value df_output.loc[t, 'on_heat_plant'] = instance.on_heat_plant[t].value df_output.loc[t, 'StartCost_heat_plant'] = instance.StartCost_heat_plant[t].value for j in instance.j_store.value: df_output.loc[t, '_'.join([str(j), 'charge'])] = instance.store_charge[j, t].value df_output.loc[t, '_'.join([str(j), 'capacity'])] = instance.store_capacity[j, t].value df_output.loc[t, '_'.join([str(j), 'discharge'])] = instance.store_discharge[j, t].value df_output.loc[t, 'demand'] = instance.dem[t] df_output.loc[t, 'spot'] = instance.spot[t] df_output.loc[t, 'gas'] = instance.gas[t] # Export Timeseries df_output.to_csv(path_out+'timeseries.csv')
def scenario_inputs(inputs_directory): """loads in scenario data from csvs and formats as Pyomo DataPortal for abstract model input NOTE: if you create more params in the model, you'll have to make sure they get properly loaded here Arguments: inputs_directory {filepath} -- filepath of case directory with input csvs Returns: <class 'pyomo.dataportal.DataPortal.DataPortal'> -- Pyomo DataPortal """ data = DataPortal() data.load( filename=os.path.join(inputs_directory, "generators.csv"), index=model_competitive_test.dispatch_model.GENERATORS, param=( model_competitive_test.dispatch_model.capacity, model_competitive_test.dispatch_model.fuelcost, model_competitive_test.dispatch_model.pmin, model_competitive_test.dispatch_model.startcost, model_competitive_test.dispatch_model.canspin, model_competitive_test.dispatch_model.cannonspin, model_competitive_test.dispatch_model.minup, model_competitive_test.dispatch_model.mindown, model_competitive_test.dispatch_model.noloadcost, model_competitive_test.dispatch_model.ramp, model_competitive_test.dispatch_model.tonneCO2perMWh, model_competitive_test.dispatch_model.CO2price, model_competitive_test.dispatch_model.CO2dollarsperMWh, model_competitive_test.dispatch_model.zonelabel, model_competitive_test.dispatch_model.genco_index, model_competitive_test.dispatch_model.uc_index, ), ) data.load( filename=os.path.join(inputs_directory, "storage_resources.csv"), index=model_competitive_test.dispatch_model.STORAGE, param=( model_competitive_test.dispatch_model.discharge_max, model_competitive_test.dispatch_model.charge_max, model_competitive_test.dispatch_model.soc_max, model_competitive_test.dispatch_model.discharge_eff, model_competitive_test.dispatch_model.charge_eff, model_competitive_test.dispatch_model.storage_zone_label, model_competitive_test.dispatch_model.storage_index, ), ) data.load( filename=os.path.join(inputs_directory, "initialize_generators.csv"), param=( model_competitive_test.dispatch_model.commitinit, model_competitive_test.dispatch_model.upinit, model_competitive_test.dispatch_model.downinit, ), ) data.load( filename=os.path.join( inputs_directory, "generators_scheduled_availability.csv" ), param=( model_competitive_test.dispatch_model.scheduled_available, model_competitive_test.dispatch_model.capacity_time, model_competitive_test.dispatch_model.fuel_cost_time, ), ) data.load( filename=os.path.join(inputs_directory, "timepoints_index.csv"), index=model_competitive_test.dispatch_model.TIMEPOINTS, param=( model_competitive_test.dispatch_model.reference_bus, model_competitive_test.dispatch_model.reg_up_mw, model_competitive_test.dispatch_model.reg_down_mw, model_competitive_test.dispatch_model.flex_up_mw, model_competitive_test.dispatch_model.flex_down_mw, ), ) data.load( filename=os.path.join(inputs_directory, "zones.csv"), index=model_competitive_test.dispatch_model.ZONES, param=( model_competitive_test.dispatch_model.wind_cap, model_competitive_test.dispatch_model.solar_cap, model_competitive_test.dispatch_model.voltage_angle_max, model_competitive_test.dispatch_model.voltage_angle_min, ), ) data.load( filename=os.path.join(inputs_directory, "timepoints_zonal.csv"), param=( model_competitive_test.dispatch_model.gross_load, model_competitive_test.dispatch_model.wind_cf, model_competitive_test.dispatch_model.solar_cf, ), ) data.load( filename=os.path.join(inputs_directory, "transmission_lines.csv"), index=model_competitive_test.dispatch_model.TRANSMISSION_LINE, param=(model_competitive_test.dispatch_model.susceptance), ) data.load( filename=os.path.join(inputs_directory, "transmission_lines_hourly.csv"), param=( model_competitive_test.dispatch_model.transmission_from, model_competitive_test.dispatch_model.transmission_to, model_competitive_test.dispatch_model.transmission_from_capacity, model_competitive_test.dispatch_model.transmission_to_capacity, model_competitive_test.dispatch_model.hurdle_rate, ), ) data.load( filename=os.path.join(inputs_directory, "generator_segments.csv"), index=model_competitive_test.dispatch_model.GENERATORSEGMENTS, param=(model_competitive_test.dispatch_model.base_generator_segment_length), ) data.load( filename=os.path.join(inputs_directory, "generator_segment_marginalcost.csv"), param=( model_competitive_test.dispatch_model.generator_segment_length, model_competitive_test.dispatch_model.generator_marginal_cost, model_competitive_test.dispatch_model.previous_offer, model_competitive_test.dispatch_model.marginal_CO2, model_competitive_test.dispatch_model.CO2_damage, ), ) data.load( filename=os.path.join(inputs_directory, "case_index.csv"), index=model_competitive_test.dispatch_model.CASE, param=(model_competitive_test.dispatch_model.genco), ) return data
""" intsance of the break schedule problem """ from model import * from pyomo.opt import SolverFactory from pyomo.environ import DataPortal import numpy as np import pandas as pd path_in = 'input/' path_out = 'output/' """ Select a solver: cbc or glpk. """ opt = SolverFactory('cbc') """ Load the input data. """ data = DataPortal() data.load(filename=path_in + 'demand.csv', index='t', param='demand') data.load(filename=path_in + 'workers.csv', set=m.w) data.load(filename=path_in + 'capacities.csv', index=m.w, param='cap') data.load(filename=path_in + 'break_settings.csv', index=m.w, param=[ 'break_length', 'num_breaks', 'earliest_break_start', 'latest_break_end' ]) data.load(filename=path_in + 'shift_schedule.csv', param=m.shift_schedule, format='array') """ Create the instanz. """ instance = m.create_instance(data) """ Solve the optimization problem """ results = opt.solve(instance, symbolic_solver_labels=True,
from pyomo.environ import SolverFactory, DataPortal from DiseaseEstimation import model model.pprint() data = DataPortal(model=model) data.load(filename='DiseaseEstimation.dat') data.load(filename='DiseasePop.dat') instance = model.create(data) instance.pprint() solver = SolverFactory("ipopt") results = solver.solve(instance) instance.display()
def run_optimization(path_out): # Select Solver opt = SolverFactory('cbc') # Create DataPortal data = DataPortal() # Read timeseries data.load(filename='input/timeseries_gas.csv', index='t', param='gas') data.load(filename='input/timeseries_heat_demand.csv', index='t', param='dem') data.load(filename='input/timeseries_spot.csv', index='t', param='spot') # Create instanz instance = m.create_instance(data) # Solve the optimization problem results = opt.solve(instance, symbolic_solver_labels=True, tee=True, load_solutions=True) # Write timeseries df_output = pd.DataFrame() for t in instance.t.value: for j in instance.j_chp.value: df_output.loc[t, '_'.join([str(j), 'gas'])] = instance.gen_chp_gas[ j, t].value df_output.loc[ t, '_'.join([str(j), 'power'])] = instance.gen_chp_power[j, t].value df_output.loc[ t, '_'.join([str(j), 'heat'])] = instance.gen_chp_heat[j, t].value df_output.loc[t, '_'.join([str(j), 'oh'])] = instance.on_chp[j, t].value df_output.loc[ t, '_'.join([str(j), 'grid'])] = instance.gen_chp_gas[j, t].value for j in instance.j_heat_plant.value: df_output.loc[t, '_'.join([str(j), 'gas'] )] = instance.gen_heat_plant_gas[j, t].value df_output.loc[t, '_'.join([str(j), 'heat'] )] = instance.gen_heat_plant_heat[j, t].value df_output.loc[ t, '_'.join([str(j), 'oh'])] = instance.on_heat_plant[j, t].value df_output.loc[t, '_'.join([str(j), 'grid'] )] = instance.gen_heat_plant_gas[j, t].value for j in instance.j_store.value: df_output.loc[ t, '_'.join([str(j), 'charge'])] = instance.store_charge[j, t].value df_output.loc[t, '_'.join([str(j), 'capacity'] )] = instance.store_capacity[j, t].value df_output.loc[t, '_'.join([str(j), 'discharge'] )] = instance.store_discharge[j, t].value df_output.loc[t, 'demand'] = instance.dem[t] df_output.loc[t, 'spot'] = instance.spot[t] df_output.loc[t, 'gas'] = instance.gas[t] df_output.to_csv(path_out + 'timeseries.csv')