JuliaDynamics / Agents.jl

Agent-based modeling framework in Julia
https://juliadynamics.github.io/Agents.jl/stable/
MIT License
762 stars 125 forks source link

3D visualization of agents in julia #369

Closed AayushSabharwal closed 3 years ago

AayushSabharwal commented 3 years ago

Julia currently supports drawing of 2D continuous and grid spaces. It is slightly restrictive, and I think there is a case to be made for 3D visualizations. It should be possible in a similar pattern to the InteractiveChaos implementation (https://github.com/JuliaDynamics/InteractiveChaos.jl/blob/master/src/agents/agents.jl). I would be willing to work on implementing this.

Additionally, it might also just be useful to make 3D data plots, visualizing the interdependency of 3 parameters as the model evolves. This could be implemented as a separate feature.

Is your feature request related to a problem? Please describe. Not a problem, but something I see as a potential area for improvement

Describe the solution you'd like Makie.jl already supports 3D plots. Agents in a continuous/grid 3D space could be visualised by drawing 3D solids at those positions.

Describe alternatives you've considered I think Makie might be the best option, but I have also opened this issue to see if there are other alternatives/better ways of doing it, and if anyone else has tackled this in their own way.

If there is anything about the feature request I should elaborate on further, I am willing to do so.

Datseris commented 3 years ago

Sure, we want to try and move plotting of Agents entirely into InteractiveChaos.jl. Do you have a 3D model? The first step is for you to come up with a 3D model to actually test the implementation. We anyways plan to move Agents.jl entirely to plotting with Makie.jl (and InteractiveChaos.jl), so this issue is timely and your help welcomed.

Datseris commented 3 years ago

Additionally, it might also just be useful to make 3D data plots, visualizing the interdependency of 3 parameters as the model evolves. This could be implemented as a separate feature.

This isn't related with Agents.jl, but what you do with the data after you have them. I see no reason that such a functionality should be a "feature" of Agents.jl. We can have an example in the documentation of doing such a thing, but implementing a source function that does it feels unnecessary.

Libbum commented 3 years ago

DataVoyager does a good job with 2D representations of data, but as for a Makie-based 3D dataviewing system: I have yet to see one. Something like that would be super useful, although I agree with @Datseris here that this should be something to consider outside the scope of Agents.jl.

Having said that, a decent 3D model with a custom 3D representation is certainly something we'd like to include in the examples section of the documentation. Usually these kind of things are the seed points of new packages anyway...

Datseris commented 3 years ago

I started working on using InteractivePlotting.jl for all plotting purposes of Agents.jl. The progress is here: https://github.com/JuliaDynamics/InteractiveChaos.jl/pull/30

@AayushSabharwal once you have a 3D ABM you can contribute to InteractiveChaos.jl by updating the abm_plot function to work with 3D (you'll need to add an if clause that uses Scene instead of Axis, which can allow for 3D plots).

My only concern is that I'm not sure how to plot networks with Makie. I'm not sure if it is even possible, but I still find it worth it to drop network plots and switching to Makie.

Libbum commented 3 years ago

Graph plotting is tracked in JuliaPlots/Makie.jl#119. So not quite yet, but its on the roadmap.

Marc-Cox-08 commented 3 years ago

Maybe search through the Makie Demo Gallery (newer gallery exists ?) to see if something fits here >> http://juliaplots.org/MakieReferenceImages/gallery/ You can also search by keyword here , for example enter 3D into Search box.

Go here and scroll down to see what reminded me of a Chaos Lorentz Attractor animation ( but its really a DifferentialEquations path animation with adjustable Camera POV ) >> http://juliaplots.org/MakieReferenceImages/gallery/differentialequations_path_animation/index.html

Some Code tips about Makie Syntax >> https://makie.juliaplots.org/dev/interaction.html

Datseris commented 3 years ago

@Marc-Cox-08 ? We know how to use Makie.jl. Also, the reference gallery you show is outdated. Also, you can run an animation of the Lorenz attractor by running:

using InteractiveChaos
using DynamicalSystems, GLMakie
using OrdinaryDiffEq

ds = Systems.lorenz()

u1 = [10,20,40.0]
u2 = [10,20,40.0 + 1e-3]
u3 = [20,10,40.0]
u0s = [u1, u2, u3]

idxs = (1, 2, 3)
diffeq = (alg = Tsit5(), dtmax = 0.01)

figure, obs = interactive_evolution(
    ds, u0s; idxs, tail = 1000, diffeq
)
AayushSabharwal commented 3 years ago

I'll get to work on a 3D ABM. Currently, Julia already has an example for boids flocking (https://juliadynamics.github.io/Agents.jl/stable/examples/flock/), which is usually the 3D demo of choice. I was thinking either that could be reworked into a 3D model, or a simple inverse-square model for gravitation. Thoughts?

Libbum commented 3 years ago

3D flocking is the straightforward choice, but I think I'd be quite interested to see what you had in mind for a gravitation model. The novelty of that one would be far more interesting from an ABM perspective. Either would be completely fine as a start though.

Datseris commented 3 years ago

But gravity models suit much more continuous ODEs though...? But I'm also looking forward to see what you mean by "gravitation".

AayushSabharwal commented 3 years ago

My idea is as follows: Agents are planets, with a specific mass, position and velocity in a 3D continuous space. agent_step! for each agent calculates the net force on it due to all other agents using the inverse-square law, then updates the velocity by calculating acceleration as a = F/m and assuming a dt delta time. \delta v = adt

Datseris commented 3 years ago

Okay, and my question is "Why would you make this an ABM instead of solving the well-established, well-known equations of planetary motion as a set of ODEs"?

To my understanding so far, ABMs target situations where there are no governing mathematical rules, and ad hoc behaviors are implemented instead. If you have a mathematical theory, why go through the ABM route? Solving ODEs also allows you to drop the trivial Euler stepping scheme and instead use an advanced RungeKutta solver.

AayushSabharwal commented 3 years ago

Oh. I understand now, your point is valid. It doesn't make sense to model gravitation here. The only real purpose gravitation solves is 3D visualization, which isn't the point of the library. Boids shouldn't require a large change to the source, and is also a well-known ABM. I'll look around and see if there are any other simplistic 3D models that can be added to examples.

Datseris commented 3 years ago

@Libbum I can just use your battle royale as a 3D example in the meantime, right? The 3rd dimension "height" of the agents is just their level, no? Then even intuitively it makes sense to plot them in 3D?

Libbum commented 3 years ago

That should be fine for now, yeah.

Datseris commented 3 years ago

Actually, I really want to push 4.0 out, so I'll not do any work on 3D plotting until we're through this. I want to be done by next Sunday. If Aayush wants to work on 3D plotting that's welcomed. The PR I've linked works now with 2D continuous and discrete, tested thoroughly. I'll now spend time to see if we can re-make the Agents.jl docs with CairoMakie.

Libbum commented 3 years ago

Sure. Let's put this on the roadmap for 4.1 then.

AayushSabharwal commented 3 years ago

I have modified the flocks example to use 3D coordinates (which was just changing a bunch of 2s to 3s). I modified the plot_abm and plot_abm! functions from the PR @Datseris linked above to work for 3D. The boids are currently shown as red spheres. The code is over in https://github.com/AayushSabharwal/julia-agents-wip. If there's anything I can improve on, I'll do so.

Libbum commented 3 years ago

Great! We're in the process of getting the Makie ecosystem into Agents and replacing Plots.jl. Once that is done, we can help you refactor this into a pull request & merge it in as our first 3D example.

Datseris commented 3 years ago

I'm finished with my code. Would be nice if you can have a look and play around. You need the following two PRs checked out:

https://github.com/JuliaDynamics/InteractiveChaos.jl/pull/30

and here is its application in building plots and videos for the Agents.jl docs:

https://github.com/JuliaDynamics/Agents.jl/pull/370