Two-stage Method

Two-stage approach trains a regression model \(\hat{c} = g(\mathbf{x}; \mathbf{\theta})\) by minimizing a prediction error \(l(\hat{\mathbf{c}}, \mathbf{c})\) such as mean square error \(l_{MSE}(\hat{\mathbf{c}}, \mathbf{c}) = \frac{1}{n} \sum_i^n \| \hat{\mathbf{c}}_i - \mathbf{c}_i \| ^ 2\). Then in an inference process, the machine learning model predicts \(\hat{c} = g(\mathbf{x}; \mathbf{\theta})\) first. After that, the predicted value \(\hat{c}\) is used for solving the optimization problem.

pyepo.twostage.sklearnPred(pmodel)

Two-stage prediction and optimization with scikit-learn.

Parameters:

pmodel (Regressor) – scikit-learn regression model

Returns:

scikit-learn multi-output regression model

Return type:

MultiOutputRegressor

pyepo.twostage.sklearnPred is a helper function to build multi-output regressor with scikit-learn.

import pyepo

# model for shortest path
grid = (5,5) # grid size
model = pyepo.model.grb.shortestPathModel(grid)

# generate data
num_data = 1000 # number of data
num_feat = 5 # size of feature
deg = 4 # polynomial degree
noise_width = 0 # noise width
x, c = pyepo.data.shortestpath.genData(num_data, num_feat, grid, deg, noise_width, seed=135)

# sklearn regressor
from sklearn.linear_model import LinearRegression
reg = LinearRegression() # linear regression

# build model
twostage_model = pyepo.twostage.sklearnPred(reg)

# training
twostage_model.fit(x, c)

# prediction
c_pred = twostage_model.predict(x)