Skip to content

Commit

Permalink
fix undefined grad; apply pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
luk036 committed Sep 9, 2023
1 parent 3c71e17 commit 9ce0bbe
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 16 deletions.
1 change: 0 additions & 1 deletion lowpass_oracle_ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,3 @@ int main() {

return 0;
}

13 changes: 9 additions & 4 deletions src/ellalgo/cutting_plane.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import copy
from typing import Any, MutableSequence, Optional, Tuple, Union
from typing import Generic
from typing import Any, Generic, MutableSequence, Optional, Tuple, Union

from .ell_config import CutStatus, Options
from .ell_typing import (
Expand Down Expand Up @@ -83,7 +82,10 @@ def cutting_plane_feas(


def cutting_plane_optim(
omega: OracleOptim[ArrayType], space: SearchSpace[ArrayType], target, options=Options()
omega: OracleOptim[ArrayType],
space: SearchSpace[ArrayType],
target,
options=Options(),
) -> Tuple[Optional[ArrayType], float, int]:
"""Cutting-plane method for solving convex optimization problem
Expand Down Expand Up @@ -146,7 +148,10 @@ def cutting_plane_feas_q(


def cutting_plane_optim_q(
omega: OracleOptimQ[ArrayType], space_q: SearchSpaceQ[ArrayType], target, options=Options()
omega: OracleOptimQ[ArrayType],
space_q: SearchSpaceQ[ArrayType],
target,
options=Options(),
) -> Tuple[Optional[ArrayType], float, int]:
"""Cutting-plane method for solving convex discrete optimization problem
Expand Down
4 changes: 2 additions & 2 deletions src/ellalgo/ell_calc.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from math import sqrt
from typing import Tuple, Optional
from typing import Optional, Tuple

from .ell_config import CutStatus
from .ell_calc_core import EllCalcCore
from .ell_config import CutStatus


class EllCalc:
Expand Down
2 changes: 1 addition & 1 deletion src/ellalgo/ell_stable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from .ell_calc import EllCalc
from .ell_config import CutStatus
from .ell_typing import SearchSpace, SearchSpaceQ, ArrayType
from .ell_typing import ArrayType, SearchSpace, SearchSpaceQ

Matrix = np.ndarray
CutChoice = Union[float, ArrayType] # single or parallel
Expand Down
5 changes: 3 additions & 2 deletions src/ellalgo/ell_typing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from abc import ABC, abstractmethod
from typing import MutableSequence, Optional, Tuple, Union
from typing import Generic, TypeVar
from typing import Generic, MutableSequence, Optional, Tuple, TypeVar, Union

import numpy as np

from .ell_config import CutStatus

ArrayType = TypeVar("ArrayType", bound=np.ndarray)
Expand Down
10 changes: 6 additions & 4 deletions src/ellalgo/oracles/lowpass_oracle.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from math import floor
from typing import Tuple

import numpy as np
from math import floor

Arr = np.ndarray
Cut = Tuple[Arr, float]
Expand Down Expand Up @@ -40,7 +40,9 @@
class LowpassOracle:
more_alt: bool = True

def __init__(self, ndim: int, wpass: float, wstop: float, lp_sq: float, up_sq: float):
def __init__(
self, ndim: int, wpass: float, wstop: float, lp_sq: float, up_sq: float
):
# *********************************************************************
# optimization parameters
# *********************************************************************
Expand Down Expand Up @@ -110,8 +112,8 @@ def assess_optim(self, x: Arr, sp_sq: float):

# case 1 (unlikely)
if x[0] < 0:
grade = np.zeros(ndim)
grade[0] = -1.0
grad = np.zeros(ndim)
grad[0] = -1.0
return (grad, -x[0]), None

# Begin objective function
Expand Down
5 changes: 3 additions & 2 deletions tests/test_lowpass.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import time

import numpy as np

from ellalgo.cutting_plane import Options, cutting_plane_optim
from ellalgo.ell import Ell
from ellalgo.oracles.lowpass_oracle import create_lowpass_case
Expand Down Expand Up @@ -33,15 +34,15 @@ def run_lowpass(use_parallel_cut: bool, duration=0.000001):


def test_lowpass():
""" Test the lowpass case with parallel cut """
"""Test the lowpass case with parallel cut"""
result, feasible = run_lowpass(True)
assert feasible
assert result >= 1083
assert result <= 1194


def test_no_parallel_cut():
""" Test the lowpass case with no parallel cut """
"""Test the lowpass case with no parallel cut"""
result, feasible = run_lowpass(False)
assert feasible
assert result >= 16461

0 comments on commit 9ce0bbe

Please sign in to comment.