java java中的模拟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/523541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:44:47  来源:igfitidea点击:

Simulation in java

javasimulation

提问by joel.neely

I am novice to the simulation world, and want to learn how programmers develop real simulation projects in java. I would use eclipse. Could anyone point to other things that I need to know (e.g. other packages, software etc. and their purposes)?

我是仿真界的新手,想学习程序员如何用java开发真实的仿真项目。我会使用日食。谁能指出我需要知道的其他事情(例如其他包、软件等及其目的)?

I am afraid the question might seem a bit vague as it is not clear which type of project I am talking about. But being a novice, let me say that it's to begin how to code a simulation project.

我担心这个问题可能看起来有点含糊,因为不清楚我在谈论哪种类型的项目。但作为一个新手,让我说这是开始如何编码一个模拟项目。

回答by ConcernedOfTunbridgeWells

If you are building a Monte-Carlo modelfor a discrete event simulationor simulation model for pricing derivativesyou should find there is a bodyof frameworkcodealready out there. If you are doing a numerical simulation such as a finite-element modelyou will be basing your simulation on a matrixcomputationlibrary. Other types of simulationexist, but these are the two most likely cases.

如果你正在建设一个蒙特卡罗模型离散事件仿真和仿真模型定价的衍生品,你会发现有一个身体框架代码已经在那里。如果您正在进行数值模拟,例如有限元模型,您的模拟将基于矩阵计算库。 存在其他类型的模拟,但这是两种最可能的情况。

I've never written a finite element model and know next to nothing about these, although I did have occasion to port one to DEC Visual FORTRANat one point. Although the program (SAFIR,if anyone cares) was commented in French, the porting exercise consisted of modifying two date functions for a total of 6 lines of FORTRAN code - and writing a makefile.

我从来没有写过一个有限元模型,对这些几乎一无所知,尽管我曾经有机会将一个模型移植到DEC Visual FORTRAN。尽管程序(SAFIR,如果有人关心的话)是用法语注释的,移植练习包括修改两个日期函数,总共 6 行 FORTRAN 代码 - 并编写一个 makefile。

Monte-Carlo models consist of measuring some base population to get distributionsof one or more variables of interest. Then you take a Pseudo-Random number generatorwith good statisticaland geometricproperties (the Mersenne Twisteralgorithm is widely used for this) and write a functionto convert the output of this to a random variable with the appropriate distribution.You will probably be able to find libraryfunctionsthat do this unless your variables have a really unusual distribution.

蒙特卡罗模型包括测量一些基本人口以获得一个或多个感兴趣变量的分布。然后,您使用具有良好统计几何特性的伪随机数生成器Mersenne Twister算法广泛用于此)并编写一个函数将其输出转换为具有适当分布随机变量。除非您的变量具有非常不寻常的分布,否则您 可能会找到执行此操作的函数

Then you build or obtain a simulation framework and write a routine that takes the random variables and does whatever computation you want to do for the model. You run it, storing the results of each simulation, until the error is within some desired tolerance level. After that, you calculate statistics (means, distributions etc.) from all of the runs of the simulation model.

然后您构建或获得一个模拟框架并编写一个例程,该例程采用随机变量并执行您想要为模型执行的任何计算。您运行它,存储每个模拟的结果,直到误差在某个所需的容限范围内。之后,您可以从模拟模型的所有运行中计算统计数据(均值、分布等)。

There are quite a lot of resources on the web, and many books on simulation modelling, particularly in the area of derivatives pricing. You should hunt around and see what you can find.

网上有很多资源,还有很多关于模拟建模的书籍,特别是在衍生品定价领域。你应该四处寻找,看看你能找到什么。

As an aside, the randommodule on Pythonhas conversion functions for quite a few distributions. If you want one you could get that and port the appropriate conversion function to java. You could use the output of the python one with the same random number seed to test the correctness of the java one.

顺便说一句randomPython上的模块具有许多发行版的转换函数。如果你想要一个,你可以得到它并将适当的转换函数移植到 java。您可以使用具有相同随机数种子的python 的输出来测试java 的正确性。

回答by joel.neely

Discrete-event simulation is a good option for problems that can be modeled as individual events that take place at specific times. Key activities are:

