
Algorithmic Solution to My Insomnia - csmajorfive
http://ilya.sukhar.com/blog/an-algorithmic-solution-to-insomnia.html
======
wakkaflokka
My entire life, but especially in graduate school, I've had horrible sleep.
Both falling asleep, and staying asleep.

I did multiple sleep studies, used sleeping medications, took supplements,
tried to meditate, etc. and had no luck.

I went and did CBT-I, where they had me change my sleep hygeine (all your
standard recommendations of "no TV in the bedroom", etc). But the biggest
thing was they had me start off by going to bed at 1 am and waking up at 5am
every day for a week or two. I'd be so insanely tired, I would start falling
asleep quicker and staying asleep less interrupted (this was recorded on a
worksheet). Then they had me go to bed an hour earlier, record it. After a
while, we found my optimal schedule (bed at 11am, wake up at 630am), and I
stick to it no matter what, every single day.

It's solved my sleep issues, full-stop. I get tired by 11 pm, and wake up at
6:30 am feeling just fine.

FWIW, grad school - with no solid schedule - was disastrous on my sleep.
Moving to my 9-5 corporate job has made scheduling my sleep a whole lot
better.

Just my experience.

~~~
dkersten
> bed at 11pm, wake up at 630am

Interestingly, this is on a 90 minute boundary, so you should naturally be in
a wakeful sleep state when you awake. I’ve found that 7.5 hours is best for me
too, because 6 hours isn’t long enough and 9 hours is too long, so 7.5 is the
sweet spot (and anything not on a sleep cycle boundary and I wake up very
tired). 11pm also seems to be the best time for me to go to bed, although I’m
naturally a night person, but if I have to work, going to bed later doesn’t
leave me with enough time to get 7.5 hours and still make it to work.

So... I didn’t try a process like yours to find my optimal times, but mine
seems to be quite similar to yours.

Having said that, I’m not a light sleeper in that some noise doesn’t bother me
or wake me and I normally don’t have much trouble getting to sleep, but I do
find I often wake up still tired. Its mostly solved by keeping a consistent
sleep schedule and sticking to 7.5 hours.

~~~
tarnith
I've found the same, more than 7.5 or less and I wake up either feeling groggy
or tired.

------
petilon
I have seen multiple doctors, including sleep specialists, about my insomnia,
and all they wanted to do is put me on prescription sleep meds for life. Those
meds are addictive. I didn't want sleep meds. As a software engineer I was
more interested in finding and fixing the underling problem -- there has to be
one -- as opposed to the quick fixes the doctors were offering me. But that's
not how doctors think, at least the ones I saw.

After much research I believe I have succeeded in finding the root cause.
Cells in your body need calcium to go into ON state, and to turn OFF magnesium
needs to go in and displace the calcium. Stress depletes magnesium. When your
body runs out of magnesium your cells can't turn off. Then your muscles become
stiff and when your brain cells can't go into OFF state you can't sleep. The
solution that works well for me: magnesium supplements, especially magnesium
l-threonate, which can pass through what is known as the blood brain barrier.
This solved my insomnia as well as muscle stiffness.

~~~
TheSpiceIsLife
About three months ago, I started taking Magnesium Amino Acid Chelate 500mg,
equivalent to elemental magnesium 100mg, at a dose of twelve capsules a day
for three weeks, then dropping back to six capsules a day as a maintenance
dose. Changed my life. I'm calmer, more emotionally resilient, I sleep better
and longer and when I want. My interactions are others go much more how I'd
like most of the time: that is, I feel I'm being a reasonable person.

I recently started taking 8000IU of Vitamin D per day, as I realise I hardly
ever spend any time in the sun. This, along with the magnesium, seems to have
made my emotional roller coaster and suicidal ideation completely evaporate.

I also regularly take zinc, C, lipoic acid.

~~~
bloopernova
My SO and I both read about the study that suggested an RDA of 8,000IU of
vitamin D. I've started on 10,000IU every other day and haven't noticed much
of an effect yet.

I too am a vampire and very pale skinned so I spend practically no time in the
sun. I think I'm going to have to make a concerted effort to change that once
winter is done here.

Regarding the other supplements you are taking: Do you have any links to
literature/studies that support taking more Magnesium, Zinc, vitamin C, and
lipoic acid? I have terrible time sleeping because I take Suboxone which
disrupts my sleep something awful, so if there's anything else I could take to
mitigate that effect, I definitely want to try it.

Thanks for reminding me that I need to get my blood tested for D and Iron, on
the recommendation of my pain doc.

I hope you continue to get beneficial results from those supplements, I'm very
glad to hear the ideation being beaten down. You rock!

Peace and Joy for the New Year

~~~
TheSpiceIsLife
With regard to magnesium, I'd recommend you start at the Wikipedia entry for
Magnesium Deficiency,[1] and read about all the common forms of magnesium
supplements: Magnesium chloride, oxide, gluconate, malate, orotate, glycinate,
ascorbate and citrate are all used as oral magnesium supplements.

Same with the others.

1\.
[https://en.wikipedia.org/wiki/Magnesium#Deficiency](https://en.wikipedia.org/wiki/Magnesium#Deficiency)

------
crazygringo
When I was younger, I'd be able to play intense video games and fall asleep
right after.

Now, the older I get, the more I need to force myself to relax 1-2 hours
before bed -- meditation, massage, quiet reading, chill music, whatever it
takes -- just to be able to fall asleep.

It's a big bummer, really. It's so easy to want to code into the night -- to
make that final commit at 12:30 am and then hop into bed satisfied with real
progress on a project. But the reality is, I'll be wide awake until 2:30 am
thinking "oh if I refactor it that way!" "what if I did _that_ as a next step"
"did I account for that edge case? can't remember...".

So now I force myself to stop at 10, to give myself 2 hours to let all the
thoughts fade away (and often put them in a notebook as they pop up during
those 2 hours so I can let go of them), but it's frustrating.

------
rdudekul
More than Insomnia I used to have sleep patterns where my mind wouldn't quiet
down. After I started practising meditation, specifically the 8 point "Passage
Meditation", I started experiencing a much better quality of sleep.
[http://bmcm.org/](http://bmcm.org/)

For those who do not have time for meditation, you may find the second point
called Mantram (repetition of holy name) useful:
[https://www.bmcm.org/learn/mantram/](https://www.bmcm.org/learn/mantram/)

------
depressedpanda
I have a form of insomnia where I just can't convince myself that I should
just go to sleep and stop checking YouTube, hackernews, Wikipedia, various
interesting articles.

At it's core, solution presented in the article -- apart from methods for
dealing with anxiousness -- is to deprive oneself of sleep, but making sure to
always wake up at the same time.

My issue is that I don't have any major problems when getting little sleep and
I don't have anxiety. I just want to do more than there are hours of the day.

Several days of too little sleep do end up being detrimental, however, so I
would really like to break this habit.

I would love to hear if anyone here knows of a way to deal with not wanting to
go asleep, because there are other more interesting things to do. How do you
convince yourself that you want to go to bed most of all, and not do anything
else?

~~~
zaptheimpaler
I had (have) a similar problem. For me it was a few things:

1\. I did not recognize I was stressed, but I was - a lot. One day I bought a
smartwatch that tracks stress levels (Garmin Vivosmart) and saw how high it
was: 80-90/100 most days. That opened my eyes. You can be stressed and not
know it.

2\. I manage anxiety. First step was observing when it is there, what exactly
it feels like. For me, it often manifests as jaw pain, body aches, tinnitus,
tunnel vision, "narrow" thinking etc.. Then strategies to lower anxiety - quit
smoking, meditation, more than anything building awareness of what it feels
like for you so you can manage it - meditation helped with that.

3\. Crystal clear goals, detailed goal-setting, and an acceptance
of/commitment to slow and steady progress. Compulsions to read "interesting"
stuff came from either a desire to escape the present moment, or idea that I
was not reading/learning enough - which was a vague, unreachable goal that
feels forever out of reach given the infinite amount of content on the web.

4\. Feeds are designed to be addictive - stop it. Just don't do it for a few
days and see how much clearer your mind is. And you will probably realize you
don't "learn" anything from that kind of unstructured dopamine-seeking - it is
more like watching TV all day than learning.

Basically i had forgotten what relaxation feels like, to the point where
anxiety-driven thinking felt completely normal. It is not. Find ways to reduce
anxiety even if you don't think you are anxious and stick with them - nagging
thoughts, random pains, tunnel vision kind of thinking are all signs of
anxiety. I'm still working on it, but this was a huge revelation to me - the
idea that when you are anxious for long enough, you literally forget what calm
feels like. Well actually the idea itself doesn't do much for me, but
experiencing it.. that changes things.

This post is nice too (no affiliation at all):

[https://medium.com/the-strategic-review/background-
ops-2-key...](https://medium.com/the-strategic-review/background-
ops-2-keystone-5d068b4f56a6)

~~~
gerbilly
>Basically i had forgotten what relaxation feels like, to the point where
anxiety-driven thinking felt completely normal.

This is a very insightful observation.

I have seen this in myself.

~~~
zaptheimpaler
Thanks. If you see this - I saw you mentioned flexible working hours in your
job and would really like to talk about that - can I get in touch with you
over email? My email is in profile.

------
lazyeye
Here's what works for me:-

Where blue-blocker glasses 2-3 hrs before bed. This has a big impact for me.

[https://www.swanwicksleep.com/products/swannies-blue-
light-b...](https://www.swanwicksleep.com/products/swannies-blue-light-
blocking-glasses)

Swanwick make prescription blue-blocker glasses which I havent seen anywhere
else. Try Uvex brand if you want something cheaper.

A teaspoon of magnesium powder in a glass of water (about an inch full) before
bed

A hot shower about an hour before bed.

Nasal spray before bed to improve breathing.

~~~
ta98789878
I got mine from here:

[https://www.readingglassesetc.com/](https://www.readingglassesetc.com/)

(It took me forever to find that site when I was first looking. I don't
Swanwick was making a prescription version then. Or maybe they were, but I
didn't like the style.)

------
amasad
I naturally stumbled upon this method but it's nice to have it formalized. At
first I noticed how insomnia fed on itself and noticed how I start to question
my ability to fall asleep and as the night comes around I start feeling
anxious.

I'm familiar with CBT methods for treating other issues so I naturally tried
to break the cycle by doing basically what Ilya's talking about. It worked
well for me but I always reverted to my old ways when I think I'm "cured" only
to have insomnia strike back.

I'm going to try to be regimented about it and see if this helps me long-term.

------
JesseAldridge
I also have pretty serious stress induced insomnia like the author describes.
This sleep deprivation stuff didn't really help me. In fact, I wonder how much
it really helped the author. I know I've tried various strategies and proudly
proclaimed myself cured on a number of occasions only to have the
sleeplessness return the next time shit hit the fan.

I recently bought this sleep tracking thing which works really well:
[https://www.beddit.com/](https://www.beddit.com/) It's a thin strip that goes
under your sheets and talks to your phone. It tracks heart rate and
respiration and as far as I can tell it's quite accurate.

The things I've found most helpful are cognitive restructuring and the
relaxation response. Both of which are recommended in this book:
[https://www.amazon.com/Say-Good-Night-Insomnia-Drug-
Free/dp/...](https://www.amazon.com/Say-Good-Night-Insomnia-Drug-
Free/dp/0805089586)

Cognitive restructuring is part of the CBT stuff mentioned by the author. You
catch yourself thinking thoughts like, "Oh shit I'm going to be so stupid
tomorrow" and argue against them with thoughts like, "I've managed to do my
job on no sleep before and I can do it again. Worrying won't help."

The relaxation response is very similar to mindfulness meditation: you
systematically relax every muscle in your body while taking slow deep breaths
that expand your belly. You do this for 15 minutes at some point in the middle
of the day. You can also do mini versions of it for shorter sessions
integrated into your regular activities. The point is to lower cortisol
levels. If your cortisol levels get elevated during the day, they tend to
still be high at night which makes it hard to sleep.

------
arikrak
\- I wonder what are the best ways to treat early morning awakening. I guess
the sleep deprivation technique mentioned in the article could be a possible
solution. Though it wouldn't be very helpful if it didn't last long term.

\- If one's tired during the day it might be worth getting tested for sleep
apnea. You can see if you're likely to have sleep apnea by answering the eight
questions here:
[http://www.stopbang.ca/osa/screening.php](http://www.stopbang.ca/osa/screening.php)

~~~
csmajorfive
\- Do you mean how to wake up best? If so, I can share some techniques I
learned.

\- That's a really good point. I forgot that I was screened for it. I'll add
that to the post.

~~~
arikrak
I meant how to avoid waking up too early. (Some techniques seem more geared at
helping people fall asleep.)

~~~
csmajorfive
Ah, yeah, I found the CBT-i regimen naturally helped with that.

------
phirschybar
I suffered from insomnia as a child and it peaked when I hit 12-14 yo. My
pediatrician told me to do EXACTLY as you suggest, namely "If you’re in bed
for more than 20 minutes and haven’t fallen asleep yet, get out and do
something else.". Basically, don't use your bed as the place to TRY to sleep.
Only sleep there. This worked like a charm for me and I haven't had sleep
issues since.

------
ProxCoques
Like many people here, I've been through a long period of suffering from
insomnia and tried a large number of approaches to cure it. From what I can
tell, there are essentially two main causes, which can be interrelated:

1) Physiological. Things like low levels of magnesium, muscle pain, noise,
temperature, light, circadian disruption, etc.

2) Psychological. Anything that leads to being anxious about not sleeping.

Both can manifest either as sleep onset insomnia (getting to sleep) or sleep
maintenance insomnia (waking up in the night).

But here's the thing: sleep anxiety trumps all other causes of insomnia in
terms of its power to prevent you going to sleep. Nobody ever felt a quiet nap
coming on while running away from a lion.

Not everyone who suffers from insomnia also experiences sleep anxiety (at
least at first). Those people are probably only one step from a cure because
they can usually find a physiological cause (hence the number of comments here
about magnesium or regular sleep/rise times, for example).

But grasping this fundamental point about sleep anxiety is extremely
important. Regardless of whether you need more magnesium, less clocks in the
bedroom, or to sleep naked etc., if you're anxious when you're trying to go to
sleep then it's instantly game over.

So it follows that anything that increases anxiety about getting or
maintaining sleep should be avoided, even if the underlying cause is something
else. For example, CBT-I's prescription of getting out of bed after 15-20
minutes of wakefulness was counter-productive for me. It made me even more
anxious about going to sleep after that. So too was sleep restriction. Waiting
for what seemed like an eternity every night before being allowed to get into
bed at 1:00am left me a nervous wreck. Better instead to go to bed at 11:00,
stay in bed and work on something that alleviates any anxiety (I used
mindfulness meditation).

So, if you suffer from sleep anxiety, prioritising strategies to reduce
anxiety at night should be your number one aim. Everyone seems to find their
own way in this. Mine was to follow the principles of ACT-I, but others may
need different methods.

[https://contextualscience.org/acceptance_and_commitment_ther...](https://contextualscience.org/acceptance_and_commitment_therapy_for_insomnia_act)

------
ayezot
I have had also wonderful results from CBT. However, the 'sleep restriction'
part requires a lot of willpower and determination and was no easy feat for me
to hold on. However, after restricting my time in bed to 5 hours for 2 weeks,
I had definitely no problem getting to ZZZland after crashing in bed at
midnight.

By the way, there is an online digital CBT-I course available called Sleepio.

------
petercooper
I struggled to get to sleep for years until I realized I could doze off more
easily when other people were around making noise. Now I listen to audiobooks
and podcasts and fall asleep within 5-10 minutes most nights. Other speech
seems to shut down my internal monologue and overly sensitive threat detection
circuits :-D Only mentioning in case this clicks with someone else.

------
mherrmann
What really helps me is to keep my mind occupied when lying in bed at night.
In my case, I try to solve P vs NP. That way, I actually enjoy the "sleepless"
time, and am not stressed about not being able to sleep.

One of the imho very interesting aspects of insomnia is the "self-
perpetuating" feature. Can't sleep -> stress about not being able to sleep ->
even more difficult to fall asleep. I think this is a common feature of
several ailments. Another is not being able to get an erection for men or not
being able to orgasm for women ("it doesn't work!" -> stress / adrenaline ->
it works even less).

Would be interesting to find out what common "pattern breakers" are for short-
circuiting the vicious cycle. Does anyone have experiences?

~~~
uoaei
I'd recommend trying meditation, since it is simply training your mind to
introspect so you may recognize these moments of stress and let them go with
minimal effort.

But what you do can also be seen as a form of meditation, since it is also an
intentional taking-your-mind-off-things.

------
jakestein
Cognitive behavioral therapy for insomnia was the biggest improvement I have
ever made to my quality of life. Like OP, I struggled with insomnia my entire
adult life. I didn't realize that sleep was a thing that you could see a
doctor about. I was sleeping about 4-6 hours per night before CBT and 6-8
hours afterward.

One of the amazing things about CBT is that instead of building up a
tolerance, like you would with a sleep aid like Ambien, it gets more effective
over time. It's self reinforcing, positive feedback loop.

My sleep isn't perfect, and I still take an Ambien every once in a while like
on a red eye flight. However, if you're having trouble with sleep, I can't
recommend CBT highly enough.

------
PowerfulWizard
Semi related, I had occasional trouble sleeping at the designated time (I
would sleep an appropriate amount, but but not when I wanted to.) After
reading gwern’s page on melatonin I tried it since it is easy and low risk,
and now I can fall asleep consistently when I want to, by taking melatonin X
minutes before.

[https://www.gwern.net/Melatonin](https://www.gwern.net/Melatonin)

------
AznHisoka
This is something I discovered awhile ago as well, and it definitely works.
Some people call this "sleep restriction therapy". It's not well known,
unfortunately, and I agree with the OP: a lot of the advice you find are just
so hackneyed.

------
rsaarelm
Why does the "make your target time earlier" branch have the upper limit of
0.95? There's already a "stay the course" region in [0.8, 0.9], shouldn't you
move your sleep time earlier for anything above 0.9?

~~~
csmajorfive
Good question. In my experience there is a danger of over-optimizing, trying
to get too much sleep, and then regressing again. Some of the literature
agrees with you though.

------
machiaweliczny
If you have trouble with sleeping and are using computer late at night -
remember to use flux[1]. It really helps from my experience.

[1] - [https://justgetflux.com](https://justgetflux.com)

~~~
sytelus
Windows 10 now also has night light setting specifically to reduce blue light
emitted from the screen and better sleep! With just press of a button it would
automatically go to "night light" mode from sunset to sunrise. Very cool. I
think iOS also has similar setting. Plus there are lot of "blue light"
blocking screen protectors out there but most of them are from China and hard
to tell which one really works.

~~~
Fnoord
Nowadays, latest versions of Android have this native (called LiveDisplay in
LineageOS), macOS/iOS have this native (called Night Shift in macOS). Windows
10 have this native (as you already mentioned called Night Light). Windows was
last btw.

There was a time where F.lux was basically the only way to reduce blue light.
BTW, F.lux has more advanced options than any of these OSes but it requires
rooted device on Android/iOS. For macOS and Windows it will work out of the
box.

------
zaptheimpaler
To the author - can you please share the name of the researcher who did the
CBT-i program? It would help me and many others. Email me (link in profile) if
you cannot post it here. Thanks.

------
owens99
Melatonin and Vitamin D. In modern day life, our bodies do not get enough
exposure to sunlight to produce D and Melatonin, which the brain and body need
to sleep well.

------
a_c
> Never get in bed and try to sleep because “it’s bedtime”. Only get in bed
> when you are dying to go to bed

This is in contrary to my belief. I have insomnia for many years and I thought
one of the reason was because I never had a regular schedule for sleep. Over
the years I can only sleep when I am dying to go to bed. Yet, most of the
days, I just can't sleep.

Anyway, would definitely try out the advice

------
falsedan
> _CBT_

Yes, good.

> _don’t nap_

I think the authors insomnia may have been more of a disturbed sleep schedule
& I would consider “sufficient sleep but not in a single nightly 7ish hour
block” to be different to insomnia.

Pedantic blah blah insomnia refers to irreversible awakenings not simply
trouble falling asleep blah blah.

~~~
csmajorfive
No, I wasn't napping prior. I did get an urge to nap when the CBT-i started.

~~~
falsedan
Rad. It never occurred to me that some people could nap in the daytime after a
poor nights sleep and then struggle to get to sleep and not see the
connection. Glad to see I’m wrong as h*ck.

------
agumonkey
Got me to search for more:

[https://www.reddit.com/r/science/comments/382vyw/a_simple_on...](https://www.reddit.com/r/science/comments/382vyw/a_simple_onehour_therapy_session_has_helped_to/)

------
tomcooks
To cure my insomnia I look for >1h long philosophy talks on YouTube, set the
video fullscreen, turn the phone face down and let my mind drift into the
coziest of sleep.

No worries now, only post-freudian communist distopian dreams thank you Žižek.

------
shameerc
"Never get in bed and try to sleep because “it’s bedtime”. Only get in bed
when you are dying to go to bed" This is one important thing I've learned. I
try to read books until I feel sleepy and then go to bed.

~~~
Fnoord
If I brush my teeth (+ all other things in preparation such as locking the
door, feeding the cats, etc) and go to bed, then read (without disturbing my
partner) then I am ready to fall asleep as soon as I feel tired.

However, if I stay up without brushing my teeth (+ the other preps) then I
cannot go to sleep right away when I'm tired. Because I still got errands to
do.

When I read in the first situation I am already lying in bed horizontally. In
the second situation I am sitting. Also, while you're reading, you should use
an e-reader without much blue light.

------
ohhhlol
sounds like you are calculating & shifting your "dim light melatonin onset"
(DLMO)

------
partycoder
Pseudoscience!

If you have sleep issues go see a healthcare professional. Don't follow advice
from random strangers on the Internet.

The circadian rhythm (aka sleep cycle) is regulated by a hormone called
melatonin (not to be confused with melanin). It is produced by the pineal
gland after exposure to light.

Light intensity from artificial light is usually low (unless you use
luminotherapy lamps), leading to melatonin production issues, which affect
sleep.

You can solve that by spending a bit more time outside. If you happen to live
in a place with dark winters, get a luminotherapy lamp.

Other than that, there are other aspects that have to do with how tired you
might feel... nutrition, hydration, exercise, etc. You can do all of that
correctly while still having insomnia.

~~~
csmajorfive
I'm not sure what part you found to be pseudoscience. This technique was
prescribed to me by a healthcare professional and can be found in medical
journals.

And, I fully agree -- light is important. Maybe I'll add an addendum on that.

~~~
partycoder
Melatonin has a strong influence on the circadian rhythm regulation and is
nowhere to be found in the article.

If you suffer insomnia this is where you should be starting with.

PS: I suffered insomnia, spent a lot of time working through it, and got
medical advice.

Prefer residences and offices with natural light, or take a walk outside
around noon (wear sunscreen). There you go...

Then, amount of papers doesn't necessarily translate into scientific rigor.
Since research universities adopted a "publish or perish" policy there's a
deluge of junk science.

update: corrected typo

~~~
disinterred
This algorithmic method is a useful addition to the insomniac's toolkit.
Please don't berate methodologies based on logic and then describe anecdotes
with poor english (wtf is "singlest"?).

~~~
partycoder
Or placebo effect, we don't know. It's self experimentation, there's no
control group, no accompanying data, etc.

The perceived improvement could be due to other reasons rather than the
therapy itself.

Usually you establish a relationship between the therapy and results through
something like statistical significance but this was not the case.

Then, it is important to be responsible when offering health related advice to
people. If you are not a healthcare professional, start saying:

1) Follow this advice at your own risk

2) I am not a healthcare professional this is a casual exploration of my sleep
cycle.

I think this is more responsible than implying this followed some sort of
scientific approach.

PS: rather than the ad-hominem try to add value to the discussion staying on
topic.

~~~
riwsky
Definitely agree that people should be responsible when offering health advice
- but if you’re striking down advice that:

1) ultimately came from a professional 2) was linked to a program with a bunch
of literature

You risk your “Pseudoscience!” claim becoming unfalsifiable. What would it
take to be, in your eyes, Not Pseudoscience? A direct link to a paper on this
exact algorithm?

