Skip to content

Commit

Permalink
Merge pull request #5 from dapensoft/improve-print-outputs
Browse files Browse the repository at this point in the history
Improve Print Outputs
  • Loading branch information
idapena committed Mar 3, 2024
2 parents 56dff0d + b8b03ec commit 0b9b5c7
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 40 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
</a>

<a href="https://pypi.org/project/pyorlib" target="_blank">
<img src="https://img.shields.io/pypi/v/pyorlib?color=deeppink" alt="Package version">
<img src="https://img.shields.io/pypi/v/pyorlib?color=e92063" alt="Package version">
</a>

<a href="https://pypi.org/project/pyorlib" target="_blank">
<img src="https://img.shields.io/pypi/pyversions/pyorlib?color=deeppink" alt="Supported Python versions">
<img src="https://img.shields.io/pypi/pyversions/pyorlib?color=e92063" alt="Supported Python versions">
</a>

<a href="https://github.com/psf/black">
Expand Down
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ hide:
</a>

<a href="https://pypi.org/project/pyorlib" target="_blank">
<img src="https://img.shields.io/pypi/v/pyorlib?color=deeppink" alt="Package version">
<img src="https://img.shields.io/pypi/v/pyorlib?color=e92063" alt="Package version">
</a>

<a href="https://pypi.org/project/pyorlib" target="_blank">
<img src="https://img.shields.io/pypi/pyversions/pyorlib?color=deeppink" alt="Supported Python versions">
<img src="https://img.shields.io/pypi/pyversions/pyorlib?color=e92063" alt="Supported Python versions">
</a>

<a href="https://github.com/psf/black">
Expand Down
17 changes: 17 additions & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ hide:

[//]: # (--------------------------------------------------------------------------------------------------------------)

## [v0.1.1](https://github.com/dapensoft/pyorlib/releases/tag/0.1.1) <small>March 2, 2024</small> { id="0.1.1" }

<hr class="divider">

##### Changed

- The `print_info` and `print_solution` methods in the `Model` class have been enhanced to improve visibility and
provide deeper insights.
- The `get_pretty_string` method in the `Term` class has been refactored as an `@abstractmethod` for subclass
customization.
- Badges on the main page of the documentation and the readme file have been updated to improve visibility and align
with the package's color palette.
- The `git-committers` plugin in the `mkdocs.yml` file has been updated to exclude the `index.md`, `examples/index.md`,
and `api/index.md` files for consistency in the current configuration with the `git-revision-date-localized` plugin.

[//]: # (--------------------------------------------------------------------------------------------------------------)

## [v0.1.0](https://github.com/dapensoft/pyorlib/releases/tag/0.1.0) <small>March 2, 2024</small> { id="0.1.0" }

<hr class="divider">
Expand Down
6 changes: 5 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ plugins:
- git-committers:
repository: dapensoft/pyorlib
branch: master
exclude:
- examples/index.md
- api/index.md
- index.md
- git-revision-date-localized:
type: timeago
enable_creation_date: true
Expand Down Expand Up @@ -126,7 +130,7 @@ extra_javascript:
nav:
- PyORlib: index.md
- Getting Started: getting-started.md
- Examples:
- Examples:
- examples/index.md
- A Transportation Model: examples/a-transportation-problem.md
- API Reference:
Expand Down
2 changes: 1 addition & 1 deletion src/pyorlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
mathematical models in a standardized manner across different optimization packages.
"""

__version__ = "0.1.0"
__version__ = "0.1.1"

from .engines import Engine
from .model import Model
Expand Down
14 changes: 14 additions & 0 deletions src/pyorlib/algebra/terms/constants/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any

from ..term import Term
from ....core.constants import StdOutColors
from ....enums import ValueType, TermType
from ....exceptions import TermException
from ....validators import ValueTypeValidator
Expand Down Expand Up @@ -66,3 +67,16 @@ def __init__(self, name: str, value_type: ValueType, value: float):

self._value: float = value
""" The internal value of the constant. """

def get_pretty_string(self, float_precision: int = 6) -> str: # pragma: no cover
default, debug = StdOutColors.DEFAULT, StdOutColors.PURPLE
return "".join(
[
f"Name: {debug}{self.name}{default} | ",
f"Type: {debug}{self.term_type.name.capitalize()}{default} | ",
f"Value type: {debug}{self.value_type.name.capitalize()}{default} | ",
f"Val:{debug} ",
"{0:.{prec}g} ".format(self.value, prec=float_precision),
f"{default}",
]
)
20 changes: 3 additions & 17 deletions src/pyorlib/algebra/terms/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from ..element import Element
from ..expressions import Expression
from ...core.constants import StdOutColors
from ...enums import TermType, ValueType
from ...exceptions import TermException

Expand Down Expand Up @@ -121,27 +120,14 @@ def __init__(self, term_type: TermType, value_type: ValueType):
def _build_expression(self, expression: Any) -> Element:
return Expression(expression=expression)

def get_pretty_string(self, float_precision: int = 6) -> str: # pragma: no cover
@abstractmethod
def get_pretty_string(self, float_precision: int = 6) -> str:
"""
Returns a formatted string representation of the term.
:param float_precision: It represents the number of digits used in printing the solution and objective.
:return: A formatted string representing the term.
"""
default, debug = StdOutColors.DEFAULT, StdOutColors.PURPLE
return "".join(
[
f"Name: {debug}{self.name}{default} | ",
f"Type: {debug}{self.term_type.name.capitalize()}{default} | ",
f"Value type: {debug}{self.value_type.name.capitalize()}{default} | ",
f"lb:{debug} ",
"{0:.{prec}g} ".format(self.lower_bound, prec=float_precision),
f"{default}| ub:{debug} ",
"{0:.{prec}g} ".format(self.upper_bound, prec=float_precision),
f"{default}| val:{debug} ",
"{0:.{prec}g} ".format(self.value, prec=float_precision),
f"{default}",
]
)
pass

def __str__(self) -> str: # pragma: no cover
return "".join(
Expand Down
18 changes: 18 additions & 0 deletions src/pyorlib/algebra/terms/variables/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from math import inf

from ..term import Term
from ....core.constants import StdOutColors
from ....enums import ValueType, TermType
from ....exceptions import TermException

Expand Down Expand Up @@ -44,3 +45,20 @@ def __init__(self, name: str, value_type: ValueType, lower_bound: float = 0, upp
raise TermException("Invalid lower bound for an integer variable term.")
if value_type == ValueType.INTEGER and not upper_bound == inf and not float(upper_bound).is_integer():
raise TermException("Invalid upper bound for an integer variable term.")

def get_pretty_string(self, float_precision: int = 6) -> str: # pragma: no cover
default, debug = StdOutColors.DEFAULT, StdOutColors.PURPLE
return "".join(
[
f"Name: {debug}{self.name}{default} | ",
f"Type: {debug}{self.term_type.name.capitalize()}{default} | ",
f"Value type: {debug}{self.value_type.name.capitalize()}{default} | ",
f"Lb:{debug} ",
"{0:.{prec}g} ".format(self.lower_bound, prec=float_precision),
f"{default}| Ub:{debug} ",
"{0:.{prec}g} ".format(self.upper_bound, prec=float_precision),
f"{default}| Val:{debug} ",
"{0:.{prec}g} ".format(self.value, prec=float_precision),
f"{'(N/A) ' if self.value == -0.0 else ''}{default}",
]
)
34 changes: 18 additions & 16 deletions src/pyorlib/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,67 +445,69 @@ def print_info(self, display_term_sets: bool = False) -> None: # pragma: no cov
:param display_term_sets: Whether to display information about term sets. Defaults to False.
:return: None.
"""
default, debug = StdOutColors.DEFAULT, StdOutColors.PURPLE
print(f"\n------ MODEL INFORMATION ------\n")
print("Model properties:")
print(f"\tName: {StdOutColors.PURPLE}{self.name}{StdOutColors.DEFAULT}")
print(f"\tName: {debug}{self.name}{default}")
print("Objective function:")
print(f"\tExpression: {self.objective_expr}")
print(f"\tStatus: {StdOutColors.PURPLE}{self.solution_status.name}{StdOutColors.DEFAULT}")
print(f"\tStatus: {debug}{self.solution_status.name}{default}")
print(
f"\tValue: {StdOutColors.PURPLE}",
f"\tValue: {debug}",
(
"{0:.{prec}g}".format(self.objective_value, prec=self.float_precision)
if self.objective_value is not None
else "--"
),
f"{StdOutColors.DEFAULT}",
f"{default}",
)

print("Dimensions:")
print(f"Dimensions: {debug}{len(self.dimensions)}{default}")
for name, size in self.dimensions.items():
print(
f"\tName: {StdOutColors.PURPLE}{name}{StdOutColors.DEFAULT} | ",
f"Val: {StdOutColors.PURPLE}{size}{StdOutColors.DEFAULT}",
f"\tName: {debug}{name}{default} | ",
f"Val: {debug}{size}{default}",
)

print("Terms:")
print(f"Terms: {debug}{len(self.terms)}{default}")
for name, term in self.terms.items():
print(f"\t{term.get_pretty_string(float_precision=self.float_precision)}")
if display_term_sets:
print("Terms Sets:")
print(f"Terms Sets: {debug}{len(self.term_sets)}{default}")
for name, terms in self.term_sets.items():
print(f"\tTerm: {StdOutColors.PURPLE}{name}{StdOutColors.DEFAULT}")
print(f"\tTerm: {debug}{name}{default}")
for index, term in terms.items():
print(
f"\t\tIndex: {StdOutColors.PURPLE}{index}{StdOutColors.DEFAULT} | ",
f"\t\tIndex: {debug}{index}{default} | ",
f"{term.get_pretty_string(float_precision=self.float_precision)}",
)

print("Constraints:")
constraints = self.constraints
print(f"Constraints: {debug}{len(constraints)}{default}")
for exp in constraints:
try:
print(f"\tExpression: {exp}")
except RecursionError:
print("\tExpression: Unprintable expression")
print("\tExpression: Print Error")
print()

def print_solution(self) -> None: # pragma: no cover
"""
Prints the solution of the optimization problem.
:return: None.
"""
default, debug = StdOutColors.DEFAULT, StdOutColors.PURPLE
print(f"\n------ MODEL SOLUTION ------\n")
print("Objective function:")
print(f"\tStatus: {StdOutColors.PURPLE}{self.solution_status.name}{StdOutColors.DEFAULT}")
print(f"\tStatus: {debug}{self.solution_status.name}{default}")
print(
f"\tValue: {StdOutColors.PURPLE}",
f"\tValue: {debug}",
(
"{0:.{prec}g}".format(self.objective_value, prec=self.float_precision)
if self.objective_value is not None
else "--"
),
f"{StdOutColors.DEFAULT}",
f"{default}",
)

solution_variables: Dict[str, Term] = {
Expand Down
2 changes: 1 addition & 1 deletion tests/algebra/terms/test_term.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def value(self) -> float:
def raw(self) -> Any:
pass

def validate(self) -> None:
def get_pretty_string(self, float_precision: int = 6) -> str:
pass

def __init__(self):
Expand Down

0 comments on commit 0b9b5c7

Please sign in to comment.