0

In a simulation i get some data looking like a arctan or tanh function.

I want to implement a function fit in Java for getting the parameter of this function for optimization. For other functions i used for example the Apache code for function fit of polynomial and gaus function but couldn't find a solution for tangent.

To be honest I don't know how to write such a function fit so maybe someone can help me fixing this problem or does know if there is already a function fit existing for such functions.

Handballer73
  • 103
  • 5
  • Your question is a bit unclear. If, rather than what Ben's assumed in his answer, you are talking about fitting a function to data more generally (e.g., to use as a 'hardcoded' function in your model --- pre-fitted to external data --- to generate something), that's something you do outside of simulation using tools like Stat::Fit. (I think some other sim tools, like Simul8, have Stat::Fit as a plug-in but AnyLogic doesn't.) – Stuart Rossiter Jan 27 '21 at 09:11
  • Finally, I am reading values (x and y value) from an Excel file into Anylogic. Since I need a function for the optimization in the course of the simulation, I want to determine a function or the parameters for a tangent function from this data and output it. Since this should happen in Anyogic, I am looking for a solution. As already described I used a code from Apache (Common Math curve fitter), which contains both Gaussian functions and polynomial functions. However, the function is still too soft, so I would like to try a tangent function. – Handballer73 Jan 27 '21 at 09:22
  • As I said, there are no generalised data-fitting capabilities built-in to AnyLogic (other than some for general interpolation --- see Table Functions in the help) and, from your description, that isn't intrinsically something that should be done in the simulation. (You'd do it beforehand using something like Stat::Fit.) Also determining *parameters* for a tangent function makes sense, but what does "determine a *function* [...] for a tangent function" mean? – Stuart Rossiter Jan 27 '21 at 11:01
  • You may also want to explain what "I need a function for the optimization" means? There is nothing intrinsic in AnyLogic (heuristic) optimization which 'needs functions', so I assume you're just saying something in your to-have-its-parameters-optimized simulation design means you want to use a data-fitted function (rather than the discrete empirical data values) for some unexplained reason. – Stuart Rossiter Jan 27 '21 at 11:09
  • To go a little further, perhaps: My overall model is to run for 33 years later. But the optimization itself should run for every month of a year. So it is not about an optimization of the whole model but of intermediate results. Since I didn't find a solution for this, the idea was to implement a curvefitter that outputs my histogram data as a function, which can then be used as a condition in the optimization. But to get the function for the data, I need this curvefitter that gives me the parameters of a function. – Handballer73 Jan 27 '21 at 13:48
  • This has already worked with a polynomial function, but the histogram data is more like an arctan function. The used curvefitter is only based on polynomial equations and now I am looking for an alternative solution...but maybe it is possible to optimize the data/paramters yearly although the overall model runs over many more years. – Handballer73 Jan 27 '21 at 13:48
  • do not know how important it is but the optimization itself is written in gurobi – Handballer73 Jan 27 '21 at 13:54

2 Answers2

0

There is an example model called "Calibration of agent based SIR model" that does what you are looking for: Calibrate model parameters so the output matches a given function (not tangent in this example but easy to adjust)

Benjamin
  • 10,603
  • 3
  • 16
  • 28
  • where can i find it? but i don't want to change the model parameters, i want to determine the parameter of a function fitting best to my data...and the function should be a tangent function – Handballer73 Jan 27 '21 at 07:36
  • In the AnyLogic Menu -> Help -> Example models. Good practice to always check there, many many use cases are covered, really good resource. The model is doing what you are looking for, I think – Benjamin Jan 27 '21 at 08:47
  • ah ok i found it under the name " SIR Agent Based Calibration "...i will try it but i still think it is not what I´m searching for...maybe you have a look on my comment under Stuart – Handballer73 Jan 27 '21 at 09:31
0

Short answer

AnyLogic does not have any data-fitting capabilities built-in, other than simple interpolation of discrete data (see Table Functions in the help). So

(a) if you needed to do it in-model (e.g., driven by some model state), you'd need to find a suitable Java library that did what was missing in what you'd already tried (Apache Commons), and call that from the AnyLogic model;

(b) if you could do it outside the model, use a data-fitting tool like Stat::Fit (which exists as a plug-in for some sim tools like Simul8, but not for AnyLogic).

Longer answer

Based on your additional explanatory comments, it sounds like this is a question where it's crucial to properly explain your context, and perhaps you don't need to use data-fitting at all (and there may be a more 'AnyLogic-centric' way of approaching it in that case). Particularly around the intended interaction between simulation and (mathematical) Gurobi optimisation; note that AnyLogic has built-in heuristic optimisation via OptQuest so any normal discussion of 'optimisation' with AnyLogic is referring to that.

On the one hand you seem to suggest you want to fit a function to some input data to your simulation. (You talk about having Excel inputs and wanting to fit a curve to it.)

On the other hand, you seem to suggest you want an approach where you are optimising at intermediate time intervals based on run-time model state. But what is the optimiser determining and how do its results affect the ongoing execution of the simulation? You say "So it is not about an optimization of the whole model but of intermediate results. Since I didn't find a solution for this". What 'solution' are you looking for? This sounds like an approach where you're modelling decisions for time period N being made inside the simulation, where those decisions are based on an optimisation using the outcomes from period N-1 as its inputs (and thus the optimisation is effectively based on a simplified emulation of the simulation using a function, since the simulation is already supposed to be the most-accurate computational representation of the real-world system).

So perhaps(?) you're saying that you are emulating/approximating the simulation as a function of its input data (where you happen to think a tangent function fits). In which case the original suggestion (a) is probably the only thing that makes sense. Though, even then, when you are optimising for anything after the first time period, the 'inputs' are no longer the original model inputs; they are some representation of the simulation's current state/outcomes (so it's not clear that this relates to the Excel input data directly, and so maybe I'm barking up the wrong tree).

Stuart Rossiter
  • 2,432
  • 1
  • 17
  • 20
  • I'd also suggest editing your question to fill in the detail you added in comments, together with anything that becomes clearer (in terms of how to present your problem) given the questions I'm asking here in this answer. But your call based on whether the 'long answer' here helps or not! – Stuart Rossiter Jan 28 '21 at 12:14
  • ...Or maybe leave this as a specific 'data-fitting with AnyLogic sims' question, but have a separate question on the sim+optimisation context and how to achieve it(?) – Stuart Rossiter Jan 28 '21 at 12:24
  • First of all, thank you for the detailed answer. About the input data: Every year, depending on the progress of the simulation, the input data and thus also the histogram data change. Since these have influence on the conditions in the optimization, the histogram data should be integrated as function, whose parameters change however annually. By a Curvefitter I would get these also annually and could adapt so the condition in the optimization. But maybe there is a smarter way to do this. – Handballer73 Jan 30 '21 at 11:27
  • After one year, the results are saved in an Excel file and used as a baseline for the next year's simulation. – Handballer73 Jan 30 '21 at 11:30