coin-or / rbfopt

RBFOpt library for black-box optimization
Other
188 stars 33 forks source link

Example to visualize the interpolant #15

Closed martinkielhorneffect closed 6 years ago

martinkielhorneffect commented 6 years ago

I would like to visualize the surrogate model. But I can't figure out how to call evaluate_rbf or bulk_evaluate_rbf.

Is it possible to extend the following code example to plot the surrogate model after alg.optimize has finished?

import rbfopt
import numpy as np
import pandas as pd
import xarray.plot as xrp
import matplotlib.pyplot as plt
plt.ion()
import subprocess
import re

def fun(x):
    a = x[0]
    b = x[1]
    return np.sin(a) + np.cos(b)

t_clad = eval('np.linspace({})'.format('.15,.4,7'))
n_core = eval('np.linspace({})'.format('3.3,3.5,9'))

xas = []
for tc in t_clad:
    for nq in n_core:
        xas.append(dict(tc=tc, nq=nq, f=fun([tc, nq])))
df = pd.DataFrame(xas)
df2 = df.set_index(['tc', 'nq'])
ds = df2.to_xarray()
xrp.plot(ds.f)
xrp.contour(ds.f, colors='k')
plt.title('original merrit function')

bb = rbfopt.RbfoptUserBlackBox(2,
                               np.array([.15, 3.3]),
                               np.array([.4, 3.5]),
                               np.array(['R', 'R']), fun)

settings = rbfopt.RbfoptSettings(max_evaluations=50)
alg = rbfopt.RbfoptAlgorithm(settings, bb)
val, x, itercount, evalcount, fast_evalcount = alg.optimize()

import rbfopt.rbfopt_utils

rbfopt.rbfopt_utils.evaluate_rbf(settings, np.array([.14,3.3]), # what arguments to put here?

2018-08-27-141036_1020x522_scrot

martinkielhorneffect commented 6 years ago
import rbfopt.rbfopt_utils
n = 2
k = len(alg.all_node_val)
# equation 3 in paper http://www.optimization-online.org/DB_FILE/2014/09/4538.pdf
# settings.rbs = 'auto' but that isn't understood
settings.rbf = alg.best_global_rbf[0]
Amat = rbfopt.rbfopt_utils.get_rbf_matrix(settings, n, k, alg.all_node_pos)
# rbfopt_utils.get_rbf_coefficients(settings, n, k, Amat, node_val)
lambda_coef, h_coef = rbfopt.rbfopt_utils.get_rbf_coefficients(
    settings, n, k, Amat, alg.all_node_val)
# rbfopt_utils.evaluate_rbf(settings, point, n, k, node_pos, rbf_lambda, rbf_h)
rbfopt.rbfopt_utils.evaluate_rbf(settings, np.array(
    [.14, 3.3]), n, k, alg.all_node_pos, lambda_coef, h_coef)
martinkielhorneffect commented 6 years ago

I figured it out:

2018-08-27-160843_1127x579_scrot

import rbfopt
import rbfopt.rbfopt_utils
import numpy as np
import pandas as pd
import xarray.plot as xrp
import matplotlib.pyplot as plt
plt.ion()
import subprocess
import re

def fun(x):
    a = x[0]
    b = x[1]
    return np.sin(a) + np.cos(b)

t_clad = eval('np.linspace({})'.format('.15,.4,7'))
n_core = eval('np.linspace({})'.format('3.3,3.5,9'))

xas = []
for tc in t_clad:
    for nq in n_core:
        xas.append(dict(tc=tc, nq=nq, f=fun([tc, nq])))
df = pd.DataFrame(xas)
df2 = df.set_index(['tc', 'nq'])
ds = df2.to_xarray()
pl = (1, 2)
ax = plt.subplot2grid(pl, (0, 0))
xrp.plot(ds.f)
levels = np.linspace(-.8, -.6, 11)
cs = xrp.contour(ds.f, colors='k', levels=levels)
plt.clabel(cs, inline=True)
plt.title('original merrit function')

bb = rbfopt.RbfoptUserBlackBox(2,
                               np.array([.15, 3.3]),
                               np.array([.4, 3.5]),
                               np.array(['R', 'R']), fun)

settings = rbfopt.RbfoptSettings(max_evaluations=50)
alg = rbfopt.RbfoptAlgorithm(settings, bb)
val, x, itercount, evalcount, fast_evalcount = alg.optimize()

n = 2
k = len(alg.all_node_val)
# matrix equation 3 in paper http://www.optimization-online.org/DB_FILE/2014/09/4538.pdf ?
# settings.rbs = 'auto' but that isn't understood
settings.rbf = alg.best_global_rbf[0]
Amat = rbfopt.rbfopt_utils.get_rbf_matrix(settings, n, k, alg.all_node_pos)
# rbfopt_utils.get_rbf_coefficients(settings, n, k, Amat, node_val)
lambda_coef, h_coef = rbfopt.rbfopt_utils.get_rbf_coefficients(
    settings, n, k, Amat, alg.all_node_val)
# rbfopt_utils.evaluate_rbf(settings, point, n, k, node_pos, rbf_lambda, rbf_h)
val = rbfopt.rbfopt_utils.evaluate_rbf(settings, np.array(
    [.14, 3.3]), n, k, alg.all_node_pos, lambda_coef, h_coef)

b, a = np.meshgrid(n_core, t_clad)

a.shape
points = np.dstack((a.reshape(a.shape[0] * a.shape[1]),
                    b.reshape(a.shape[0] * a.shape[1])))[0]

vals = rbfopt.rbfopt_utils.bulk_evaluate_rbf(
    settings, points, n, k, alg.all_node_pos, lambda_coef, h_coef).reshape(a.shape)

ax = plt.subplot2grid(pl, (0, 1))
cs = plt.contour(b, a, vals, levels=levels)

cs0 = xrp.contour(ds.f, colors='k', levels=levels)
plt.clabel(cs, inline=True)
plt.clabel(cs0, inline=True)
plt.scatter(alg.all_node_pos[:, 1], alg.all_node_pos[:, 0])
plt.title('black original, color: surrogate model')