def runSimulation(): # extract the FMU to a temporary directory unzipdir = fmpy.extract(fmu) # read the model description model_description = fmpy.read_model_description(unzipdir) # instantiate the FMU fmu_instance = fmpy.instantiate_fmu(unzipdir, model_description, 'CoSimulation') cf = fun_lib.importCostFunction(dir = 'Combined\\') params = readInitParams() cost = 1e300 # shuffledParams = copy. for iter in range(niter): # todo paralelize this cur_params = shuffleParams(params) # reset the FMU instance instead of creating a new one fmu_instance.reset() result = fmpy.simulate_fmu(unzipdir, stop_time=1, start_values=cur_params, model_description=model_description, fmu_instance=fmu_instance, fmi_call_logger=lambda s: print('[FMI] ' + s) ) var_set = {} for name in result.dtype.names: var_set[name] = result[name] if DRAW_PLOTS: var_set['__draw_plots'] = True var_set['__plot_title'] = "Run %i" % (fun_lib.getRunNumber()) var_set['__saveFig_path'] = "%sFitFig_%03d.png" % (fun_lib.getSafeLogDir('Schedules'), fun_lib.getRunNumber()) objectives = cf.getObjectives(var_set, targetsFolder = r"../data/Valsalva/") cur_cost = fun_lib.countTotalSumCost(objectives) if cur_cost < cost: # woohoo, better costs! cost = cur_cost params = cur_params writeSchedule(params, cost, iter) print(result) # free the FMU instance and unload the shared library fmu_instance.freeInstance() # delete the temporary directory shutil.rmtree(unzipdir, ignore_errors=True) print('that is all, folks') pass
def writeCost(objectives): total_cost = fun_lib.countTotalSumCost(objectives) # total_cost = sum(costs) with open('dsout.out', 'w') as file: file.write("banik pico\n") file.write('f(x) =' + repr(total_cost)) print('Total costs: %s' % (total_cost))
def logOutput(objectives, var_set): # log the output, if the log directory exists. exit otherwise filepath = getLogFilePath(var_set) run = fun_lib.getRunNumber() with open(filepath, 'a') as file: # prepare the line with value, cost value for this and percentage of total costs total_cost = fun_lib.countTotalSumCost(objectives) t = datetime.now().strftime("%Y-%m-%d %H:%M:%S") tail = " ,%03d,%s, %.6e" % (run, t, total_cost) tc = fun_lib.countTotalSumCost(objectives) if tc == 0: # dvision by zero fix tc = 1 string_seq = map(lambda o: logLine(o, tc), objectives) file.write(', '.join(string_seq) + tail + '\n')
def runOptimization(): if os.path.exists(log_file): # clear logfile os.remove(log_file) fmu_init = initFmus() (params, cost) = readInitParams() writeLog('Commencing first run..', -1) for iter in range(niter): # todo paralelize this if cost is not None: cur_params = shuffleParams(params) else: cur_params = params try: objectives = runSimulations(fmu_init, cur_params, iter) cur_cost = fun_lib.countTotalSumCost(objectives) if cost is None or cur_cost < cost: # woohoo, better costs! cost = cur_cost params = cur_params writeLog('Found better cost of %.6f' % cost, iter) writeSchedule(params, cost, iter) else: writeLog('Rejecting cost %.6f' % cur_cost, iter) except fun_lib.SimulationResultIncompleteError as e: writeLog('Sim failed', iter) writeSchedule(cur_params, 1e3, iter) # clean up for (fmu_instance, _, _, _) in fmu_init: # free the FMU instance and unload the shared library fmu_instance.freeInstance() # delete the temporary directory shutil.rmtree(fmu_instance.unzipDirectory, ignore_errors=True) print('that is all, folks') pass
def getObjectives(vars_set: dict): """ Gets all objectives for our combined CF. Trials: valsalva sit # valsalva supine tilt 60 HR brachial pressures LV volumes (SV and EF) max exercise""" plt.close('all') dpi = 100 fig, axs = plt.subplots(nrows=2, ncols=2, sharex=False, sharey=False, figsize=[1920 / dpi, 1080 / dpi], dpi=dpi) if '__root_path' not in vars_set: vars_set['__root_path'] = '..\\..\\..\\' if '__plot_title' in vars_set: fig_title = vars_set['__plot_title'] # get rid of the key del vars_set['__plot_title'] else: fig_title = 'Debug run' if '__saveFig_path' in vars_set and vars_set['__saveFig_path'] is not None: saveFig_path = vars_set['__saveFig_path'] # prevent child cost functions to save figures del vars_set['__saveFig_path'] else: saveFig_path = None if '__objectivesLog_path' in vars_set: objectivesLog_path = vars_set['__objectivesLog_path'] else: objectivesLog_path = None def flat2gen(alist): # https://stackoverflow.com/questions/3172930/flattening-mixed-lists-in-python-containing-iterables-and-noniterables for item in alist: if isinstance(item, Iterable): for subitem in item: yield subitem else: yield item ax = list(flat2gen(axs)) axes_num = 0 # returns one objective per use case - i.e. per simulation or per cost function import cost_objectives = [] # returns all objectives all_objectives = [] def buildCostObjective(name, cost_func_folder, weight): """ the name is prefix in the large model as well """ nonlocal axes_num nonlocal ax # check if there are results for this particular use case if len([v for v in vars_set.keys() if v.startswith(name + '.')]) == 0: o = ObjectiveVar('IGNORING %s' % name, value=0, targetValue=0, costFunctionType=CostFunctionType.Ignore) cost_objectives.append(o) all_objectives.append(o) return vars_set['__plot_axes'] = ax[axes_num] axes_num = axes_num + 1 cf = importCostFunction(vars_set['__root_path'] + 'Identification\\' + cost_func_folder) # mapped_vars = mapVarSet(vars_set, mapping) # filtering only vars relevant for that cost function, e.g. valsalva.brachial_pressure gives brachial_pressure mapped_vars = filterVarSet(vars_set, name + '.') objectives = cf.getObjectives(mapped_vars) # normalize to variance type cost function for o in objectives: fun_lib.unifyCostFunc(o) cost = fun_lib.countTotalWeightedCost(objectives) costObjective = ObjectiveVar( name, value=cost, costFunctionType=CostFunctionType.DistanceFromZero, weight=weight) # add it to the set cost_objectives.append(costObjective) # prepare to compare to all objectives in a log def sumObjectives(o: ObjectiveVar): o.name = name + '.' + o.name o.weight = weight * o.weight return o all_objectives.extend(map(sumObjectives, objectives)) # they append inside buildCostObjective('baseline', 'optimizeBaselineTriSegLumens', 1) buildCostObjective('tilt', 'optimizeTilt', 1) buildCostObjective('exercise', 'MaxExercise', 1) buildCostObjective('valsalva', 'valsalva', 1) # def plotObjectives(): # fignums = plt.get_fignums() # axes = [] # for f in fignums: # if len(plt.figure(f).axes) > 0: # axes.append(plt.figure(f).axes) # # # unpack axes # # axes = list(f.axes for f in figs if f is not None and f.axes is not None and len(f.axes) > 0) # plt.close('all') # plt.figure() # _, ax = plt.subplots(nrows = len(axes), sharex = True) # ax.append(axes) # ax. # plotObjectives() fun_lib.logObjectives( vars_set['__LOG_ALL_OBJECTIVES_PATH'], all_objectives, vars_set['__SORT_COSTS_BY'], compare_to_log_path=vars_set['__LOG_ALL_OBJECTIVES_PATH_BASE']) fig.suptitle('%s costs %.6f' % (fig_title, fun_lib.countTotalSumCost(cost_objectives))) if saveFig_path is not None: try: plt.savefig(saveFig_path) except PermissionError: print('Writing the figure failed, permission denied') if '__showPlots' in vars_set and vars_set['__showPlots']: # and fun_lib.getRunNumber() == 0: # zero means debug plt.show() # return cost_objectives return all_objectives