From e16750acda764c4df98b29bd54ed34df3700f0c8 Mon Sep 17 00:00:00 2001 From: metab0t Date: Thu, 13 Jun 2024 12:11:20 +0800 Subject: [PATCH] Documenting `tupledict.map` --- docs/source/container.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/source/container.md b/docs/source/container.md index 99058b0..b973f1e 100644 --- a/docs/source/container.md +++ b/docs/source/container.md @@ -121,6 +121,15 @@ subset_values = list(td.select("*", 2, 1)) subset_kv_iterator = td.select(1, "*", "*", with_key=True) ``` +## Apply a function to values with `map` method +`tupledict` provides a `map` method to apply a function to each value in the `tupledict`. The following example applies a function to each value in the `tupledict`: + +```{code-cell} +td = poi.make_tupledict(range(3), range(2), rule=lambda i, j: i+j) +td_new = td.map(lambda x: x*x) +td, td_new +``` + ## Building a model with `tupledict` `tupledict` can be used to store and manipulate multi-dimensional variables, constraints and expressions. Using it correctly will make building model more easily. @@ -156,6 +165,8 @@ obj = poi.quicksum(x.values(), lambda x: x*x) model.set_objective(obj, poi.ObjectiveSense.Minimize) model.optimize() + +x_value = x.map(model.get_value) ``` Here we use two utility functions to simplify how we express the sum notation