Too Hot, Too Humid, Too Many People

Arpit Bhayani

curious, tinkerer, and explorer


Question

You’re working on an smart home temperature control system. The system needs to optimize the comfort. Each room has sensors that measure temperature (T), humidity (H), and occupancy (O). The comfort score (C) of a room is given by the function

C(T, H, O) = 72 - (T - 70)² - 2(H - 40)² + 5O

where T is temperature in Fahrenheit, H is humidity percentage, and O is number of occupants.

Given the current readings of the room as a tuple (T, H, O), write Python code to determine which variable should be adjusted first to maximize comfort improvement and whether it should be increased or decreased.

Use the problem statement to dig deeper into the mathematical concept involved and build a practical and deeper understanding. You will also find one 3 Blue 1 Brown video on this topic, must watch for an intuitive understanding.

Inputs:

74, 45, 2
0, 50, 2
500, 50, 2

Output:

humidity, decrease
temperature, increase
temperature, decrease

Solution

Here’s the code for reference and some notes on the solution below.

The partial derivatives are:

  • dC/dT = -2(T - 70) [for temperature]
  • dC/dH = -4(H - 40) [for humidity]
  • dC/dO = 5 [for occupancy]

For your current readings say (T=74°F, H=45%, O=2), let’s run the analysis:

The partial derivatives at the current point are:

  • Temperature: -8 (negative means comfort decreases as temperature increases)
  • Humidity: -20 (negative means comfort decreases as humidity increases)
  • Occupancy: +5 (positive constant as it’s always beneficial)

Looking at the absolute magnitudes of impact:

  • Temperature: 8 units of comfort/°F
  • Humidity: 20 units of comfort/%
  • Occupancy: 5 units of comfort/person

This implies - Humidity should be prioritized first because:

  • It has the largest magnitude of impact (20)
  • The negative sign indicates you should decrease the humidity
  • The current humidity (45%) is above the optimal point (40%)

So, to maximize comfort improvement:

  1. First, reduce the humidity (currently at 45%) toward the optimal 40%
  2. Then adjust the temperature (currently at 74°F) toward the optimal 70°F
Arpit Bhayani

Creator of DiceDB, ex-Google Dataproc, ex-Amazon Fast Data, ex-Director of Engg. SRE and Data Engineering at Unacademy. I spark engineering curiosity through my no-fluff engineering videos on YouTube and my courses


Arpit's Newsletter read by 125,000 engineers

Weekly essays on real-world system design, distributed systems, or a deep dive into some super-clever algorithm.