No such file or directory error with FMU block

7 views (last 30 days)
I created FMU file with the below code by Python then loaded the file with FMU block but I got
Error with specified FMU: Cannot load dynamic library
'/MATLAB Drive/slprj/_fmu/b37a07ca0ba765ad86fa700f844987e2/MachineLearningFMU/binaries/x86_64-linux/ Cannot load MachineLearningFMU.so'
: libpython3.12.so.1.0: cannot open shared object file: No such file or directory
error when I did simulation.
Why do I get this error?
The python code is to load regression_model.pkl file that is a simple machine learning model to predict 2 outputs based on 7 inputs and them save it as .fmu file.
7 inputs (Features):
'accel'
'brake'
'hvac'
'dcdc'
'motor'
'soc'
'vl'
2 outputs (Objective Variables):
'torque'
're_brake'
And I execute this command and create FMU file.
pythonfmu3 build -f mlmodelfmu.py
I wonder if I'm missing something in my python code.
I'd appreciate it you could give me your advice.
Here is my python script (mlmodelfmu.py) to create FMU file.
from pythonfmu3 import Fmi3Causality, Fmi3Slave, Float64
import joblib
import numpy as np
class MachineLearningFMU(Fmi3Slave):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.author = "AI Assistant"
self.description = "FMU for regression prediction using pkl model"
self.model = joblib.load("regression_model.pkl")
self.inputs = [0.0] * 7
self.outputs = [0.0] * 2
input_names = ["accel", "brake", "hvac", "dcdc", "motor", "soc", "vl"]
for i, name in enumerate(input_names):
self.register_variable(
Float64(
name,
causality=Fmi3Causality.input,
getter=lambda i=i: self.inputs[i],
setter=lambda v, i=i: self.set_input(i, v),
)
)
output_names = ["torque", "re_brake"]
for i, name in enumerate(output_names):
self.register_variable(
Float64(
name,
causality=Fmi3Causality.output,
getter=lambda i=i: self.outputs[i],
)
)
def set_input(self, index, value):
self.inputs[index] = value
def predict_with_model(self):
input_array = np.array([self.inputs])
prediction = self.model.predict(input_array)
for i in range(len(self.outputs)):
self.outputs[i] = prediction[0][i]
def do_step(self, current_time, step_size):
self.predict_with_model()
return True
Also,this is how I created .pkl file. in the environment of Python 3.10 on local.
n_samples = 1000
X = np.random.rand(n_samples, 7)
y = np.random.rand(n_samples, 2)
input_names = ['accel', 'brake', 'hvac', 'dcdc',
'motor', 'soc', 'vl']
output_names = ['torque', 're_brake']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
rf = RandomForestRegressor(n_estimators=100, random_state=42)
model = MultiOutputRegressor(rf)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
joblib.dump(model, 'regression_model.pkl')

Accepted Answer

Venkat Siddarth Reddy
Venkat Siddarth Reddy on 12 Oct 2024
Edited: Venkat Siddarth Reddy on 12 Oct 2024
Hi 翼,
This issue seems to be due to the requirement of Python version "3.12" to execute the FMU. If this version of Python is already installed, I highly recommend reinstalling it and setting up the configuration again.
Additionally,once the Python setup is completed, please generate a new PKL file in "Python 3.12" version.
However if the issue persists even after the above steps, please check the compliance of the FMU with the FMI standard using the "FMU compliance checker". This should reveal more information about the issue.
For more information on "FMU compliance checker", please refer to the following repository:
Additionally, please refer to the following MATLAB Answers Community post on how to troubleshoot "FMU Import Issues" in Simulink:
I hope it helps!
  2 Comments
翼
on 13 Oct 2024
Thank you for telling me.
I generated a new PKL file in "Python 3.12" and executed my python script (mlmodelfmu.py) to create FMU file again. However it didn't work. The same error happend.
("Python 3.12" here means a virtual environment by "pipenv")
I enabled FMU Debug Logging and checked the log file (.txt) but only this is written...
# Debug Logging enabled for block 'fmu_test/FMU'
I wonder if I'm now using MATLAB Online....
Also, I installed FMpy and checked my fmu with the below code.
from fmpy import *
fmu = 'MachineLearningFMU.fmu'
dump(fmu)
The result says,
Model Info
FMI Version 3.0
FMI Type Co-Simulation
Model Name MachineLearningFMU
Description FMU for regression prediction using pkl model
Platforms linux64, win64
Continuous States 0
Event Indicators 0
Variables 9
Generation Tool PythonFMU3 0.1.15
Generation Date 2024-10-13T08:57:39+00:00
Variables (input, output)
Name Causality Start Value Unit Description
accel input 0
brake input 0
hvac input 0
dcdc input 0
motor input 0
soc input 0
vl input 0
torque output
re_brake output
thenI tried its simulation with this code, but I got the error.
result = simulate_fmu('MachineLearningFMU.fmu')
Here is the error.
Failed to validate modelDescription.xml:
Exactly one independent variable must be defined.
I could not figure out the cause...
Do you have any good ideas?
Best,
翼
on 21 Oct 2024
Edited: on 21 Oct 2024
I solved it! Sorry to bother you..
Thank you.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!