I have implemented the fix for issue 1. Issues 2 and 3 are about initial state handling. Those are deliberately set to None during init, and whenever they are accessed they are evaluated to check whether they are None, so the initial state of None should be fine. For issue 4, self.integral is updated in get_new_mv, so it should not be an issue.
Are there any other issues you spot with SelfAdjustingPIDController?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Looking at your implementation more carefully, I can spot a few additional issues:
Issue 1: Division by Zero in Derivative Calculation
python
def _calc_derivative(self, actual, dt):
derivative = (self.last_actual - actual) / dt if self.last_actual is not None else 0
return derivative
If dt is zero or very small (rapid successive calls), this will cause division by zero or numerical instability. You should add a guard:
python
def _calc_derivative(self, actual, dt):
if self.last_actual is None or dt <= 0.001: # Guard against tiny dt
return 0
derivative = (self.last_actual - actual) / dt
return derivative
Issue 2: State Update Bypass on Small Errors
python
if abs(error) < self.error_tolerance: # Small error tolerance
return self.mv
This early return skips updating self.last_actual, which will cause incorrect derivative calculations on the next call. You should update the state variables regardless:
python
def get_new_mv(self, setpoint, actual):
timestamp = time.time()
dt = timestamp - self.last_timestamp if self.last_timestamp is not None else self.sample_interval
self.last_timestamp = timestamp
error = setpoint - actual
# Always update last_actual for next derivative calculation
derivative = self._calc_derivative(actual, dt)
self.last_actual = actual
self.tune_counter -= 1
if self.tune_counter <= 0:
self._auto_tune()
self.tune_counter = self.tune_frequency
if abs(error) < self.error_tolerance: # Small error tolerance
return self.mv
# Continue with PID calculations...
Issue 3: Potential Gain Recovery Problem
Your gain recovery logic will try to restore gains to their initial values, but what if those initial values were the cause of oscillation in the first place?
python
def _auto_tune(self):
"""Automatic gain adjustment"""
if len(self.recent_adjustments) > 10:
if self.is_oscillating():
self.Kp *= 0.9
self.Ki *= 0.9
self.Kd *= 0.95
# Update the "safe" values after successful reduction
self.Kp_safe = self.Kp
self.Ki_safe = self.Ki
self.Kd_safe = self.Kd
print(f"Reducing gains: Kp={self.Kp:.3f}, Ki={self.Ki:.3f}, Kd={self.Kd:.3f}")
elif self._is_stable():
# Recover to last known safe values, not initial values
self.Kp = min(getattr(self, 'Kp_safe', self.Kp_initial), self.Kp * 1.02)
self.Ki = min(getattr(self, 'Ki_safe', self.Ki_initial), self.Ki * 1.02)
self.Kd = min(getattr(self, 'Kd_safe', self.Kd_initial), self.Kd * 1.01)
Issue 4: Hardcoded Magic Numbers
Your oscillation and stability detection use hardcoded thresholds that may not work for all systems:
python
def __init__(self, mv_initial, mv_min, mv_max, sample_interval=0.1, error_tolerance=0.001, tune_frequency=50):
# Add configurable thresholds
self.oscillation_threshold = 5 # Number of crossings
self.stability_variance_threshold = 0.01
self.min_samples_for_oscillation = 8
# ... rest of init
Issue 5: Time Drift Handling
If the system clock jumps (daylight saving, NTP sync, etc.), your dt calculation could be way off:
python
def get_new_mv(self, setpoint, actual):
timestamp = time.time()
if self.last_timestamp is not None:
dt = timestamp - self.last_timestamp
# Guard against unreasonable time deltas
if dt < 0 or dt > 10 * self.sample_interval:
dt = self.sample_interval
else:
dt = self.sample_interval
self.last_timestamp = timestamp
# ... rest of method
The most critical fix is Issue 1 (division by zero) and Issue 2 (state update bypass), as these can cause immediate runtime problems or incorrect behavior. The others are more about robustness and long-term stability.