Skip to content

Commit

Permalink
Add facility_location as a valid test
Browse files Browse the repository at this point in the history
  • Loading branch information
staadecker committed Nov 12, 2024
1 parent c4cce82 commit 49edbf0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
16 changes: 15 additions & 1 deletion docs/quadratic_expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@ Quadratic expressions work as you'd expect. Simply multiply two linear expressio

## Example

## TODO
### Maximize area of box
Here's a short example that shows that a square maximizes the area of any box with a fixed perimeter.

```python3
import pyoframe as pf
model = pf.Model("max")
model.w = pf.Variable(lb=0)
model.h = pf.Variable(lb=0)
model.limit_perimter = 2 * (model.w + model.h) <= 20
model.objective = model.w * model.h
model.solve()
print(f"It's a square: {model.w.solution==model.h.solution}")

# Outputs: It's a square: True
```
### Facility Location Problem

See [examples/facility_location](../tests/examples/facility_location/).

## Note for Pyoframe developers: Internal Representation of Quadratics

Expand Down
6 changes: 3 additions & 3 deletions src/pyoframe/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def __pow__(self, power: int):
>>> from pyoframe import Variable
>>> Variable() ** 2
<Expression size=1 dimensions={} terms=1 degree=2>
x1*x1
x1 * x1
"""
if power == 2:
return self * self
Expand Down Expand Up @@ -726,10 +726,10 @@ def _add_const(self, const: int | float) -> Expression:
x1 +5
>>> Variable() ** 2 + 5
<Expression size=1 dimensions={} terms=2 degree=2>
x2*x2 +5
x2 * x2 +5
>>> Variable() ** 2 + Variable() + 5
<Expression size=1 dimensions={} terms=3 degree=2>
x3*x3 + x4 +5
x3 * x3 + x4 +5
"""
dim = self.dimensions
data = self.data
Expand Down
6 changes: 3 additions & 3 deletions tests/examples/facility_location/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pathlib import Path


def main(G, F, **kwargs):
def main(G=4, F=3, **kwargs):
model = pf.Model("min")

g_range = range(G)
Expand Down Expand Up @@ -53,7 +53,7 @@ def main(G, F, **kwargs):
return model


def draw_results(model, G, T):
def draw_results(model, G, F):
import tkinter

root = tkinter.Tk()
Expand Down Expand Up @@ -100,6 +100,6 @@ def draw_results(model, G, T):
F,
directory=working_dir / "results",
use_var_names=True,
solution_file=working_dir / "results" / "solution.sol",
solution_file=working_dir / "results" / "pyoframe-problem.sol",
)
draw_results(model, G, F)
19 changes: 14 additions & 5 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class Example:
many_valid_solutions=True,
has_gurobi_version=False,
),
Example(
"facility_location",
has_gurobi_version=False,
many_valid_solutions=True,
),
]


Expand All @@ -49,15 +54,19 @@ def test_examples(example: Example):
symbolic_solution_file = symbolic_output_dir / "pyoframe-problem.sol"
dense_solution_file = dense_output_dir / "pyoframe-problem.sol"

symbolic_model = main_module.main(
input_dir,
symbolic_kwargs = dict(
directory=symbolic_output_dir,
solution_file=symbolic_solution_file,
use_var_names=True,
)
dense_model = main_module.main(
input_dir, directory=dense_output_dir, solution_file=dense_solution_file
)
dense_kwargs = dict(directory=dense_output_dir, solution_file=dense_solution_file)

if input_dir.exists():
symbolic_kwargs["input_dir"] = input_dir
dense_kwargs["input_dir"] = input_dir

symbolic_model = main_module.main(**symbolic_kwargs)
dense_model = main_module.main(**dense_kwargs)

if example.check_params is not None:
for param, value in example.check_params.items():
Expand Down

0 comments on commit 49edbf0

Please sign in to comment.