Skip to content

Commit

Permalink
Name mps file based on scenario title
Browse files Browse the repository at this point in the history
Fixes #112
  • Loading branch information
machristie committed Apr 5, 2021
1 parent 2fadeca commit 917263c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
13 changes: 11 additions & 2 deletions simccs_maptool/simccs_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import glob
import io
import logging
import os
import re
import shutil

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -85,7 +87,7 @@ def get_sinks_file(scenario_dir):


def get_mps_file(scenario_dir):
return os.path.join(scenario_dir, "MIP", "cap.mps")
return next(glob.iglob(os.path.join(scenario_dir, "MIP", "*.mps")), None)


def get_candidate_network_file(scenario_dir):
Expand Down Expand Up @@ -135,9 +137,11 @@ def _get_scenario_path_components(scenario_dir):


def write_mps_file(
scenario_dir, capital_recovery_rate=0.1, num_years=10, capacity_target=5
scenario_dir, capital_recovery_rate=0.1, num_years=10, capacity_target=5, filename=None
):

if filename is not None and not filename.endswith(".mps"):
raise Exception(f"Invalid filename {filename}: must end with .mps")
try:
basepath, dataset_dirname, scenario = _get_scenario_path_components(
scenario_dir
Expand Down Expand Up @@ -166,6 +170,11 @@ def write_mps_file(
scenario,
"c", # modelVersion = "c" for capture
)
if filename is not None:
mps_fullpath = get_mps_file(scenario_dir)
normalized_filename = re.sub(r'[^\w.]+', '_', filename)
os.rename(mps_fullpath,
os.path.join(os.path.dirname(mps_fullpath), normalized_filename))
except Exception as e:
logger.exception(
"Error occurred when calling writeCapPriceMPS: " + str(e.stacktrace)
Expand Down
11 changes: 8 additions & 3 deletions simccs_maptool/templates/simccs_maptool/build.html
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@


//generate MPS file
function generatempsfile(panelid,showmodal=1,paras=[]) {
function generatempsfile(panelid,showmodal=1,paras=[], filename=null) {
// get the data from panelid
//var source_selection = sourceselection_panel[panelid];
//var sink_selection = sinkselection_panel[panelid];
Expand Down Expand Up @@ -376,6 +376,9 @@
formData.set('sources', sourcedata);
formData.set('sinks', sinkdata);
formData.set('dataset', current_dataset_id);
if (filename) {
formData.set('mpsFilename', filename);
}

// check if needs run candidate network _cached
// Use the cachedCandidateNetwork if the selected sources and sinks are the same
Expand Down Expand Up @@ -431,12 +434,14 @@ <h5 class="modal-title">Generate MPS file</h5>
//console.log(panelid);
var cplexInputValues;
var mpsRequest = null;
// Name the mps file based on the scenario name
var filename = scenario_title_panel[panelid] + ".mps";
if (batchmode == 0) {
mpsRequest = generatempsfile(panelid,showmodal=0).then((result) => {cplexInputValues = result});
mpsRequest = generatempsfile(panelid,showmodal=0,[],filename).then((result) => {cplexInputValues = result});
}
// force regenerate mps for batch mode
if (batchmode == 1) {
mpsRequest = generatempsfile(panelid,showmodal=0,paras).then((result) => {cplexInputValues = result});
mpsRequest = generatempsfile(panelid,showmodal=0,paras,filename).then((result) => {cplexInputValues = result});
}

mpsRequest.then(() => {
Expand Down
2 changes: 2 additions & 0 deletions simccs_maptool/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def generate_mps(request):
sinks = request.POST["sinks"]
dataset_id = request.POST["dataset"]
candidate_network = request.POST.get("candidateNetwork", None)
mps_filename = request.POST.get("mpsFilename", None)

with tempfile.TemporaryDirectory() as datasets_basepath:
dataset_dir = datasets.get_dataset_dir(dataset_id)
Expand Down Expand Up @@ -142,6 +143,7 @@ def generate_mps(request):
capital_recovery_rate=capital_recovery_rate,
num_years=num_years,
capacity_target=capacity_target,
filename=mps_filename,
)
with open(
simccs_helper.get_sources_file(scenario_dir)
Expand Down

0 comments on commit 917263c

Please sign in to comment.