neat.py | small implementation of NEAT | Machine Learning library
kandi X-RAY | neat.py Summary
kandi X-RAY | neat.py Summary
My small personal implementation of [NEAT] (NeuroEvolution of Augmenting Topologies).
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Produce offspring of species .
- Create a genome .
- Generate population .
- calculate population size
- Create a networkx graph from a graph .
- Generate activation network .
- Perform crossover of two parents .
- Mutate a node
- Print the best fit of a species .
- The command line interface .
neat.py Key Features
neat.py Examples and Code Snippets
Community Discussions
Trending Discussions on neat.py
QUESTION
I am trying to train a neural network to play a SMB1 game made using Pygame. To get some speed up, I would like to use parallel processing in order to play multiple copies of the game at once, by different members of the population (and on different training data).
The root of my problem comes from the fact that Pygame isn't inherently instance-based; that is, it will only ever generate one window, with one display object. Because I can't create multiple Pygame windows and display objects for each process, the processes have to share a display object. This leads me to my first questions: Is there a way to have multiple instances of pygame, and if not, is there a (performance-light) method of concurrently drawing onto the display? a.k.a. each game draws to a different section of the whole window.
However, I don't really need every game to be rendered; I only care that at least one game instance is rendered, so that I can monitor its progress. My solution was then to assign each game a process id, and only the game with process id 0 would actually draw to the display. Concurrency problems solved! To accomplish this, I used multiprocessing.Process:
...ANSWER
Answered 2020-Oct-11 at 22:51I'll try to attack the main issues. My understanding of your actual problem is quite limited as I don't know what your code actually does.
"a way to use pygame as multiple different instances in different threads, spawned from the same process"This doesn't work as pygame is built on SDL2 which states "You should not expect to be able to create a window, render, or receive events on any thread other than the main one."
"a way to safely concurrently work with pygame's display (and update clock)"Same as above, the display only work in the main thread.
"a way to use multiprocessing.Process such that it doesn't need to pickle the method's class but can still access the class variables"You could pickle the methods using something like dill
, but it fells (to me) wrong to copy full on python object between processes. I'd go for another solution.
You need to use to serialize Python objects in order to send them between processes.
2. Has a way to tell the subprocess which process worker is being usedI don't understand what this mean.
It seems to me that the problem could be solved with better separation of data and visualization. The training should have no knowledge about any visualization, as it's not dependent on how you want to display it. So there should not be any reason to share the pygame display.
Once this is done, it shouldn't be too much problem (multi-threading always causes problems) to do what you're trying to do. Regarding the pickle issue; I'd try to avoid pickling Python objects and functions, and instead just pass basic primitives between threads and processes. It seems like you should be able to assign self.fitnessFromArray
with a simple int
instead and based on its value do the min/avg/max calculation in the thread/process instead.
If you want to do threading, then the main thread will take care of the rendering. It'll also spawn threads for the training. When the threads are completed they'll return their result (or put it in a thread safe storage) and the main thread will poll the data and display the result. If the work done by the training takes longer than one frame, then divide up the work so each thread only partially train and can continue where it left off the next frame.
The principal is the same if you instead want separate processes. The main process starts up several training processes and connect to them via sockets. From the sockets, you'd poll information about the state of the program and display it. It would basically be a client-server architecture (albeit on localhost) where the training scripts are servers and the main process is a client.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install neat.py
You can use neat.py like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page