简体   繁体   English

我的特定基于代理的模型(ABM)的Java或C ++?

[英]Java or C++ for my particular agent-based model (ABM)?

I unfortunately need to develop an agent-based model. 遗憾的是,我需要开发一个基于代理的模型。 My background is C++; 我的背景是C ++; I'm decent but not a professional programmer. 我很体面但不是专业的程序员。 My goal is to determine whether, my background aside for the moment, the following kind of algorithm would be faster or dramatically easier to write in C++ or Java. 我的目标是确定,目前我的背景是,使用C ++或Java编写以下类型的算法是更快还是更容易。

  1. My agents will be of class Host . 我的经纪人将成为班Host Their private member variables include their infection and immune statuses (type int ) with respect to different strains. 他们的私人成员变量包括他们对不同菌株的感染和免疫状态( int型)。 (In C++, I might use an unordered_map or vector to hold this information, depending on the number of strains.) I plan to keep track of all hosts in a vector, vector< Host *> hosts . (在C ++中,我可能会使用unordered_mapvector来保存这些信息,具体取决于应变的数量。)我计划跟踪向量中的所有主机, vector< Host *> hosts
  2. The program will need to know at any time all the particular hosts infected with a particular strain or with immunity to a particular strain. 该程序将需要随时了解感染特定菌株或对特定菌株具有免疫力的所有特定宿主。 For each strain, I could thus maintain two separate structures, eg, vector< Host *> immune and vector< Host *> infectious (I might make each two-dimensional, indexed by strain and then host). 对于每个菌株,我可以因此保持两个独立的结构,例如vector< Host *> immunevector< Host *> infectious (我可能使每个二维,通过菌株索引然后宿主)。
  3. Hosts can die. 主人可能会死。 It seems like this creates a mess in C++, in that I would have to find the right individual to kill in host and search through the other structures ( immune and infectious ) to find all pointers to this object. 这似乎在C ++中造成混乱,因为我必须找到合适的个体在host杀死并搜索其他结构( immuneinfectious )以找到指向该对象的所有指针。 I'm under the impression that Java will delete all these pointers implicitly if I delete the underlying object. 我的印象是,如果删除底层对象,Java将隐式删除所有这些指针。 Is this true? 这是真的? Is there a dramatically better way to do this in C++ than what I have here? 在C ++中有比这更好的方法吗?

Thanks in advance for any help. 在此先感谢您的帮助。


I should add that if I use C++, I will use smart pointers. 我应该补充说,如果我使用C ++,我将使用智能指针。 That said, I still don't see a slick way to delete all pointers to an object when the object needs to go. 也就是说,当对象需要时,我仍然没有看到删除指向对象的所有指针的方式。 (When a host dies, I want to delete it from memory.) (当主机死机时,我想从内存中删除它。)

I realize there's a lot to learn in Java. 我意识到在Java中需要学习很多东西。 I'm hoping someone with more perspective on the differences between the languages, and who can understand what I need to do (above), can tell me if one language will obviously be more efficient than another. 我希望有人能够更多地了解语言之间的差异,以及谁能理解我需要做什么(上图),可以告诉我一种语言是否显然比另一种语言更有效。

I can't answer your all questions, but 我不能回答你的所有问题,但是

I'm under the impression that Java will delete all these pointers implicitly if I delete the underlying object. 我的印象是,如果删除底层对象,Java将隐式删除所有这些指针。

In Java you don't delete an object; 在Java中,您不会删除对象; instead, it gets effectively deleted when the reference count to it goes to zero. 相反,当它的引用计数变为零时,它会被有效删除。 However, you may want to utilize weak references here; 但是,您可能希望在此处使用弱引用 ; this way the object disappears when the strong reference count goes to zero. 这样,当强引用计数变为零时,对象就会消失。

I'm under the impression that Java will delete all these pointers implicitly if I delete the underlying object. 我的印象是,如果删除底层对象,Java将隐式删除所有这些指针。 Is this true? 这是真的?

