prevent run_simulation(self, genomes, config) from return until a triggrt set in update(self, dt)

24 views
Skip to first unread message

Jefio Chen

unread,
Apr 14, 2024, 7:43:19 AMApr 14
to Kivy users support
Hi Sirs,
due to some reason I need to prevent run_simulation(self, genomes, config) from return until a triggrt set in update(self, dt).
How can I implement it in Kivy?

class PongGame(Widget):

    def __init__(self, **kwargs):
        pass

    def run_simulation(self, genomes, config):
        Clock.schedule_interval(self.update, 1.0 / 30.0)
           
    def load_neat_config(self):
        # Load Config
        # Run Simulation For A Maximum of 1000 Generations
        if self.simu_count < 1:
        # Run simulation with a population size of 1
            self.population.run(self.run_simulation, 1)
 
    def update(self, dt):
pass
       
thanks,
Jefio

elli...@cox.net

unread,
Apr 14, 2024, 11:34:10 AMApr 14
to Kivy users support
Please share more information about what you are trying to achieve.   A minimal runnable example is best.

 You would not want to prevent the return of a method in kivy.  This would lock up the UI.    You could test a variable in run_simulation, so it returns immediately if it is not ready to run.  You could also break the simulation up in phases, so it returns on every iteration, and only completes if some criteria has been met.

Here is an example that uses iter/next to split up a task per iteration.
from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.uix.widget import Widget
from kivy.properties import ColorProperty

kv =
"""
<ColorBox>:
   canvas:
       Color:
           rgba: self.color
       Rectangle:
           size: self.size
           pos: self.pos


BoxLayout:
   orientation: 'vertical'
   Label:
       text: 'Color Changing Boxes'
       size_hint_y: None
       height: dp(30)
   Button:
       id: start_button
       text: 'Start'
       size_hint_y: None
       height: dp(48)
       on_release: app.start_color_change()
   GridLayout:
       id: grid
       spacing: dp(2)
       cols: 10
"""


class ColorBox(Widget):
   color = ColorProperty(
'gray')


class ScheduleChangeApp(App):
   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self.widgets = None  # use to hold iter of widgets

   
def build(self):
       
return Builder.load_string(kv)

   
def on_start(self):
       
for _ in range(100):
           
self.root.ids.grid.add_widget(ColorBox())

   
def start_color_change(self):
       
self.root.ids.start_button.disabled = True
       
self.widgets = iter(self.root.ids.grid.children[::-1])
       
self.next_color_change()

   
def next_color_change(self, *args):
       
try:
           w =
next(self.widgets)
           w.color =
'yellow'
           
Clock.schedule_once(self.next_color_change, .1)
       
except StopIteration:
           pass


ScheduleChangeApp().run()


From: kivy-...@googlegroups.com <kivy-...@googlegroups.com> on behalf of Jefio Chen <jefi...@gmail.com>
Sent: Sunday, April 14, 2024 4:43 AM
To: Kivy users support <kivy-...@googlegroups.com>
Subject: [kivy-users] prevent run_simulation(self, genomes, config) from return until a triggrt set in update(self, dt)
 
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/7eb3aa10-9f9f-43e9-abe2-ec50cd89fd17n%40googlegroups.com.

Mia Chen

unread,
Apr 15, 2024, 10:08:53 AMApr 15
to kivy-...@googlegroups.com
thanks Sir,
I found a way to print out best genom although not default format.
I can delv in from this way.

--

Mia Chen

unread,
Apr 15, 2024, 10:09:02 AMApr 15
to kivy-...@googlegroups.com
Thanks, Sir,
I am utilizing NEAT-python module to make a simple test program.
The program seems working. but it generates training report automatically with incorrect data after  self.population.run or self.run_simulation returns. (same as mentioned on NEAT-python website, shown below)
        if self.simu_count < 1:
        # Run simulation with a population size of 1
            self.population.run(self.run_simulation, 1)

I also tried below, but self.update is not successfully scheduled.
it seems prevented by this while loop?

Clock.schedule_interval(self.update, 1.0 / 30.0)
while not self.simulation_done:
    pass

Once the call to the population object’s run method has returned, you can query the statistics member of the population (a neat.statistics.StatisticsReporter object) to get the best genome(s) seen during the run. In this example, we take the ‘winner’ genome to be that returned by pop.statistics.best_genome().
thanks,
Jefio

elli...@cox.net

unread,
Apr 15, 2024, 11:28:39 AMApr 15
to kivy-...@googlegroups.com
You do not want to use an while loop like this, it will lock up the UI.  You want to return to the kivy event loop.

You could create a kivy BooleanProperty, and use the associated event...

class MyWidget(Widget):  # what ever widget make sense for your code
    simulation_done = BooleanProperty(False)

    def on_simulation_done(self, obj, value):
        # called when simulation_done value changes
        # take the desired action here... 

    


From: kivy-...@googlegroups.com <kivy-...@googlegroups.com> on behalf of Mia Chen <jefi...@gmail.com>
Sent: Sunday, April 14, 2024 4:22 PM
To: kivy-...@googlegroups.com <kivy-...@googlegroups.com>
Subject: Re: [kivy-users] prevent run_simulation(self, genomes, config) from return until a triggrt set in update(self, dt)
 
Reply all
Reply to author
Forward
0 new messages