def _read_solution(self, symbols_filename, solution_filename): """ Parses an SD solution file """ # parse the symbol map symbol_map = {} with open(symbols_filename) as f: for line in f: lp_symbol, scenario_tree_id = line.strip().split() symbol_map[lp_symbol] = scenario_tree_id results = SPSolverResults() results.status = None results.solver.status = None results.solver.termination_condition = None results.solver.message = None xhat = {} try: with open(solution_filename, 'r') as f: line = f.readline() assert line.startswith("Problem:") assert line.split()[1].strip() == "pysp_model" line = f.readline() assert line.startswith("First Stage Rows:") line = f.readline() assert line.startswith("First Stage Columns:") line = f.readline() assert line.startswith("First Stage Non-zeros:") line = f.readline() assert line.startswith("Replication No.") or \ line.startswith("Number of replications:") line = f.readline() assert line.startswith("Status:") results.solver.message = line.split(":")[1].strip() (results.status, results.solver.status, results.solver.termination_condition) = \ _sd_status_map.get(results.solver.message, (SolutionStatus.unknown, SolverStatus.unknown, TerminationCondition.unknown)) # # Objective and Bound # line = f.readline() assert line.startswith("Total Objective Function Upper Bound:") line = line.split(':') if line[1].strip() == '': pass else: assert len(line) == 4 line = line[1] if "half-width" in line: # we are given confidence intervals on the objective line = line.split(',') assert len(line) == 4 results.objective = float(line[0]) assert line[1].startswith('[') assert line[2].endswith(']') results.objective_interval = (float(line[1][1:]), float(line[2][:-1])) else: results.objective = float(line[1]) line = f.readline() assert line.startswith("Total Objective Function Lower Bound:") line = line.split(':') if line[1].strip() == '': pass else: if "half-width" in line[1]: # we are given confidence intervals on the bound line = line[1].split(',') assert len(line) == 4 results.bound = float(line[0]) assert line[1].startswith('[') assert line[2].endswith(']') results.bound_interval = (float(line[1][1:]), float(line[2][:-1])) else: results.bound = float(line[1].strip()) # # Xhat # line = f.readline() assert line.strip() == '' line = f.readline() assert line.startswith('First Stage Solutions:') line = f.readline() assert line.startswith( ' No. Row name Activity Lower bound Upper bound Dual Dual STDEV' ) line = f.readline() assert line.startswith( '------ ------------ ------------- ------------- ------------- ------------- -------------' ) xhat_start_line = ' No. Column name Activity Lower bound Upper bound Reduced Cost RC STDEV' line = f.readline() while not line.startswith(xhat_start_line): line = f.readline() line = f.readline() assert line.startswith( '------ ------------ ------------- ------------- ------------- ------------- -------------' ) line = f.readline().strip().split() while line: varlabel, varvalue = line[1:3] varlabel = varlabel.strip() varvalue = float(varvalue) xhat[symbol_map[varlabel]] = varvalue line = f.readline().strip().split() except (IOError, OSError): logger.warn("Exception encountered while parsing sd " "solution file '%s':\n%s'" % (solution_filename, traceback.format_exc())) xhat = None return xhat, results
def _read_solution(self, filename): """ Parses an SD solution file """ results = SPSolverResults() xhat = {} with open(filename, 'r') as f: line = f.readline() assert line.startswith("Problem:") assert line.split()[1].strip() == "pysp_model" line = f.readline() assert line.startswith("First Stage Rows:") line = f.readline() assert line.startswith("First Stage Columns:") line = f.readline() assert line.startswith("First Stage Non-zeros:") line = f.readline() assert line.startswith("Replication No.") or \ line.startswith("Number of replications:") line = f.readline() assert line.startswith("Status:") results.solver_status = line.split(":")[1].strip() # # Objective and Bound # line = f.readline() assert line.startswith("Total Objective Function Upper Bound:") line = line.split(':') if line[1].strip() == '': pass else: assert len(line) == 4 line = line[1] if "half-width" in line: # we are given confidence intervals on the objective line = line.split(',') assert len(line) == 4 results.objective = float(line[0]) assert line[1].startswith('[') assert line[2].endswith(']') results.objective_interval = (float(line[1][1:]), float(line[2][:-1])) else: results.objective = float(line[1]) line = f.readline() assert line.startswith("Total Objective Function Lower Bound:") line = line.split(':') if line[1].strip() == '': pass else: assert len(line) == 4 line = line[1] if "half-width" in line: # we are given confidence intervals on the bound line = line.split(',') assert len(line) == 4 results.bound = float(line[0]) assert line[1].startswith('[') assert line[2].endswith(']') results.bound_interval = (float(line[1][1:]), float(line[2][:-1])) else: results.bound = float(line[1]) # # Xhat # line = f.readline() assert line.strip() == '' line = f.readline() assert line.startswith('First Stage Solutions:') line = f.readline() assert line.startswith(' No. Row name Activity Lower bound Upper bound Dual Dual STDEV') line = f.readline() assert line.startswith('------ ------------ ------------- ------------- ------------- ------------- -------------') xhat_start_line = ' No. Column name Activity Lower bound Upper bound Reduced Cost RC STDEV' line = f.readline() while not line.startswith(xhat_start_line): line = f.readline() line = f.readline() assert line.startswith('------ ------------ ------------- ------------- ------------- ------------- -------------') line = f.readline().strip().split() while line: varlabel, varvalue = line[1:3] varlabel = varlabel.strip() varvalue = float(varvalue) xhat[varlabel] = varvalue line = f.readline().strip().split() return xhat, results
def _read_solution(self, filename): """ Parses an SD solution file """ results = SPSolverResults() xhat = {} with open(filename, 'r') as f: line = f.readline() assert line.startswith("Problem:") assert line.split()[1].strip() == "pysp_model" line = f.readline() assert line.startswith("First Stage Rows:") line = f.readline() assert line.startswith("First Stage Columns:") line = f.readline() assert line.startswith("First Stage Non-zeros:") line = f.readline() assert line.startswith("Replication No.") or \ line.startswith("Number of replications:") line = f.readline() assert line.startswith("Status:") results.solver_status = line.split(":")[1].strip() # # Objective and Bound # line = f.readline() assert line.startswith("Total Objective Function Upper Bound:") line = line.split(':') if line[1].strip() == '': pass else: assert len(line) == 4 line = line[1] if "half-width" in line: # we are given confidence intervals on the objective line = line.split(',') assert len(line) == 4 results.objective = float(line[0]) assert line[1].startswith('[') assert line[2].endswith(']') results.objective_interval = (float(line[1][1:]), float(line[2][:-1])) else: results.objective = float(line[1]) line = f.readline() assert line.startswith("Total Objective Function Lower Bound:") line = line.split(':') if line[1].strip() == '': pass else: assert len(line) == 4 line = line[1] if "half-width" in line: # we are given confidence intervals on the bound line = line.split(',') assert len(line) == 4 results.bound = float(line[0]) assert line[1].startswith('[') assert line[2].endswith(']') results.bound_interval = (float(line[1][1:]), float(line[2][:-1])) else: results.bound = float(line[1]) # # Xhat # line = f.readline() assert line.strip() == '' line = f.readline() assert line.startswith('First Stage Solutions:') line = f.readline() assert line.startswith( ' No. Row name Activity Lower bound Upper bound Dual Dual STDEV' ) line = f.readline() assert line.startswith( '------ ------------ ------------- ------------- ------------- ------------- -------------' ) xhat_start_line = ' No. Column name Activity Lower bound Upper bound Reduced Cost RC STDEV' line = f.readline() while not line.startswith(xhat_start_line): line = f.readline() line = f.readline() assert line.startswith( '------ ------------ ------------- ------------- ------------- ------------- -------------' ) line = f.readline().strip().split() while line: varlabel, varvalue = line[1:3] varlabel = varlabel.strip() varvalue = float(varvalue) xhat[varlabel] = varvalue line = f.readline().strip().split() return xhat, results