mirror of
https://github.com/Klipper3d/klipper.git
synced 2025-07-07 23:17:37 -06:00
mathutil: Move coordinate_descent() to new file
Add a new python file (mathutil.py) and move the coordinate_descent() code to it. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
7290ed5f73
commit
fa07be9346
4 changed files with 45 additions and 39 deletions
40
klippy/mathutil.py
Normal file
40
klippy/mathutil.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
# Simple math helper functions
|
||||
#
|
||||
# Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
|
||||
#
|
||||
# This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
import logging
|
||||
|
||||
# Helper code that implements coordinate descent
|
||||
def coordinate_descent(adj_params, params, error_func):
|
||||
# Define potential changes
|
||||
params = dict(params)
|
||||
dp = {param_name: 1. for param_name in adj_params}
|
||||
# Calculate the error
|
||||
best_err = error_func(params)
|
||||
|
||||
threshold = 0.00001
|
||||
rounds = 0
|
||||
|
||||
while sum(dp.values()) > threshold and rounds < 10000:
|
||||
rounds += 1
|
||||
for param_name in adj_params:
|
||||
orig = params[param_name]
|
||||
params[param_name] = orig + dp[param_name]
|
||||
err = error_func(params)
|
||||
if err < best_err:
|
||||
# There was some improvement
|
||||
best_err = err
|
||||
dp[param_name] *= 1.1
|
||||
continue
|
||||
params[param_name] = orig - dp[param_name]
|
||||
err = error_func(params)
|
||||
if err < best_err:
|
||||
# There was some improvement
|
||||
best_err = err
|
||||
dp[param_name] *= 1.1
|
||||
continue
|
||||
params[param_name] = orig
|
||||
dp[param_name] *= 0.9
|
||||
logging.info("Coordinate descent best_err: %s rounds: %d", best_err, rounds)
|
||||
return params
|
Loading…
Add table
Add a link
Reference in a new issue