简体   繁体   English

Java中的最大流图仿真

[英]Max-Flow graph simulation in Java

I am writing Java program for league sports that goes through current set of played games of each team and their schedules of next games, and then based on that I make a model of flow network. 我正在编写用于联赛的Java程序,该程序将遍历每支球队的当前比赛情况以及下一场比赛的时间表,然后在此基础上建立一个流程网络模型。 The idea of program is to find which teams are already eliminated and have no chances of wining or sharing 1 place with any other team. 该计划的目的是找出哪些团队已经被淘汰,并且没有机会赢得或与其他团队分享1个席位。 After analyzing network (applying EdmondsKarp algo.) I find out if team get's eliminated or not. 在分析了网络(应用EdmondsKarp算法)之后,我发现团队是否被淘汰。 Now I also want to simulate this. 现在我也想模拟一下。 I am using JGraphT as graph library, and will probably be using JGraph for visualization (reason: once I create JGraphT objects I can simply instantiate JGraph objects with them and display graph). 我正在使用JGraphT作为图形库,并且可能会使用JGraph进行可视化(原因:一旦创建JGraphT对象,我就可以使用它们实例化JGraph对象并显示图形)。 I also found out for Jung framework yesterday, seems nice. 昨天我也发现了Jung框架,看起来不错。

Main problem is I never wrote simulation and it's the point where I need "Hello World" help. 主要问题是我从未编写过仿真程序,而这正是我需要“ Hello World”帮助的关键所在。 When I say simulation I mean I want to visually show every part of algorithm execution, and here is example scenario: algorithm has to find augmenting paths, so I want to show when every new edge is added to augmenting path. 当我说仿真时,我的意思是我想直观地展示算法执行的每个部分,这里是示例场景:算法必须找到扩展路径,因此我想展示何时将每个新边添加到扩展路径中。 User will be able to play and stop animation. 用户将能够播放和停止动画。 I also want to show changes in flow in all edges and things like that. 我还想展示所有方面的流量变化之类的东西。 So far, I have algorithm working but I don't know how to approach simulation. 到目前为止,我可以使用算法,但是我不知道如何进行仿真。 Should I be using separate thread for simulation execution? 我应该使用单独的线程进行模拟执行吗? Should I write separate class that would be executed as algorithm but with states recording without even knowing for real algorithm (because I don't want to interrupt performance of real algorithm). 我是否应该编写一个单独的类,该类将作为算法执行但带有状态记录,甚至不知道真实算法(因为我不想中断真实算法的性能)。 Should I use current algorithm and add some lines in between for saving execution states in some data structures that I could use later for displaying simulation to user? 我是否应该使用当前算法并在两者之间添加一些行,以将执行状态保存在某些数据结构中,以便稍后用于向用户显示模拟? Any ideas might help.. 任何想法可能会有所帮助..

If I do understand you correctly, you're asking for a way to animate your algorithm and control its execution interactively from within the animation, which is not quite the same as simulation (a simulation just executes a model, usually over a given time interval - which has nothing to do with user interaction or animation, but of course could be combined with both as well). 如果我确实理解正确,那么您正在寻找一种方法来使算法动画化,并从动画内部以交互方式控制算法的执行,这与仿真并不完全相同(仿真只是在给定的时间间隔内执行模型) -与用户互动或动画无关,但当然也可以将两者结合使用)。

I would suggest you split the problem into the two main parts, interaction and animation. 我建议您将问题分为两个主要部分:交互和动画。 Both can be solved by applying a model-view-controller approach: 都可以通过应用模型-视图-控制器方法来解决:

  • To interact with your algorithm, identify the 'atomic steps' you want to distinguish, eg the addition of an edge to a path. 要与算法交互,请确定要区分的“原子步骤”,例如,在路径上增加一条边。 Then you either extend your algorithm to also work step-wise or write an extra class that wraps the algorithm and provides the necessary routines for a stepwise execution. 然后,您可以扩展算法使其也可以逐步执行,也可以编写一个额外的类来包装该算法并为逐步执行提供必要的例程。

  • To animate the current state you algorithm is in, you should use the observer pattern , where your animation component is the observer and gets notified by the algorithm whenever its state changed, eg an edge was added to a path. 为了使算法处于当前状态,请使用观察者模式 ,其中动画组件是观察者,并且只要其状态发生变化(例如,将边线添加到路径),算法都会通知该动画组件。 You could also describe the actual state change by passing a hint (such as the edge object that has been added to the path); 您还可以通过传递提示来描述实际状态变化(例如已添加到路径的边缘对象); this might make it easier to visualize the difference between old and new state. 这可能使可视化旧状态和新状态之间的差异变得更加容易。

Regarding you threading questions: the algorithm should probably run in an extra thread (unless it's very very fast), and you could also put the animation in an extra thread (this is probably already provided by JGraph anyway, just check the docs or use their components as advised). 关于您的线程问题:算法可能应该在额外的线程中运行(除非非常快),您还可以将动画放入额外的线程中(无论如何,JGraph已经提供了动画,只需检查文档或使用其文档即可)。建议的组件)。 You should note, however, that your algorithm's runtime performance is almost certainly affected by the aninmation, even if it is running in another thread (since notification still has to be done by the algorithm) - so be careful with performance analysis and use an un-animated version for such studies. 您应注意,但是,你的算法的运行时性能几乎可以肯定是受aninmation,即使在另一个线程运行(因为通知还必须通过算法来进行) -所以要小心性能分析和使用一个未动画版本用于此类研究。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM