Skip to content

Commit

Permalink
Code linting and improvement, performance and computational improveme…
Browse files Browse the repository at this point in the history
…nt and modularization
  • Loading branch information
Ahmed-Bayoumy committed Oct 6, 2024
1 parent 1dcc639 commit faaa1d1
Show file tree
Hide file tree
Showing 33 changed files with 2,457 additions and 2,904 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/win-build-and-pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
pip install flake8 pytest pytest-cov
pip install wheel
python setup.py sdist bdist_wheel
pip install dist/OMADS-2408.1-py3-none-any.whl
pip install dist/OMADS-2410-py3-none-any.whl
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ visualize.py
vis.py
deeplearning.mplstyle
CFD
temp
temp
tests/*.log
testing*
Binary file removed dist/OMADS-2408.1-py3-none-any.whl
Binary file not shown.
Binary file added dist/OMADS-2410-py3-none-any.whl
Binary file not shown.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ requires = [
build-backend = "setuptools.build_meta"

[tool.pytest.ini_options]
addopts = "--cov=OMADS"
testpaths = [
"tests",
]
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = OMADS
version = 2408.1
version = 2410
author = Ahmed H. Bayoumy
author_email = ahmed.bayoumy@mail.mcgill.ca
description = "Python package for DFO; an implementation of the mesh adaptive direct search (MADS)."
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
name="OMADS",
author="Ahmed H. Bayoumy",
author_email="ahmed.bayoumy@mail.mcgill.ca",
version='2408.1',
version='2410',
packages=find_packages(include=['OMADS', 'OMADS.*']),
description="Mesh Adaptive Direct Search (MADS)",
install_requires=[
Expand All @@ -14,7 +14,8 @@
'scipy',
'pyDOE2',
'samplersLib>=2408',
'paramiko>=3.4.0'
'paramiko>=3.4.0',
'deap==1.4'
],
extras_require={
'interactive': ['matplotlib>=3.5.2', 'plotly>=5.14.1'],
Expand Down
84 changes: 39 additions & 45 deletions src/OMADS/Barrier.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import copy
from dataclasses import dataclass, field
from typing import List, Any
from typing import List, Optional
from .CandidatePoint import CandidatePoint
from .Point import Point
from ._globals import *
from ._globals import DType, DESIGN_STATUS, EVAL_TYPE
import numpy as np
from typing import Protocol
from .Parameters import Parameters
from .Cache import Cache

@dataclass
class BarrierData(Protocol):
_xFeas: List[CandidatePoint] = None
_xInf: List[CandidatePoint] = None
_xFeas: Optional[List[CandidatePoint]] = None
_xInf: Optional[List[CandidatePoint]] = None

_xIncFeas: List[CandidatePoint] = None
_xIncInf: List[CandidatePoint] = None
_xIncFeas: Optional[List[CandidatePoint]] = None
_xIncInf: Optional[List[CandidatePoint]] = None

_refBestFeas: CandidatePoint = None
_refBestInf: CandidatePoint = None
_refBestFeas: Optional[CandidatePoint] = None
_refBestInf: Optional[CandidatePoint] = None

_dtype: DType = None
_dtype: Optional[DType] = None

def init(self, xFeas: CandidatePoint = None, evalType: EVAL_TYPE = None, barrierInitializedFromCache: bool = True):
def init(self, eval_point_list: Optional[List[Point]] = None):
...

def getAllXFeas(self):
Expand Down Expand Up @@ -91,7 +90,7 @@ def getSuccessTypeOfPoints(self):
def updateWithPoints(self):
...

def findPoint(self, Point: Point, foundEvalPoint: CandidatePoint):
def findPoint(self, point: Point):
...

def setN(self):
Expand All @@ -112,12 +111,12 @@ def findEvalPoint(self):
class BarrierBase(BarrierData):


_hMax: float = np.inf
_h_max: float = np.inf

_n: int = 0

def __init__(self, hMax: float = np.inf):
self._hMax = hMax
def __init__(self, h_max: float = np.inf):
self._h_max = h_max
self._n = 0
self._dtype = DType()
self._xInf = []
Expand All @@ -126,25 +125,25 @@ def __init__(self, hMax: float = np.inf):
self._xIncInf = []

def setN(self):
isSet: bool = False
is_set: bool = False
s: str

for cp in self.getAllPoints():
if not isSet:
if not is_set:
self._n = cp._n
isSet = True
is_set = True
elif cp._n != self._n:
s = f"Barrier has points of size {self._n} and of size {cp._n}"
raise IOError(s)
if not isSet:
if not is_set:
raise IOError("Barrier could not set point size")

def checkCache(self, cache: Cache):
def checkCache(self, cache: Cache = None):
if cache == None:
raise IOError("Cache must be instantiated before initializing Barrier.")

def checkHMax(self):
if self._hMax is None or self._hMax < self._dtype.zero:
if self._h_max is None or self._h_max < self._dtype.zero:
raise IOError("Barrier: hMax must be positive.")

def clearXFeas(self):
Expand All @@ -155,19 +154,19 @@ def clearXInf(self):
del self._xIncInf

def getAllPoints(self) -> List[CandidatePoint]:
allPoints: List[CandidatePoint] = []
all_points: List[CandidatePoint] = []
if self._xFeas is None:
self._xFeas = []
for cp in self._xFeas:
allPoints.append(cp)
all_points.append(cp)
if self._xInf is None:
self._xInf = []
for cp in self._xInf:
allPoints.append(cp)
all_points.append(cp)

return allPoints
return all_points

def getFirstPoint(self) -> CandidatePoint:
def getFirstPoint(self) -> Optional[CandidatePoint]:
if self._xIncFeas and len(self._xIncFeas) > 0:
return self._xIncFeas[0]
elif self._xFeas and len(self._xFeas) > 0:
Expand All @@ -180,43 +179,38 @@ def getFirstPoint(self) -> CandidatePoint:
return None

def findEvalPoint(self, cps: List[CandidatePoint] = None, cp: CandidatePoint = None):
ind = 0
for p in cps:
if p.signature == cp.signature:
return True, p
ind+=1

return False, p

def findPoint(self, Point: Point, foundEvalPoint: CandidatePoint) -> bool:
def findPoint(self, point: Point) -> bool:
found: bool = False

evalPointList: List[CandidatePoint] = self.getAllPoints()
for cp in evalPointList:
if cp._n != Point._n:
eval_point_list: List[CandidatePoint] = self.getAllPoints()
for cp in eval_point_list:
if cp._n != point._n:
raise IOError("Error: Eval points have different dimensions")
if Point == cp.coordinates:
foundEvalPoint = copy.deepcopy(cp)
if point == cp.coordinates:
found = True
break

return found

def checkXFeas(self, xFeas: CandidatePoint = None, evalType: EVAL_TYPE = None):
if xFeas.evaluated:
self.checkXFeasIsFeas(xFeas=xFeas, evalType=evalType)

def checkXFeas(self, x_feas: CandidatePoint = None, eval_type: EVAL_TYPE = None):
if x_feas.evaluated:
self.checkXFeasIsFeas(x_feas=x_feas, eval_type=eval_type)

def getAllXFeas(self):
return self._xFeas

def checkXFeasIsFeas(self, xFeas: CandidatePoint=None, evalType: EVAL_TYPE = None):
if xFeas.evaluated and xFeas.status != DESIGN_STATUS.ERROR:
h = xFeas.h
if h is None or h!= 0.0:
def checkXFeasIsFeas(self, x_feas: CandidatePoint=None, eval_type: EVAL_TYPE = None):
if x_feas.evaluated and x_feas.status != DESIGN_STATUS.ERROR:
h = x_feas.h
if h is None or not np.isclose(h, 0.0, rtol=1e-09, atol=1e-09):
raise IOError(f"Error: Barrier: xFeas' h value must be 0.0, got: {h}")


def checkXInf(self, xInf: CandidatePoint = None, evalType: EVAL_TYPE = None):
if not xInf.evaluated:
def checkXInf(self, x_inf: CandidatePoint = None, eval_type: EVAL_TYPE = None):
if not x_inf.evaluated:
raise IOError("Barrier: xInf must be evaluated before being set.")
Loading

0 comments on commit faaa1d1

Please sign in to comment.