This is a numerical optimization template that can be used in experiments for a general Chinese medicine course. It utilizes principles similar to Bayesian Optimization and employs the Multilayer perceptron for machine learning.
All rights are reserved by my high school classmate and former college roommate, Colin.
所有權由我的高中同學兼大學前室友 Colin 所保留
1. Adjust the range of three parameters. (in experiment.py)
The default range is real numbers.
In this case [-10.0,
class Params:
PARAM1_RANGE = (-10, 10)
PARAM2_RANGE = (-10, 10)
PARAM3_RANGE = (-10, 10)
def __init__(self, param1, param2, param3):
if not (Params.PARAM1_RANGE[0] <= param1 <= Params.PARAM1_RANGE[1]):
raise ValueError(f"param1 must be in the range {Params.PARAM1_RANGE}")
self.param1 = param1
if not (Params.PARAM2_RANGE[0] <= param2 <= Params.PARAM2_RANGE[1]):
raise ValueError(f"param2 must be in the range {Params.PARAM2_RANGE}")
self.param2 = param2
if not (Params.PARAM3_RANGE[0] <= param3 <= Params.PARAM3_RANGE[1]):
raise ValueError(f"param3 must be in the range {Params.PARAM3_RANGE}")
self.param3 = param3
Based on the experimental results generated by the three parameters, you need to give a score to these results. In this context, it could be the time it takes for the temperature to stop oscillating, the extent to which the temperature exceeds the upper limit, and so on...
You can convert the experimental MATLAB code to Python or use libraries to integrate the results.
class Experiment:
def __init__(self, params: Params):
self.params = params
def run(self):
# Selfdefined function
# TODO: Implement the function
x1 = self.params.param1
x2 = self.params.param2
x3 = self.params.param3
return -(x1**2 + x2**2 + x3**2) + 10 # Example: maximized when x1=x2=x3=0
Install the required packages.
pip install -r requirements.txt
Execute main.py.
python main.py
-
Define the Problem and Blackbox Function: You need a clear definition of your three parameters and the blackbox function that evaluates them.
-
Generate Initial Data: Create an initial set of data points by sampling the parameter space.
-
Model the Evaluation Function: Use a machine learning model to approximate the blackbox function based on your initial data. A simple choice can be a linear regression or a neural network.
-
Optimization Algorithm: Use an optimization algorithm to find the parameters that maximize the evaluation function. Bayesian Optimization is a good choice for blackbox optimization problems.
-
Iterative Improvement: Iteratively update your model with new data points obtained by evaluating the blackbox function at the predicted optimal parameters.