对于可以建模为在特定时间发生的单个事件的问题,离散事件模拟是一个不错的选择。主要活动是:

  • randomly generating times and durations based on empirical data, and
  • accumulating statistics as the simulation runs.
  • 根据经验数据随机生成时间和持续时间,以及
  • 在模拟运行时累积统计数据。

For example, you could simulate the activity in a parking garage as the entries and departures of a cars and the loss of customers who can't enter because the garage is full. This can be done with two model classes, a Car and the Garage, and three infrastructure classes, an Event class (described below), a Schedule to manage events, and a Monitor to accumulate data.

例如,您可以将停车场中的活动模拟为汽车的进出以及由于车库已满而无法进入的客户流失。这可以通过两个模型类(Car 和 Garage)和三个基础结构类、一个 Event 类(如下所述)、一个管理事件的 Schedule 和一个用于累积数据的 Monitor 来完成。

Here's a brief sketch of how it could work.

这是它如何工作的简要草图。

Event

事件

An Event has a time, and represents calling a specific method on an object of a specific class.

一个事件有一个时间,代表对特定类的对象调用特定的方法。

Schedule

日程

The Schedule keeps a queue of Events, ordered by Event time. The Schedule drives the overall simulation with a simple loop. As long as there are remaining Events (or until the Event that marks the end of the simulation run):

时间表保持一个事件队列,按事件时间排序。计划通过一个简单的循环来驱动整体模拟。只要还有剩余的事件(或直到标志着模拟运行结束的事件):

  1. take the earliest Event from the queue,
  2. set the "world clock" to the time of that event, and
  3. invoke whatever action the Event specifies.
  1. 从队列中取出最早的事件,
  2. 将“世界时钟”设置为该事件的时间,并且
  3. 调用事件指定的任何操作。

Car

The Car class holds the inter-arrival and length-of-stay statistics.

Car 类保存到达间隔和停留时间统计数据。

When a Car arrives, it:

当汽车到达时,它:

  1. logs its arrival with the Monitor,

  2. consults the world clock, determines how long before the next Car should arrive, and posts that arrival Event on the Schedule.

  3. asks the Garage whether it is full:

    1. if full, the Car logs its departure as a lost customer with the Monitor.

    2. if not full, the Car:

      1. logs its entry with the Monitor,

      2. tells the Garage it has entered (so that the Garage can decrease its available capacity),

      3. determines how long it will stay, and posts its departure Event with the Schedule.

  1. 用监视器记录它的到达,

  2. 查询世界时钟,确定下一辆车应在多长时间前到达,并将该到达事件发布到日程表中。

  3. 询问 Garage 是否已满:

    1. 如果已满,Car 将其离开作为丢失客户记录在 Monitor 中。

    2. 如果未满,汽车:

      1. 使用监视器记录其条目,

      2. 告诉车库它已经进入(这样车库可以减少它的可用容量),

      3. 确定它将停留多长时间,并将其离开事件与时间表一起发布。

When a Car departs, it:

当汽车离开时,它:

  1. tells the Garage (so the Garage can increase available capacity), and

  2. logs its departure with the Monitor.

  1. 告诉车库(这样车库可以增加可用容量),和

  2. 用监视器记录它的离开。

Garage

车库

The Garage keeps track of the Cars that are currently inside, and knows about its available capacity.

车库会跟踪当前在里面的汽车,并了解其可用容量。

Monitor

监视器

The Monitor keeps track of the statistics in which you're interested: number of customers (successfully-arriving Cars), number of lost customers (who arrived when the lot was full), average length of stay, revenue (based on rate charged for parking), etc.

监控器会跟踪您感兴趣的统计数据:客户数量(成功到达的汽车)、流失客户的数量(当车位已满时到达)、平均停留时间、收入(基于收取的费用)停车)等。

A simulation run

模拟运行

Start the simulation by putting two Events into the schedule:

通过将两个事件放入计划中来开始模拟:

  1. the arrival of the first Car (modeled by instantiating a Car object and calling its "arrive" event) and

  2. the end of the simulation.

  1. 第一辆 Car 的到达(通过实例化 Car 对象并调用其“到达”事件来建模)和

  2. 模拟结束。

