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

Creating variable for each linear expression #300

Closed
aurelije opened this issue Jun 5, 2024 · 4 comments
Closed

Creating variable for each linear expression #300

aurelije opened this issue Jun 5, 2024 · 4 comments

Comments

@aurelije
Copy link
Contributor

aurelije commented Jun 5, 2024

Hi linopyers,
What is the recommended way to create variable for each linear expression? The context is that operation has been done on multidimensional variable resulting in LinearExpresssion object. I need to set soft constraint for that expression so I need variable per each linear expression to create constraint.

For me it was logical to do something like this:

soft_constraint_deviation_var = model.add_variables(
    0, 
    100, 
    coords=linear_expression.coords, 
    name='soft_constraint_deviation_var')

Variable gets created but if I try to print it I get KeyError: '_term'

The thing that worked (but it looks like too much of work) is:

soft_constraint_deviation_var = model.add_variables(
    linear_expression.assign(lower=0).data.lower, 
    linear_expression.assign(upper=100).data.upper, 
    name='soft_constraint_deviation_var')

Maybe in api that is made more for OR people and less for XArray people, we could have an method to generate soft constraint and/or deviation varaible.

@FabianHofmann
Copy link
Collaborator

FabianHofmann commented Jun 5, 2024

good question! (EDIT HERE, previous answer was wrong) how does linear_expression.coords look like?

@FabianHofmann
Copy link
Collaborator

ah, you could take linear_exprssion.indexes instead of .coords

@aurelije
Copy link
Contributor Author

aurelije commented Jun 5, 2024

It worked, just it is a bit misleading that for parameter named coords you need to send indexes as an argument

@FabianHofmann
Copy link
Collaborator

I know, it would be nice if that worked. but I would not know how to realize that. I am closing this issue, but if you don't mind you are could to document your use cases so that we can incorporate them into the documentation later.

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

2 participants