Sleep pattern algorithm

Started by
5 comments, last by iMalc 14 years, 8 months ago
Hi, So im trying to come up with a sleeping algorithm for my pet game. Basically i'd like it to be as close as human sleeping behavior. Pets start off with their normal sleeping hour. But during the duration of a game, if the players keep their pets stay up too late, or make them go to bed too early then their sleeping patterns will change accordingly. For example: a pet's initial sleeping time is from 12am - 8am. but for a duration of days, if a player keeps them stay up til 3am, then eventually they wont go to sleep by themselves earlier than 3 in the morning. SOrry for my bad english, and thank for reading.
Advertisement
One simple option is to calculate extra hours of sleep needed based on how late you kept them awake. For example keep it awake for an extra two hours, and it needs one hour more sleep than usual. That will move it's normal sleep pattern.
I forget what this is called but if you wanna search then look for learnining ai stuff (might be reinforcment learning).

Just to make it easy for now, theres 10 hours in a day and 10 minutes per hour :P.

Normally your creature sleeps at say hour 5, it sleeps Always for 2 hours. Now we don't need to modify the end time since its just start + 2.

you get a leanrning rate (lets say 0.1) and then when the pet goes to sleep you do:

normal_bed_time = normal_bed_time*(1-learning_rate) + actual_bed_time*learning rate

So if normally bed time is 5 but they didn't go to bed till 6 then normal bed time becoems
5*(1-0.1) + 6 * 0.1 = 5.1

By changing that learning rate you can change how fast the sleep time changes. Your going to need something to allow it to sleep before its normal bed time (like say 30 mmins before normal) otherwise its neveer gonna be able to reduce.

Just a quick example:
Normal bed time is 0 (mid night) but the player keeps them up untill 3 am for a few days:
learning rate = 0.4
bed time = 0
bed time = 0.0*0.6 + 3*0.4 = 1.2
bed time = 1.2*0.6 + 3*0.4 = 1.92
bed time = 1.92*0.6 + 3*0.4 = 2.352
bed time = 2.352*0.6 + 3*0.4 = 2.6112

Eventually it will converge on 3 (depending on your learning rate, might take a while). With 0.4 it took 12 days to get to 2.99. If you then start sleeping earlier it will go back down again.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

You could also give your pets an "energy" variable that is full at, say, 100 and decays over time. When the energy is below 25% or so, the pet can display fatigue and encourage the user to make it go to sleep, which will restore this value. That way, if the user ignores the pet's behavior, it will have to restore more of its energy and thus take longer to restore, changing its sleeping habit based on how much the user drained it.

You could then adjust the rate of energy decay, treshold for the fatigue behavior, and restoration rate according to your preference.
Nanoha's solution seems very nice and not too hard to implement. NOw I have to see if I make my pet sleep in daytime n stay up and night :D
Im gonna try to submit some pics soon tothe gamedev gallery. so look forward to that

Thanks for the suggestion guys.
Quote:Original post by Tutukun
But during the duration of a game, if the players keep their pets stay up too late, or make them go to bed too early


Keep in mind that you might not want either of those to work all the time. Maybe the pet falls asleep (or can't sleep) whatever the player does. :)
I've often noticed that if I'm sleep deprived that it's not the following sleep that I sleep longer for, but the one the day after that. Trying to factor that in crushes any ideas I had myself.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement