Package: alpypeopt
ALPypeOptALPypeOptorAnyLogic Python Pipe for Optimizationis an open source library for connectingAnyLogicsimulation models with python-basedsequential optimizationpackages such asscikit-optimize,optuna,hyperoptandbayesian optmization.WithALPypeOptyou will be able to:Connect your AnyLogic model to asequential optimizationpackage of your choice (e.g. scikit-optimizeskopt).(IN PROGRESS)Scale your optimization loop by launching many AnyLogic models simultaneously (requires an exported model).Debug your AnyLogic models during optimization loop (this special feature improves the user experience during model debugging remarkably).Leverage on the AnyLogic rich visualization as the optimization runs (which ties to the previous bullet point).There is a more comprehensivedocumentationavailable that includes numerous examples to help you understand the basic functionalities in greater detail.NOTE: ALPypeOpt has been engineered as a framework that is independent of any specific sequential optimization package. This design facilitates its compatibility with a wide range of state-of-the-art optimization packages.EnvironmentsALPypeOptincludes 2 environments that make the connection betweenAnyLogicand yourpython scriptpossible:ALPypeOptConnector- The AnyLogic connector ('agent') library to be dropped into your simulation model.alpypeopt- The library that you will use after configuring youroptimization solverin your python script to connect to the AnyLogic model.InstallationTo install the baseALPypeOptlibrary in python, usepip install alpypeopt.To useALPypeOptConnectorinAnyLogic, you can add thelibraryto yourPalette. That will allow you to drag and drop the connector into your model.Note that furtherinstructionsare required to be followed in order for the connector to work.RequirementsTheALPypeOptrequires you to have theAnyLogic software(or a valid exported model). AnyLogic is a licensed software for building simulations that includes an ample variety of libraries for modelling many industry challenges. At the moment, AnyLogic provides afreelicense under the name PLE (Personal Learning Edition). There are other options available. For more information, you can visit theAnyLogic website.Note: This is not a package that is currently backed by the AnyLogic support team.The python packagealpypeoptdoesn't require any additional dependencies other that the sequential optimization package of your choice:E.g.:scikit-optimizeAPI basicsOptimization loopTo be able to solve anoptimization problem, you must have the following:AnAnyLogic modelthat requires certain parameters to be set at the beggining of the run and will impact the overall goal. Using theGas Processing Plantexample, a decision must be taken on the plant setup in order for thetotal revenueto be maximized. For that, the AnyLogic model will be consuming any setup passed to theALPypeOptConnectorand returning the newely calculatedrevenue.Apython scriptthat contains the optimization algorithm. Here is where the optimal solution will be computed. For that, you will need to create an instance (to handle the connection) of the AnyLogic model and encapsulate it under a function that can be consumed by the optimization package that you have chosen to use:fromalpypeoptimportAnyLogicModelfromskoptimportgp_minimizegpp_model=AnyLogicModel(env_config={'run_exported_model':False,'exported_model_loc':'./resources/exported_models/gas_processing_plant','show_terminals':False,'verbose':False})# Initialize model setupgpp_setup=gpp_model.get_jvm().gasprocessingplant.GPPSetup()# Start setting up gas product pricesgpp_setup.setProductPrices(30.0,10.0)# Create input variable ratios as (min, max)# GPU 1&2 plant load ratiosplant_load_ratio=(0.01,0.99)# GPU 1&2 distillation column operating temperaturegpp_opp_temp_ratio=(20.0,100.0)bounds=[plant_load_ratio,# dec1 flow allocationgpp_opp_temp_ratio,# dec1 temperaturegpp_opp_temp_ratio# dec2 temperature]# Encapsulate simulation model as a python functiondefsimulation(x,reset=True):# Setup selected plant loads and temperaturesgpp_setup.setFlowAllocRateToDec1(x[0])gpp_setup.setDecTemperatures(x[1],x[2])# Pass input setup and run model until endgpp_model.setup_and_run(gpp_setup)# Extract model output or simulation resultmodel_output=gpp_model.get_model_output()ifreset:# Reset simulation model to be ready for next iterationgpp_model.reset()# Return simulation value. 'skopt' package only allows minimization problems# Because of that, value must be negatedreturn-model_output.getTotalRevenue()# Setup and execute sequential optimmization modelres=gp_minimize(simulation,# the function to minimizebounds,# the bounds on each dimension of xacq_func="EI",# the acquisition functionn_calls=10,# the number of evaluations of simulationn_random_starts=5,# the number of random initialization pointsrandom_state=1234)# the random seed# Print optimal solutionprint(f"Solution is{res.x}for a value of{-res.fun}")# Run simulation with optimal result to use UI to explore results in AnyLogicsimulation(res.x,reset=False)# Close modelgpp_model.close()Bugs and/or development roadmapAt the moment, ALPypeOpt is at its earliest stage. You can join thealpypeopt projectand raise bugs, feature requests or submit code enhancements via pull request.Support ALPypeOpt's developmentIf you are financially able to do so and would like to support the development ofALPypeOpt, please reach out tomarcescandellmari@gmail.com.LicenseThe ALPypeOpt software suite is licensed under the terms of the Apache License 2.0. SeeLICENSEfor more information.