Skip to content

Commit

Permalink
modified unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
luk036 committed Aug 2, 2024
1 parent c553042 commit 5eeaa57
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 123 deletions.
191 changes: 77 additions & 114 deletions benches/test_bm_example1.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,138 +5,101 @@

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


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

# constraint 1: x + y <= 3
def fn1(self, x, y):
return x + y - 3

# constraint 2: x - y >= 1
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])
class MyOracle1(OracleOptim):
"""
This Python class `MyOracle1` contains a method `assess_optim` that assesses optimization based on
given parameters and returns specific values accordingly.
"""

def __init__(self):
self.fns = (self.fn1, self.fn2, self.fn0)
self.grads = (self.grad1, self.grad2, self.grad0)
"""
Creates a new `MyOracle` instance with the `idx` field initialized to 0.
def assess_feas(self, z):
"""[summary]
This is the constructor for the `MyOracle` class, which is the main entry point for
creating new instances of this type. It initializes the `idx` field to 0, which is the
default value for this field.
Arguments:
z ([type]): [description]
Examples:
>>> oracle = MyOracle()
>>> assert oracle.idx == 0
"""
self.idx = 0

Returns:
[type]: [description]
def assess_optim(self, xc, gamma: float):
"""
x, y = z
The function assess_optim assesses feasibility and optimality of a given point based on a specified
gamma value.
:param xc: The `xc` parameter in the `assess_optim` method appears to represent a point in a
two-dimensional space, as it is being unpacked into `x` and `y` coordinates
:param gamma: Gamma is a parameter used in the `assess_optim` method. It is a float value that is
compared with the sum of `x` and `y` in the objective function. The method returns different values
based on the comparison of `gamma` with the sum of `x` and
:type gamma: float
:return: The `assess_optim` method returns a tuple containing two elements. The first element is a
tuple containing an array `[-1.0, -1.0]` and either the value of `fj` (if `fj > 0.0`) or `0.0` (if
`fj <= 0.0`). The second element of the tuple is
"""
x, y = xc
f0 = x + y

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


class MyOracle(OracleOptim):
helper = MyOracleFeas()

def assess_optim(self, z, gamma: float):
"""[summary]
Arguments:
z ([type]): [description]
gamma (float): the best-so-far optimal value
Returns:
[type]: [description]
"""
self.helper.gamma = gamma
if cut := self.helper.assess_feas(z):
return cut, None
x, y = z
# objective: maximize x + y
return (np.array([-1.0, -1.0]), 0.0), x + y

if self.idx == 0:
fj = f0 - 3.0
elif self.idx == 1:
fj = -x + y + 1.0
elif self.idx == 2:
fj = gamma - f0
else:
raise ValueError("Unexpected index value")

class MyOracleFeas2(OracleFeas):
gamma = 0.0
if fj > 0.0:
if self.idx == 0:
return ((np.array([1.0, 1.0]), fj), None)
elif self.idx == 1:
return ((np.array([-1.0, 1.0]), fj), None)
elif self.idx == 2:
return ((np.array([-1.0, -1.0]), fj), None)

# constraint 1: x + y <= 3
def fn1(self, x, y):
return x + y - 3

# constraint 2: x - y >= 1
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.fn0)
self.grads = (self.grad1, self.grad2, self.grad0)

def assess_feas(self, z):
"""[summary]
Arguments:
z ([type]): [description]
Returns:
[type]: [description]
"""
x, y = z
for idx in range(3):
if (fj := self.fns[idx](x, y)) > 0:
return self.grads[idx](), fj
return None
gamma = f0
return ((np.array([-1.0, -1.0]), 0.0), gamma)


class MyOracle2(OracleOptim):
helper = MyOracleFeas2()
"""
This Python class `MyOracle2` contains a method `assess_optim` that assesses optimization based on
given parameters and returns specific values accordingly.
"""

def assess_optim(self, z, gamma: float):
"""[summary]
Arguments:
z ([type]): [description]
gamma (float): the best-so-far optimal value
Returns:
[type]: [description]
def assess_optim(self, xc, gamma: float):
"""
The function assess_optim assesses feasibility and optimality of a given point based on a specified
gamma value.
:param xc: The `xc` parameter in the `assess_optim` method appears to represent a point in a
two-dimensional space, as it is being unpacked into `x` and `y` coordinates
:param gamma: Gamma is a parameter used in the `assess_optim` method. It is a float value that is
compared with the sum of `x` and `y` in the objective function. The method returns different values
based on the comparison of `gamma` with the sum of `x` and
:type gamma: float
:return: The `assess_optim` method returns a tuple containing two elements. The first element is a
tuple containing an array `[-1.0, -1.0]` and either the value of `fj` (if `fj > 0.0`) or `0.0` (if
`fj <= 0.0`). The second element of the tuple is
"""
self.helper.gamma = gamma
if cut := self.helper.assess_feas(z):
return cut, None
x, y = z
# objective: maximize x + y
return (np.array([-1.0, -1.0]), 0.0), x + y
x, y = xc
f0 = x + y
if (fj := f0 - 3.0) > 0.0:
return ((np.array([1.0, 1.0]), fj), None)
if (fj := -x + y + 1.0) > 0.0:
return ((np.array([-1.0, 1.0]), fj), None)
if (fj := gamma - f0) > 0.0:
return ((np.array([-1.0, -1.0]), fj), None)
return ((np.array([-1.0, -1.0]), 0.0), f0)


def run_example1(omega):
Expand All @@ -150,7 +113,7 @@ def run_example1(omega):


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


Expand Down
18 changes: 9 additions & 9 deletions tests/test_quasicvx2.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MyQuasicvxOracle(OracleOptim):
optimality of a given point based on constraints and an objective function.
"""

idx = 0
idx = 0 # for round robin

def assess_optim(self, xc, gamma: float):
"""
Expand All @@ -37,9 +37,9 @@ def assess_optim(self, xc, gamma: float):
"""
x, y = xc

for _ in range(4):
for _ in range(3):
self.idx += 1
if self.idx == 4:
if self.idx == 3:
self.idx = 0

if self.idx == 0:
Expand All @@ -55,14 +55,14 @@ def assess_optim(self, xc, gamma: float):
# constraint 3: x > 0
if x <= 0.0:
return (np.array([-1.0, 0.0]), -x), None
elif self.idx == 3:
# objective: minimize -sqrt(x) / y
tmp2 = math.sqrt(x)
if (fj := -tmp2 - gamma * y) > 0.0: # infeasible
return (np.array([-0.5 / tmp2, -gamma]), fj), None

# objective: minimize -sqrt(x) / y
tmp2 = math.sqrt(x)
if (fj := -tmp2 - gamma * y) > 0.0: # infeasible
return (np.array([-0.5 / tmp2, -gamma]), fj), None

gamma = -tmp2 / y
return (np.array([-0.5 / tmp2, -gamma]), 0), gamma
return (np.array([-0.5 / tmp2, -gamma]), 0.0), -tmp2 / y


def test_case_feasible():
Expand Down

0 comments on commit 5eeaa57

Please sign in to comment.