Skip to content

Commit

Permalink
sdfgsdfgsdfgs
Browse files Browse the repository at this point in the history
  • Loading branch information
ianran committed Oct 9, 2024
1 parent 688cd2e commit 270fe5f
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 3 deletions.
8 changes: 7 additions & 1 deletion examples/other/plot_fake_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
import argparse
import lop

import pdb

def main():
parser = argparse.ArgumentParser(description='Fake function plotter')
parser.add_argument('-f', type=str, default='logistic', help='Enter the type of function [linear squared logistic sin_exp max min squared_min_max]')
parser.add_argument('-f', type=str, default='logistic', help='Enter the type of function [linear squared logistic sin_exp max min squared_min_max mix_gauss int_gauss]')
parser.add_argument('-d', type=int, default=1, help='Enter the dimmensionality of the fake function (1 or 2) for plotting')
args = parser.parse_args()

Expand All @@ -51,6 +52,11 @@ def main():
fc = lop.FakeWeightedMin(dim)
elif args.f == 'squared_min_max':
fc = lop.FakeSquaredMinMax(dim)
elif args.f == 'mix_gauss':
fc = lop.FakeMixtureGaussian(dim)
elif args.f == 'int_gauss':
f_to_int = lop.FakeMixtureGaussian(dim)
fc = lop.FakeIntegrate(dim, f_to_int)
else:
print('Unknown function: ' + str(args.f))
return
Expand Down
66 changes: 66 additions & 0 deletions src/lop/utilities/FakeFunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
# Includes randomization for full tests. Some of this code I wrote in 2022.

import numpy as np
from scipy.stats import norm, multivariate_normal

import pdb

## Fake function
Expand Down Expand Up @@ -244,6 +246,36 @@ def __str__(self):
return 'FakeSinExp: (w: ' + str(self.w) + ' k: ' + str(self.k) + ' phase: ' + str(self.phase) + ')'


class FakeMixtureGaussian(FakeFunction):

def __init__(self, dimension=2):
self.dim = dimension
self.randomize()

def randomize(self):
self.num_kerns = np.random.randint(2, 10)

self.sigs = np.random.exponential(1.0, size=(self.num_kerns, self.dim))
self.covs = np.array([np.diag(self.sigs[i]) for i in range(len(self.sigs))])
self.means = np.random.random(size=(self.num_kerns, self.dim)) * 3.0

def calc(self, rewards):


if isinstance(rewards, np.ndarray):
z = np.zeros(rewards.shape[0])
else:
z = 0

for i in range(self.num_kerns):
#z += norm.pdf(rewards, loc=self.means[i], scale=self.sigs[i])
z += multivariate_normal.pdf(rewards, mean=self.means[i], cov=self.covs[i])

return z

def __str__(self):
return 'MixtureGaussian (num_kerns: '+ str(self.num_kerns) + ', sigs: ' + str(self.sigs) + ', means=' + str(self.means)




Expand All @@ -263,3 +295,37 @@ def __str__(self):
return 'FakeStaticSin: ()'




############################## integrate fake function (for monotonic (if postive values))

class FakeIntegrate(FakeFunction):

def __init__(self, dimension, fc):
print(dimension)
self.dim = dimension
self.fc = fc
self.randomize()


def randomize(self):
self.fc.randomize()

# perform "integration" between 0, 3
grid_size = 0.05
ticks = np.arange(0, 10.2, grid_size)

vals = np.zeros((len(ticks),) * self.dim)


for i in range(1,len(ticks)):
for j in range(1, len(ticks)):
vals[i,j] = vals[i-1, j] + vals[i, j-1] - vals[i-1, j-1] + self.fc.calc(np.array([[ticks[i], ticks[j]]]))*grid_size

self.vals = vals
self

def calc(self, rewards):
return rewards[:,0]


2 changes: 1 addition & 1 deletion src/lop/utilities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .training_utility import k_fold_x_y, get_y_with_idx, normalize_0_1
from .human_choice_model import p_human_choice, sample_human_choice
from .pareto import get_pareto
from .FakeFunction import FakeFunction, FakeLinear, FakeSquared, FakeLogistic, FakeSinExp, FakeWeightedMax, FakeWeightedMin, FakeSquaredMinMax, FakeStaticSin
from .FakeFunction import FakeFunction, FakeLinear, FakeSquared, FakeLogistic, FakeSinExp, FakeWeightedMax, FakeWeightedMin, FakeSquaredMinMax, FakeStaticSin, FakeMixtureGaussian, FakeIntegrate
from .mcmc_sampling import metropolis_hastings, normal_prop_dist
from .gamma_dist import pdf_gamma, log_pdf_gamma, d_log_pdf_gamma
from .probability_utility import calc_cdf
Expand Down
25 changes: 24 additions & 1 deletion tests/utilities/test_fake_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,27 @@ def test_fake_min_1d():
f.randomize()
z3 = f(1.0)

assert z3 <= 1.0
assert z3 <= 1.0




def test_mixture_gaussians():
f = lop.FakeMixtureGaussian(dimension=1)

assert f is not None
assert isinstance(f, lop.FakeFunction)

f.randomize()

assert f is not None

z = f(np.array([1.0,0]))
z2 = f(np.array([1.0,0]))
assert len(z) == 2
assert (z == z2).all

f.randomize()
z3 = f(1.0)

assert z3 >= 0.0

0 comments on commit 270fe5f

Please sign in to comment.