question

João Victor SDA avatar image
0 Likes"
João Victor SDA asked anthony.johnson commented

Run FlexSim in Parallel from Python

Hi,


I am controlling FlexSim from a python script (via FlexSimPy) for optimization, which works well when running it in serial. I.e.., I can change the parameters, random seed (via parameters table), run, and get the performance measure.

However, I need to run FlexSim in parallel (i.e., run multiple experiments simultaneously - as the Experimenter does). How can I do this?


Thank you very much.

Following is my python scrip to optimize a FlexSim Model (in at serial way). I already use traditional parallelization libraries to parallelize the FlexSim execution, but it falls.


  1. ###Import libraries###
  2. import sys
  3. import pandas as pd
  4. import openpyxl
  5. import os
  6. import numpy as np
  7. from EGO_NEW_rev01 import EGO_New
  8. from AMSO_Rev01 import AMSO
  9. from scipy.stats import norm
  10. from scipy.optimize import minimize
  11. from math import exp
  12. import matplotlib.pyplot as plt
  13. from matplotlib import colors
  14. from smt.sampling_methods import LHS
  15. from smt.applications import EGO
  16. from smt.applications.mixed_integer import MixedIntegerSurrogateModel
  17. import warnings
  18. warnings.filterwarnings("ignore")
  19. from smt.applications.mixed_integer import (
  20. FLOAT,
  21. ORD,
  22. INT,
  23. ENUM,
  24. MixedIntegerSamplingMethod,
  25. cast_to_mixed_integer, unfold_with_enum_mask
  26. )
  27. #Import FlexSimPy Module
  28. sys.path.insert(0, path1)
  29. import FlexSimPy as fp
  30.  
  31. #Import parameters from Excel
  32. solution_space = pd.read_excel("Dashboard.xlsx",sheet_name="Painel",usecols="B:E",keep_default_na=False,header=11)
  33. general_parameters = pd.read_excel("Dashboard.xlsx",sheet_name="Painel",usecols="G:H",keep_default_na=False,header=11)
  34. files_path = pd.read_excel("Dashboard.xlsx",sheet_name="Painel",usecols="j:k",keep_default_na=False,header=11)
  35. general_parameters=general_parameters.set_index('Parameters')
  36. files_path=files_path.set_index('Description')
  37.  
  38. #cleaning data
  39. remove = solution_space.loc[(solution_space['Variables']=='')]
  40. solution_space = solution_space.drop(remove.index)
  41. #General Parameters
  42. path1 = files_path.loc['FlexSimPy']['Path']
  43. path2 = files_path.loc['FlexSim.exe']['Path']
  44. replications = general_parameters.loc['Replications']['Value']
  45. Response_variable=general_parameters.loc['Response Variable']['Value']
  46. Stop_time= general_parameters.loc['Stop Time']['Value']
  47. criterion=general_parameters.loc['EGO Criteria']['Value'] #'EI' or 'SBO' or 'LCB'
  48. DOE_given = general_parameters.loc['DOE Given']['Value']
  49. model_name= general_parameters.loc['Model Name']['Value']
  50. ndoe = general_parameters.loc['Inicial Points']['Value']
  51. n_iter = general_parameters.loc['Iterations']['Value']
  52. Direction = general_parameters.loc['Direction']['Value']
  53. #Variables Information
  54. xlimits=solution_space[['Min','Max']].to_numpy()
  55. xtypes = solution_space['Type'].tolist()
  56. variaveis = solution_space['Variables'].tolist()
  57.  
  58. #The optimization direction
  59. if Direction == "Max":
  60. factor = -1
  61. else:
  62. factor = 1
  63.  
  64. print("start")
  65. #Lauch FlexSim
  66. scriptPath = os.path.dirname(os.path.realpath(__file__))
  67. programDir = path2
  68. controller = fp.launch(evaluationLicense=False,showGUI=False,programDir=programDir)
  69. controller.open(scriptPath+"\\"+model_name)
  70.  
  71. def fun(x):
  72. n,dim = np.atleast_2d(x).shape
  73. y=[]
  74. w=[]
  75. for i in range(n):
  76. xi = x[i,:]
  77. for r in range(replications):
  78. w.append(simulate(xi, r, controller, variaveis, Stop_time, Response_variable))
  79. y.append(np.mean(w))
  80. print(np.atleast_2d(y).T)
  81. return np.atleast_2d(y).T
  82.  
  83.  
  84.  
  85. def simulate(x,r,controller,variaveis,Stop_time,Response_variable):
  86.  
  87. seed=r+1
  88. print(x)
  89. #mudar seed
  90. instancia = controller
  91. instancia.setParameter("Replication",seed)
  92.  
  93. for j in range(dim):
  94. valor = int(x[j])
  95. nome = variaveis[j]
  96. instancia.setParameter(nome,valor)
  97. instancia.reset()
  98. instancia.runToTime(Stop_time)
  99. result = instancia.getPerformanceMeasure(Response_variable)
  100. print(result*factor)
  101. return result*factor
  102.  
  103. #number of points in the initial DOE
  104. dim=len(variaveis)
  105.  
  106. #Create a inicial DOE (required in optimization algorithm)
  107. path = scriptPath + "\\DOE.xlsx"
  108. if DOE_given == 'False':
  109. sampling = MixedIntegerSamplingMethod(xtypes, xlimits, LHS, criterion="ese")
  110. xdoe = sampling(ndoe)
  111. X = pd.DataFrame(xdoe)
  112. ydoe=fun(xdoe)
  113. Y = pd.DataFrame(ydoe)
  114. with pd.ExcelWriter(path,mode="a", if_sheet_exists="replace",engine="openpyxl") as writer:
  115. X.to_excel(writer, sheet_name="X",index=False)
  116. Y.to_excel(writer, sheet_name="Y",index=False)
  117.  
  118. else:
  119. X = pd.read_excel(path,sheet_name="X")
  120. Y= pd.read_excel(path,sheet_name="Y")
  121. xdoe= X.to_numpy()
  122. ydoe = Y.to_numpy()
  123.  
  124.  
  125. print("X: ",xdoe," Y: ",ydoe)
  126.  
  127. #Optimization call
  128. ego = AMSO(n_iter=n_iter, criterion=criterion, xdoe=xdoe,ydoe=ydoe, xlimits=xlimits,xtypes=xtypes)
  129.  
  130. x_opt, y_opt, ind_best, x_data, y_data, R_square, pred, std = ego.optimize(fun=fun)
  131. y_opt=y_opt*factor
  132.  
  133. print('Xopt for Model ', x_opt, y_opt, ' obtained using EGO criterion = ', criterion )
  134.  
  135. controller.close(scriptPath+"\\ABDI_1.fsm")
  136.  
  137. with pd.ExcelWriter(path, mode="a", if_sheet_exists="replace",engine="openpyxl") as writer:
  138. pd.DataFrame(x_data).to_excel(writer, sheet_name="x_data",index=False)
  139. pd.DataFrame(y_data).to_excel(writer, sheet_name="y_data",index=False)
  140. pd.DataFrame(R_square).to_excel(writer, sheet_name="R2",index=False)
  141. pd.DataFrame(pred).to_excel(writer, sheet_name="pred",index=False)
  142. pd.DataFrame(std).to_excel(writer, sheet_name="std",index=False)
  143. pd.DataFrame(x_opt).to_excel(writer, sheet_name="Result1",index=False,startcol=1,startrow=1)
  144. pd.DataFrame(y_opt).to_excel(writer, sheet_name="Result2",index=False,startcol=1,startrow=3)
  145.  
  146.  
FlexSim 22.2.1
flexsimpy
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Jordan Johnson avatar image
1 Like"
Jordan Johnson answered anthony.johnson commented

The FlexSimPy module only allows one instance of FlexSim at a time, and it executes commands synchronously. To run multiple instances of FlexSim at a time, you'd need to use a "controller" script of Python launch several processes of Python, each controlling an instance of FlexSim. The controller would then use some form of inter-process communication (sockets, pipes, etc.) to send tasks to the FlexSim instances and return results.

· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.