#!/usr/bin/env python # coding: utf-8 # # Surrogating a function with a machine learning estimator # # System dynamics generally represents the relationships between model elements as either analytical equations, or lookup tables. However, in some situations we may be presented with relationships that are not well estimated by equations, but involve more than a single input leading to a single output. When confrontied with this situation, other paradigms # # Simple defects model .. image:: ../../../source/models/Manufacturing_Defects/Defects.png :width: 400 px # In[1]: get_ipython().run_line_magic('pylab', 'inline') import pysd import numpy as np import pandas as pd # In[2]: model = pysd.read_vensim('../../models/Manufacturing_Defects/Defects.mdl') # In[3]: data = pd.read_csv('../../data/Defects_Synthetic/Manufacturing_Defects_Synthetic_Data.csv') data.head() # In[4]: plt.scatter(data['Workday'], data['Time per Task'], c=data['Defect Rate'], linewidth=0, alpha=.6) plt.ylabel('Time per Task') plt.xlabel('Length of Workday') plt.xlim(0.15, .45) plt.ylim(.01, .09) plt.box('off') plt.colorbar() plt.title('Defect Rate Measurements') plt.figtext(.88, .5, 'Defect Rate', rotation=90, verticalalignment='center'); # In[5]: from sklearn.svm import SVR Factors = data[['Workday','Time per Task']].values Outcome = data['Defect Rate'].values regression = SVR() regression.fit(Factors, Outcome) # In[6]: def new_defect_function(): """ Replaces the original defects equation with a regression model""" workday = model.components.length_of_workday() time_per_task = model.components.time_allocated_per_unit() return regression.predict([[workday, time_per_task]])[0] model.components.defect_rate = new_defect_function # In[7]: model.components.defect_rate() # In[8]: model.run().plot();