示例#1
0
def generate_minimax_square(N = 10, seed = 0):
	domain = psdr.BoxDomain(0*np.zeros(2), np.ones(2))

	# Generate a design
	X = psdr.minimax_lloyd(domain, N, maxiter = 500, xtol = 1e-7, verbose = True)

	#X = domain.sample(N)

	# Compute the disc diameter to cover the domain
	V = psdr.voronoi_vertex(domain, X)
	D = psdr.cdist(X, V)
	radius = np.max(np.min(D, axis= 0))

	# save the file
	design = {
		'author': 'Jeffrey M. Hokanson',
		'objective': 'minimax',
		'metric': 'l2',
		'domain': 'square',
		'radius': radius,
		'X': X.tolist()
	}

	with open('square_%04d.dat' % N, 'w') as f:
		json.dump(design, f)	
def generate_minimax_square(N, seed):
    np.random.seed(seed)
    domain = psdr.BoxDomain(0 * np.zeros(2), np.ones(2))

    # Generate a design
    try:
        X = psdr.minimax_lloyd(domain,
                               N,
                               maxiter=500,
                               xtol=1e-9,
                               verbose=False)

        # Compute the disc diameter to cover the domain
        V = psdr.voronoi_vertex(domain, X)
        D = psdr.cdist(X, V)
        radius = np.max(np.min(D, axis=0))

        # save the file
        design = {
            'author': 'Jeffrey M. Hokanson',
            'notes':
            f'psdr.minimax_lloyd seed={seed}, maxiter=500, xtol = 1e-9',
            'objective': 'minimax',
            'metric': 'l2',
            'domain': 'square',
            'radius': radius,
            'X': X.tolist()
        }
    except:
        design = {'radius': np.inf}

    print(f"M: {N:4d} \t seed {seed:4d} finished")
    return design
def random_maximin_minimax_design(seed):
    Xhat = random_maximin_design(seed)
    Xhat = psdr.minimax_lloyd(domain,
                              M,
                              L=L,
                              maxiter=100,
                              full=True,
                              Xhat=Xhat,
                              verbose=True)
    return Xhat
def coffeehouse_minimax_design(seed):
    Xhat = coffeehouse_design(seed)
    Xhat = psdr.minimax_lloyd(domain,
                              M,
                              L=L,
                              maxiter=100,
                              full=True,
                              Xhat=Xhat,
                              verbose=True)
    return Xhat
示例#5
0
def generate_X(fun, M):
    np.random.seed(0)
    X = fun.domain.sample(1000)
    grads = fun.grad(X)
    lip = psdr.LipschitzMatrix()
    lip.fit(grads=grads)
    L = lip.L

    X = psdr.minimax_lloyd(fun.domain, M, L=L, verbose=True)
    return X
示例#6
0
def test_minimax_lloyd(m = 2, M = 7, plot = False):
	domain = psdr.BoxDomain(-np.ones(m), np.ones(m))
	Xhat = psdr.minimax_lloyd(domain, M, maxiter =  100)

	if plot:
		import matplotlib.pyplot as plt
		fig, ax = plt.subplots()
		ax.plot(Xhat[:,0], Xhat[:,1],'k.')
		ax.set_xlim(-1,1)
		ax.set_ylim(-1,1)
		ax.axis('equal')
	
		plt.show()	
