Skip to content

Commit

Permalink
update test_bm_example1
Browse files Browse the repository at this point in the history
  • Loading branch information
luk036 committed Jul 31, 2024
1 parent c78e09b commit a86565a
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions benches/test_bm_example1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

import numpy as np

from ellalgo.cutting_plane import cutting_plane_optim
from ellalgo.cutting_plane import Options, cutting_plane_optim
from ellalgo.ell import Ell
from ellalgo.ell_typing import OracleFeas, OracleOptim


class MyOracleFeas(OracleFeas):
idx = 0
gamma = 0.0

# constraint 1: x + y <= 3
def fn1(self, x, y):
Expand All @@ -19,15 +20,21 @@ def fn1(self, x, y):
def fn2(self, x, y):
return -x + y + 1

def fn0(self, x, y):
return self.gamma - (x + y)

def grad1(self):
return np.array([1.0, 1.0])

def grad2(self):
return np.array([-1.0, 1.0])

def grad0(self):
return np.array([-1.0, -1.0])

def __init__(self):
self.fns = (self.fn1, self.fn2)
self.grads = (self.grad1, self.grad2)
self.fns = (self.fn1, self.fn2, self.fn0)
self.grads = (self.grad1, self.grad2, self.grad0)

def assess_feas(self, z):
"""[summary]
Expand All @@ -40,9 +47,9 @@ def assess_feas(self, z):
"""
x, y = z

for _ in [0, 1]:
for _ in range(3):
self.idx += 1
if self.idx == 2:
if self.idx == 3:
self.idx = 0 # round robin
if (fj := self.fns[self.idx](x, y)) > 0:
return self.grads[self.idx](), fj
Expand All @@ -62,20 +69,16 @@ def assess_optim(self, z, gamma: float):
Returns:
[type]: [description]
"""
self.helper.gamma = gamma
if cut := self.helper.assess_feas(z):
return cut, None
x, y = z
# objective: maximize x + y
f0 = x + y
if (fj := gamma - f0) > 0.0:
return (-1.0 * np.array([1.0, 1.0]), fj), None

gamma = f0
return (-1.0 * np.array([1.0, 1.0]), 0.0), gamma
return (np.array([-1.0, -1.0]), 0.0), x + y


class MyOracleFeas2(OracleFeas):
idx = 0
gamma = 0.0

# constraint 1: x + y <= 3
def fn1(self, x, y):
Expand All @@ -85,15 +88,21 @@ def fn1(self, x, y):
def fn2(self, x, y):
return -x + y + 1

def fn0(self, x, y):
return self.gamma - (x + y)

def grad1(self):
return np.array([1.0, 1.0])

def grad2(self):
return np.array([-1.0, 1.0])

def grad0(self):
return np.array([-1.0, -1.0])

def __init__(self):
self.fns = (self.fn1, self.fn2)
self.grads = (self.grad1, self.grad2)
self.fns = (self.fn1, self.fn2, self.fn0)
self.grads = (self.grad1, self.grad2, self.grad0)

def assess_feas(self, z):
"""[summary]
Expand All @@ -105,13 +114,9 @@ def assess_feas(self, z):
[type]: [description]
"""
x, y = z

for _ in [0, 1]:
self.idx += 1
if self.idx == 2:
self.idx = 0 # round robin
if (fj := self.fns[self.idx](x, y)) > 0:
return self.grads[self.idx](), fj
for idx in range(3):
if (fj := self.fns[idx](x, y)) > 0:
return self.grads[idx](), fj
return None


Expand All @@ -128,31 +133,29 @@ def assess_optim(self, z, gamma: float):
Returns:
[type]: [description]
"""
self.helper.gamma = gamma
if cut := self.helper.assess_feas(z):
return cut, None
x, y = z
# objective: maximize x + y
f0 = x + y
if (fj := gamma - f0) > 0.0:
return (-1.0 * np.array([1.0, 1.0]), fj), None

gamma = f0
return (-1.0 * np.array([1.0, 1.0]), 0.0), gamma
return (np.array([-1.0, -1.0]), 0.0), x + y


def run_example1(omega):
xinit = np.array([0.0, 0.0]) # initial xinit
ellip = Ell(10.0, xinit)
xbest, _, num_iters = cutting_plane_optim(omega(), ellip, float("-inf"))
options = Options()
options.tolerance = 1e-10
xbest, _, num_iters = cutting_plane_optim(omega(), ellip, float("-inf"), options)
assert xbest is not None
return num_iters


def test_bm_with_round_robin(benchmark):
num_iters = benchmark(run_example1, MyOracle)
assert num_iters == 31
assert num_iters == 25


def test_bm_without_round_robin(benchmark):
num_iters = benchmark(run_example1, MyOracle2)
assert num_iters == 31
assert num_iters == 25

0 comments on commit a86565a

Please sign in to comment.