Nope. 不。 You actually have it backwards; 你实际上倒退了; if you delete all the pointers, Java will delete the underlying object. 如果删除所有指针,Java将删除基础对象。 So you'll still need to search through all three of your data structures ( hosts , immune , and infectious ) to kill that particular host. 所以你仍然需要搜索所有三个数据结构( hostsimmuneinfectious )来杀死那个特定的主机。

However, this "search" will be fast and simple if you use the right data structures; 但是,如果使用正确的数据结构,这种“搜索”将是快速而简单的; a HashSet will do the job very nicely. HashSet可以很好地完成这项工作。


    private HashSet<Host> hosts;
    private HashSet<Host> immune;
    private HashSet<Host> infectious;

    public void killHost(Host deadManWalking) {
        hosts.remove(deadManWalking);
        immune.remove(deadManWalking);
        infectious.remove(deadManWalking);
    }

It's really that simple, and will take place in O(lg n) time. 它真的很简单,并且会在O(lg n)时间内发生。 (Though you will have to override the equals and hashCode methods in your implementation of Host ; this is not technically challenging.) (虽然您必须在Host的实现中覆盖equalshashCode方法;这在技术上并不具有挑战性。)

My memories of C++ are too hazy for me to give any sort of authoritative comparison between the two languages; 我对C ++的记忆对我来说太朦胧了,无法对这两种语言进行任何权威的比较。 I did a ton of C++ work in college, haven't touched it since. 我在大学里做了大量的C ++工作,从那时起就没有碰过它。 Will C++ code run faster? C ++代码会运行得更快吗? Done right and assuming you don't have any memory leaks, I'd suspect it would, though Java's rep as a slow language is mostly a holdover from its youth; 做得对,并假设你没有任何内存泄漏,我怀疑它会,虽然Java的代表作为一种缓慢的语言主要是从它的年轻人的保留; it's pretty decent these days. 这些天很不错。 Easier to write? 更容易写? Well, give that you'd be learning the language, probably not. 好吧,假设你正在学习这门语言,可能不是。 But the learning curve from C++ to Java is pretty gentle, and I personally don't miss C++ at all. 但是从C ++到Java的学习曲线非常温和,我个人根本不会错过C ++。 Once you know the languages, Java is, in my opinion, vastly easier to work with. 一旦你了解了这些语言,在我看来,Java就更容易使用了。 YMMV, natch, but it may well be worth the effort for you. YMMV,natch,但它可能值得为你付出努力。

Actually, your impression is basicly backwards: Java will assume an object (the host in this case) is dead when there's no longer any pointer to give access to that object. 实际上,你的印象基本上是倒退的:当没有任何指针可以访问该对象时,Java会假定一个对象(在这种情况下是主机)已经死了。 At that point it'll clean up the object (automatically). 此时它将自动清理对象。

At a guess, however, there's one collection that "owns" the hosts, and would be responsible for deleting a host when it dies. 然而,在猜测中,有一个“拥有”主机的集合,并且负责在主机死亡时删除它。 The other pointers to the host don't own it. 其他指向主机的指针不拥有它。 If that's the case, then in C++ you'd normally handle this by having the "owning" collection contain a shared_ptr to the host, and the other collections contain weak_ptr s to the host. 如果是这种情况,那么在C ++中,您通常会通过让“拥有”集合包含一个到主机的shared_ptr来处理它,而其他集合包含对主机的weak_ptr To use the object via a weak_ptr , you have to first convert that to a shared_ptr that you can dereference to get to the host itself. 要通过weak_ptr使用该对象,您必须先将其转换为shared_ptr ,您可以取消引用它以获取主机本身。 If, however, the object has been deleted, the attempt at converting the weak_ptr to a shared_ptr will fail, and you'll know the host is dead (and you can then delete your reference to it). 但是,如果该对象已被删除,则将weak_ptr转换为shared_ptr的尝试将失败,并且您将知道主机已死(然后您可以删除对它的引用)。

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

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