Repeat the basic simulation loop until the end-of-simulation event is encountered. At that point, ask the Garage to report on its current occupants, and ask the Monitor to report the overall statistics for the session.

重复基本的模拟循环,直到遇到模拟结束事件。此时,让 Garage 报告其当前居住者,并要求 Monitor 报告会话的整体统计数据。

回答by Niyaz

The short answer is that it depends.

简短的回答是,这取决于

Unless you can make the question more specific, there is no way to give an answer.

除非你能把问题说得更具体,否则没有办法给出答案。

What do you want to simulate?

你想模拟什么?

For example, if you want to simulate adding two numbers, you can do it using something like:

例如,如果您想模拟两个数字相加,您可以使用以下方法:

a = b + c;

If you want to simulate the bouncing of a ball, you can do that using a little bit of math equations and the graphic libraries.

如果你想模拟一个球的弹跳,你可以使用一些数学方程和图形库来做到这一点。

If you want to simulate a web browser, you can do that too.

如果你想模拟一个网络浏览器,你也可以这样做。

So the exact answer depends on what simulation you want to do.

因此,确切的答案取决于您想要进行的模拟。

回答by duffymo

Come up with a problem first.

先提出一个问题。

There's no such things as a generic "simulation". There are lots of techniques out there.

没有通用的“模拟”之类的东西。那里有很多技术。

If you're just a gamer who wants to have pseudo-physics, maybe something like thiswould be what you had in mind.

如果你只是谁希望有伪物理游戏玩家,也许像将是你脑子里想的是什么。

回答by yoavram

Have a look at Repast Symphony: http://repast.sourceforge.net/repast_simphony.html

看看 Repast Symphony:http://repast.sourceforge.net/repast_simphony.html

"Repast Simphony 2.0 Beta, released on 12/3/2010, is a tightly integrated, richly interactive, cross platform Java-based modeling system that runs under Microsoft Windows, Apple Mac OS X, and Linux. It supports the development of extremely flexible models of interacting agents for use on workstations and small computing clusters.?

"Repast Simphony 2.0 Beta 于 2010 年 12 月 3 日发布,是一个紧密集成、交互丰富、跨平台的基于 Java 的建模系统,可在 Microsoft Windows、Apple Mac OS X 和 Linux 下运行。它支持极其灵活的开发用于工作站和小型计算集群的交互代理模型。?

Repast Simphony models can be developed in several different forms including the?ReLogo ?dialect of Logo, point-and-click?flowcharts,?Groovy, or?Java, all of which can be fluidly interleaved?NetLogo ?models can also be imported.

Repast Simphony 模型可以以几种不同的形式开发,包括?ReLogo?Logo 方言、点击式流程图、?Groovy 或?Java,所有这些都可以流畅地交错?NetLogo ?模型也可以导入。

Repast Simphony has been successfully used in many application domains including?social science,?consumer products,?supply chains,?possible future hydrogen infrastructures , and?ancient pedestrian traffico name a few."

Repast Simphony 已成功应用于许多应用领域,包括“社会科学”、“消费品”、“供应链”、“未来可能的氢基础设施”以及“古代行人交通”等等。”

回答by Pete855217

This is an old question, but for Simulation in Java I just installed and tested JavaSim by Mark Little, University of Newcastle upon Tyne. As far as I can tell, it works very well if you have a model you can convert into a discrete event simulation. See Mark's site http://markclittle.blogspot.com.au/2008/03/csimjavasim.html. I also attempted to use Desmo-J, which is very extensive and has a 2-D graphical mode, but could not get it going under JDK 1.6 on a Mac.

这是一个老问题,但对于 Java 中的模拟,我刚刚安装并测试了由泰恩河畔纽卡斯尔大学的 Mark Little 编写的 JavaSim。据我所知,如果您有一个可以转换为离散事件模拟的模型,它会非常有效。请参阅 Mark 的站点http://markclittle.blogspot.com.au/2008/03/csimjavasim.html。我还尝试使用 Desmo-J,它非常广泛并且具有 2-D 图形模式,但无法在 Mac 上的 JDK 1.6 下运行。