def launch_scenario_performance(scenario_id, n_parallel_call=1): """Launches the scenario. :param str scenario_id: scenario index. :param int n_parallel_call: number of parallel runs. This function calls :func:scenario_julia_call. """ scenario_info = get_scenario(scenario_id) min_ts = pd.Timestamp('2016-01-01 00:00:00') max_ts = pd.Timestamp('2016-12-31 23:00:00') dates = pd.date_range(start=min_ts, end=max_ts, freq='1H') start_ts = pd.Timestamp(scenario_info['start_date']) end_ts = pd.Timestamp(scenario_info['end_date']) # Julia starts at 1 start_index = dates.get_loc(start_ts) + 1 end_index = dates.get_loc(end_ts) + 1 # Create save data folder if does not exist output_dir = os.path.join(const.EXECUTE_DIR, 'scenario_%s/output' % scenario_info['id']) if not os.path.exists(output_dir): os.mkdir(output_dir) # Update status in ExecuteList.csv on server insert_in_file(const.EXECUTE_LIST, scenario_info['id'], '2', 'running') # Split the index into n_parallel_call parts parallel_call_list = np.array_split(range(start_index, end_index + 1), n_parallel_call) proc = [] start = time() for i in parallel_call_list: p = Process(target=scenario_julia_call, args=( scenario_info, int(i[0]), int(i[-1]), )) p.start() proc.append(p) for p in proc: p.join() end = time() # Update status in ExecuteList.csv on server insert_in_file(const.EXECUTE_LIST, scenario_info['id'], '2', 'finished') runtime = round(end - start) print('Run time: %s' % str(runtime)) hours, minutes, seconds = sec2hms(runtime) insert_in_file(const.SCENARIO_LIST, scenario_info['id'], '15', '%d:%02d' % (hours, minutes))
def _record_scenario(scenario_id, runtime): """Updates execute and scenario list on server after simulation. :param str scenario_id: scenario index. :param int runtime: runtime of simulation in seconds. """ # Update status in ExecuteList.csv on server insert_in_file(const.EXECUTE_LIST, scenario_id, "status", "finished") hours, minutes, seconds = sec2hms(runtime) insert_in_file(const.SCENARIO_LIST, scenario_id, "runtime", "%d:%02d" % (hours, minutes))
def launch_scenario(self, execute_dir=None, threads=None, solver_kwargs=None): """Launches the scenario. :param None/str execute_dir: directory for execute data. None defaults to an execute folder that will be created in the input directory :param None/int threads: number of threads to use. :param None/dict solver_kwargs: keyword arguments to pass to solver (if any). :return: (*int*) runtime of scenario in seconds """ self.execute_dir = execute_dir self.threads = threads self._print_settings() # Import these within function because there is a lengthy compilation step from julia.api import Julia Julia(compiled_modules=False) from julia import Gurobi # noqa: F401 from julia import REISE start = time() REISE.run_scenario_gurobi( interval=self.interval, n_interval=self.n_interval, start_index=self.start_index, inputfolder=self.input_dir, outputfolder=self.execute_dir, threads=self.threads, ) end = time() runtime = round(end - start) hours, minutes, seconds = sec2hms(runtime) print(f"Run time: {hours}:{minutes:02d}:{seconds:02d}") return runtime
def test_sec2hms_hms(): seconds = 72 * 3600 + 45 * 60 + 15 assert sec2hms(seconds) == (72, 45, 15)
def test_sec2hms_hours_only(): seconds = 7200 assert sec2hms(seconds) == (2, 0, 0)
def test_sec2hms_minutes_only(): seconds = 120 assert sec2hms(seconds) == (0, 2, 0)
def test_sec2hms_seconds_only(): seconds = 33 assert sec2hms(seconds) == (0, 0, seconds)
def test_sec2hms_returned_value_type(): seconds = 33 assert isinstance(sec2hms(seconds), tuple) assert len(sec2hms(seconds)) == 3
def test_sec2hms_arg_type(): seconds = 3.1 with pytest.raises(TypeError): sec2hms(seconds)
def parse_runtime(self, start, end): runtime = round(end - start) hours, minutes, seconds = sec2hms(runtime) print(f"Run time: {hours}:{minutes:02d}:{seconds:02d}") return runtime