Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

newSwitchAct with multiple enum parameters #53

Open
felixpollet opened this issue May 16, 2024 · 0 comments
Open

newSwitchAct with multiple enum parameters #53

felixpollet opened this issue May 16, 2024 · 0 comments

Comments

@felixpollet
Copy link
Contributor

It would be nice to be able to implement a switch activity that combines multiple enum parameters for the selection of other activities.
For example, rather than creating a first switch activity switch_act_1 controlled by switch_param_1 that points to switch_act_2a and switch_act_2b both of which are controlled by parameter switch_param_2, it could be possible to create a single multi_switch_act controlled by the combination of switch_param_1 and switch_param_2.

Here is an example :

multi_switch_act = newMultiSwitchAct(MYDB, "MultiSwitchAct", [switchParam1, switchParam2], {
    ("switchParam1_val1", switchParam2_val1) : act1  # act1 is selected if both switchParam1 = switchParam1_val1 and switchParam2 = switchParam2_val1
    ("switchParam1_val2", switchParam2_val2) : (act2, 0.4)  # act2 is selected if both switchParam1 = switchParam1_val2 and switchParam2 = switchParam2_val2
    ("switchParam1_val1", switchParam2_val2) : (act3, b + 6)  # act3 is selected if both switchParam1 = switchParam1_val1 and switchParam2 = switchParam2_val2
}

This could be done by modifying the newSwitchAct() method as follows:

def newMultiSwitchAct(dbname, name, paramDefList: Union[List[ParamDef], ParamDef], acts_dict: Union[Dict[str, ActivityOrActivityAmount], Dict[Tuple[str], ActivityOrActivityAmount]]):
    exch = defaultdict(lambda: 0)

    unit = None
    for key, act in acts_dict.items():
        amount = 1
        if isinstance(act, (list, tuple)):
            act, amount = act
        if isinstance(paramDefList, list):  # <-- this is new
            exch[act] += amount * reduce(lambda x, y: x*y, [paramDef.symbol(key[i]) for i, paramDef in enumerate(paramDefList)])
        else:  # <-- regular switch activity
            exch[act] += amount * paramDefList.symbol(key)
        unit = act["unit"]

    res = newActivity(dbname, name, unit=unit, exchanges=exch)

    return res

This should work for any number of enum parameters (not only two).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant