Beispiel #1
0
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
Beispiel #2
0
def getObjectives(var_set) -> fun_lib.ObjectiveVar:
    tic = time.time()
    cf = fun_lib.importCostFunction(dir=var_set['__COST_FUNC_PATH'])

    if var_set['__DRAW_PLOTS_OVERRIDE']:
        var_set['__draw_plots'] = True
        var_set['__plot_title'] = "Run %i" % (fun_lib.getRunNumber())
        var_set['__saveFig_path'] = "%sFitFig_%03d.png" % (
            fun_lib.getSafeLogDir(
                var_set['__VALUE_LOG_DIRNAME']), fun_lib.getRunNumber())

    # log combined output if supported by the cost function
    var_set['__objectivesLog_path'] = "%sobjectiveLog_%03d.csv" % (
        fun_lib.getSafeLogDir(
            var_set['__VALUE_LOG_DIRNAME']), fun_lib.getRunNumber())

    objectives = cf.getObjectives(var_set)

    print("Calculating costs in ", time.time() - tic, " s")
    return objectives
def logOutput(objectives):
    # log the output, if the log directory exists. exit otherwise
    writeLogHeader(objectives)

    filepath = getLogFilePath()
    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)
        
        wc = fun_lib.countTotalWeightedCost(objectives)
        string_seq = map(lambda o: logLine(o, wc), objectives)

        file.write(',  '.join(string_seq) + tail + '\n')
Beispiel #4
0
def logCrash(logdirname, line: str):
    with open(fun_lib.getSafeLogDir(logdirname) + 'errorLog.txt', 'a') as f:
        s = '%s: At run %d sumfin went wong: %s\n' % (
            datetime.now(), fun_lib.getRunNumber(), line)
        print(s)
        f.write(s)