Skip to content

Commit

Permalink
Indexed sets: allow assigning values from dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
fdabrandao committed May 17, 2024
1 parent 4383d52 commit 3e96cdc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## 0.14.0 - 2024-05-##
- Allow assigning values to indexed sets from a dictionary with the lists of members
for every index.

## 0.13.3 - 2024-02-20
- Fix issues with AMPL.solve(verbose=False) when the solver argument was not set.

Expand Down
11 changes: 8 additions & 3 deletions amplpy/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,15 @@ def set_values(self, values):
.. code-block:: python
A, AA = ampl.getSet('A'), ampl.getSet('AA')
AA.setValues(A.getValues()) # AA has now the members {1, 2}
A, AA = ampl.get_set('A'), ampl.get_set('AA')
AA.set_values(A.get_values()) # AA has now the members {1, 2}
"""
if isinstance(values, DataFrame):
if not self.is_scalar():
if not isinstance(values, dict):
raise TypeError("Excepted dictionary of set members for each index.")
for index, members in values.items():
self[index].set_values(members)
elif isinstance(values, DataFrame):
Entity.set_values(self, values)
elif isinstance(values, Iterable):
dimen = self.arity()
Expand Down
18 changes: 18 additions & 0 deletions amplpy/tests/test_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,24 @@ def test_set_iterable(self):
with self.assertRaises(ValueError):
ampl.get_set("B").set_values([1, 2])

def test_indexed_set(self):
ampl = self.ampl
ampl.eval(
r"""
set I;
set J{I};
"""
)
self.assertEqual(ampl.set["I"].is_scalar(), True)
self.assertEqual(ampl.set["J"].is_scalar(), False)
ampl.set["I"] = range(10)
ampl.set["J"] = {i: range(i) for i in range(10)}
self.assertEqual(ampl.set["I"].size(), 10)
self.assertEqual(list(ampl.set["I"].members()), list(range(10)))
for i in range(10):
self.assertEqual(ampl.set["J"][i].size(), i)
self.assertEqual(list(ampl.set["J"][i].members()), list(range(i)))

def test_precision(self):
pi = 3.1415926535897932384626433832795028841971
ampl = self.ampl
Expand Down

0 comments on commit 3e96cdc

Please sign in to comment.