1010
1111import sys , os , shlex , re , inspect , textwrap , types , pickle , traceback , gc
1212import warnings
13+ import datetime
14+ import platform
1315
1416from pyomo .solvers .plugins .solvers .direct_or_persistent_solver import DirectOrPersistentSolver
1517
1618import switch_model
1719from switch_model .utilities import (
18- create_model , _ArgumentParser , StepTimer , make_iterable , LogOutput , warn , query_yes_no , create_info_file ,
19- get_module_list , add_module_args , _ScaledVariable
20+ create_model , _ArgumentParser , StepTimer , make_iterable , LogOutput , warn , query_yes_no ,
21+ get_module_list , add_module_args , _ScaledVariable , add_git_info
2022)
2123from switch_model .upgrade import do_inputs_need_upgrade , upgrade_inputs
2224from switch_model .tools .graphing import graph
25+ from switch_model .utilities .results_info import save_info , add_info , ResultsInfoSection
2326
2427
2528def main (args = None , return_model = False , return_instance = False , attach_data_portal = False ):
@@ -103,6 +106,9 @@ def debug(type, value, tb):
103106 if not os .path .isdir (model .options .outputs_dir ):
104107 raise IOError ("Directory specified for prior solution does not exist." )
105108
109+ add_info ("Host name" , platform .node (), section = ResultsInfoSection .GENERAL )
110+ add_git_info ()
111+
106112 # get a list of modules to iterate through
107113 iterate_modules = get_iteration_list (model )
108114
@@ -111,7 +117,7 @@ def debug(type, value, tb):
111117 print ("Switch {}, http://switch-model.org" .format (switch_model .__version__ ))
112118 print ("=======================================================================" )
113119 print ("Arguments:" )
114- print (", " .join (k + "=" + repr (v ) for k , v in model .options .__dict__ .items () if v ))
120+ print (", " .join (k + "=" + repr (v ) for k , v in model .options .__dict__ .items () if v ))
115121 print ("Modules:\n " + ", " .join (m for m in modules ))
116122 if iterate_modules :
117123 print ("Iteration modules:" , iterate_modules )
@@ -198,7 +204,15 @@ def debug(type, value, tb):
198204 graph .main (args = ["--overwrite" ])
199205
200206 total_time = start_to_end_timer .step_time_as_str ()
201- create_info_file (getattr (instance .options , "outputs_dir" , "outputs" ), run_time = total_time )
207+ add_info ("Total run time" , total_time , section = ResultsInfoSection .GENERAL )
208+
209+ add_info ("End date" , datetime .datetime .now ().strftime ('%Y-%m-%d' ), section = ResultsInfoSection .GENERAL )
210+ add_info ("End time" , datetime .datetime .now ().strftime ('%H:%M:%S' ), section = ResultsInfoSection .GENERAL )
211+
212+ save_info (
213+ os .path .join (getattr (instance .options , "outputs_dir" , "outputs" ),
214+ "info.txt" )
215+ )
202216
203217 if instance .options .verbose :
204218 print (f"Total time spent running SWITCH: { total_time } ." )
@@ -815,30 +829,6 @@ def solve(model):
815829 print ('solver_options_string and calling this script with "--suffixes iis" or "--gurobi-find-iis".' )
816830 raise RuntimeError ("Infeasible model" )
817831
818- # Raise an error if the solver failed to produce a solution
819- # Note that checking for results.solver.status in {SolverStatus.ok,
820- # SolverStatus.warning} is not enough because with a warning there will
821- # sometimes be a solution and sometimes not.
822- # Note: the results object originally contains values for model components
823- # in results.solution.variable, etc., but pyomo.solvers.solve erases it via
824- # result.solution.clear() after calling model.solutions.load_from() with it.
825- # load_from() loads values into the model.solutions._entry, so we check there.
826- # (See pyomo.PyomoModel.ModelSolutions.add_solution() for the code that
827- # actually creates _entry).
828- # Another option might be to check that model.solutions[-1].status (previously
829- # result.solution.status, but also cleared) is in
830- # pyomo.opt.SolutionStatus.['optimal', 'bestSoFar', 'feasible', 'globallyOptimal', 'locallyOptimal'],
831- # but this seems pretty foolproof (if undocumented).
832- if len (model .solutions [- 1 ]._entry ['variable' ]) == 0 :
833- # no solution returned
834- print ("Solver terminated without a solution." )
835- print (" Solver Status: " , results .solver .status )
836- print (" Solution Status: " , model .solutions [- 1 ].status )
837- print (" Termination Condition: " , results .solver .termination_condition )
838- if model .options .solver == 'glpk' and results .solver .termination_condition == TerminationCondition .other :
839- print ("Hint: glpk has been known to classify infeasible problems as 'other'." )
840- raise RuntimeError ("Solver failed to find an optimal solution." )
841-
842832 # Report any warnings; these are written to stderr so users can find them in
843833 # error logs (e.g. on HPC systems). These can occur, e.g., if solver reaches
844834 # time limit or iteration limit but still returns a valid solution
0 commit comments