No such file or directory error with FMU block
Show older comments
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
More Answers (0)
Categories
Find more on Verification, Validation, and Test in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!