示例#7
0
def test_minimax_lloyd_global_min(plot = False):
	# Here check if the algorithm converges close to the global minimizer
	# for a known case.
	# See JMY90, M = 7 case

	np.random.seed(2)

	m = 2
	M = 7
	domain = psdr.BoxDomain(0*np.ones(m), np.ones(m))
	Xhat_true = np.array([
			[0.5, 0.5], 
			[0.5, 0], [0.5,1],
			[1/3 - np.sqrt(7)/12, 3/4],
			[1/3 - np.sqrt(7)/12, 1/4],
			[2/3 + np.sqrt(7)/12, 1/4],
			[2/3 + np.sqrt(7)/12, 3/4],
			])
	Xhat = psdr.minimax_lloyd(domain, M, maxiter =  100, Xhat = None, xtol = 1e-9)

	Xhat_best = None
	best_score = np.inf
	
	for perm in permutations(range(len(Xhat))):
		perm = np.array(perm, dtype = np.int)
		R, scale = orthogonal_procrustes(Xhat, Xhat_true[perm])
		err = np.linalg.norm(Xhat @ R - Xhat_true[perm], 'fro')
		if err < best_score:
			best_score = err
			Xhat_best = Xhat @ R
		
	# TODO: need to align points out of Xhat

	print("Error in fit", best_score)

	if plot:
		import matplotlib.pyplot as plt
		fig, ax = plt.subplots()
		ax.plot(Xhat_best[:,0], Xhat_best[:,1],'rx')
		#XhatR = Xhat @ R
		#ax.plot(XhatR[:,0], XhatR[:,1],'ro')
		ax.plot(Xhat_true[:,0], Xhat_true[:,1],'k.')
		ax.set_xlim(-1,1)
		ax.set_ylim(-1,1)
		ax.axis('equal')
	
		plt.show()	
示例#8
0
import numpy as np
import psdr
from psdr.pgf import PGF

np.random.seed(0)

m = 2
domain = psdr.BoxDomain(-np.ones(m), np.ones(m))
L = np.array([[1, 0],[-1,4]])/4

print("without Lipschitz")
X = psdr.minimax_lloyd(domain, 9, maxiter = 500)
print(X)
d = psdr.geometry.fill_distance(domain, X)
print(d) 

pgf = PGF()
pgf.add('x', X[:,0])
pgf.add('y', X[:,1])
pgf.write('data/fig_cover_scalar.dat')


print("with Lipschitz")
X = psdr.minimax_lloyd(domain, 3, L = L)
print(X)
d = psdr.geometry.fill_distance(domain, X, L = L)
print(d) 

pgf = PGF()
pgf.add('x', X[:,0])
pgf.add('y', X[:,1])
示例#9
0
def build_minimax_design(M, seed):
    np.random.seed(seed)
    return psdr.minimax_lloyd(domain, M, L=L)
示例#10
0
gradX = fun.grad(X)

# Grid-based sampling
Xg = fun.domain.sample_grid(8)
fXg = fun(Xg)

lip_mat = psdr.LipschitzMatrix()
lip_con = psdr.LipschitzConstant()

lip_mat.fit(grads=gradX)
lip_con.fit(grads=gradX)

# construct designs
M = 100
ngrid = 100
X_iso = psdr.minimax_lloyd(fun.domain, M)
X_lip = psdr.minimax_lloyd(fun.domain, M, L=lip_mat.L)

# Fix ridge
U = lip_mat.U[:, 0].copy()

# Generate envelope of data
lip_con.shadow_envelope(Xg,
                        fXg,
                        ax=None,
                        pgfname='data/fig_shadow_envelope.dat',
                        U=U)

# isotropic / scalar
lip_con.shadow_plot(X_iso,
                    fun(X_iso),
示例#11
0
import psdr, psdr.demos
from psdr.pgf import PGF
import seaborn as sns
import matplotlib.pyplot as plt

funs = [
    psdr.demos.OTLCircuit(),
    psdr.demos.Borehole(),
    psdr.demos.WingWeight()
]
names = ['OTLCircuit', 'Borehole', 'WingWeight']

algs = [
    lambda dom, M, L: psdr.random_sample(dom, M),
    lambda dom, M, L: psdr.latin_hypercube_maximin(dom, M, maxiter=1),
    lambda dom, M, L: psdr.minimax_lloyd(dom, M, L=L)
]
alg_names = ['random', 'LHS', 'minimax']

# Number of repetitions
Ms = [100, 100, 10]

Nsamp = 20

for fun, name in zip(funs, names):
    # Estimate the Lipschitz matrix
    np.random.seed(0)
    X = np.vstack([fun.domain.sample(1000), fun.domain.sample_grid(2)])
    grads = fun.grad(X)

    lip = psdr.LipschitzMatrix(verbose=True,