Pyomo: How to create new a model variable without declaring its name, and then store it in a dictionary? - optimization

I was wondering if there is a way to create a variable that attaches to the model without specifying its name. Or, how do I dynamically create variables with different names in pyomo?
Background:
I currently have a concrete model for a master problem and another concrete model for a subproblem. My goal is to solve a two stage optimization problem using a column and constraint generation algorithm under this master and subproblem framework.
I am trying to constrict a while loop to add new variables and constraints to the master problem. Specifically, I am trying to create a new variable for each iteration of a while loop. Then I'd like to add to the master problem some new constraints/cuts that involve the new variable in each iteration.
To do this, I was trying to construct a dictionary using a for loop. In the dictionary, each key corresponds to a newly created variable. I wrote something like this:
for i in I:
xname= 'x' + str(i)
new_x[xname] = model.add_component(xname, pyo.Var())
where I is the set of iteration counts used to create new variables and assign them to a dictionary key.
However, this doesn't work because model.add_component(xname, pyo.Var()) cannot be assigned to a key since it is a procedure that adds the new variable directly to the model. However, I can't use the standard syntax model.x = pyo.Var() to create the new variables either because it means I have to do it manually, defeating the purpose of a while loop.
I was wondering if there is a way to create a variable that attaches to the model without specifying its name. Or, how do I dynamically create different variables in pyomo?
Thank you!

Some first options:
You could use setattr(model, xnnam, pyo.Var) in your while loop instead;
You could create a set using 'I' elements, then the variable indexed
over I and solve the model for each 'i' only in your while loop.
Hope these make sense.

Related

What is the difference between mutableStateOf and mutableStateListOf?

While working with a ViewModel and a List stored in there, I usually followed this approach:
var characteristics by mutableStateOf(listOf<Characteristic>())
Then, I could assign data to the List or modify it, and the UI would recompose properly:
characteristics = issuesRepository.getCharacteristics()
characteristics = characteristics.plus(newCharacteristic)
However, I recently stumbled across several approaches containing the keyword mutableStateListOf(), and it then seems to be a common practice to split a List into two separate variables, like this:
private val _characteristic = mutableStateListOf<Characteristic>()
val characteristic: List<Characteristic> = _characteristic
How do these approaches differ, and is one of those considered best-practice or as a cleaner approach?
Using mutableStateOf you are creating an observable object. So, the recomposition will happen just if you assign a new instance to this state.
Let's say that you want that recomposition happens after add a new item to the list. In this case, you need to create a copy of this list, add the elemento to this copied list, and then assign the copied list to the state.
The mutableStateListOf creates an observable list. All operations you've done in this list (add, remove, update) will cause a recomposition.

Yii CActiveRecord with Column Named "attributes"

I used the CRUD generator from a legacy database. When searching for a column value I get the following error:
htmlspecialchars() expects parameter 1 to be string, array given (/usr/local/share/yii/framework/web/helpers/CHtml.php:103)
The problem is that the model has an existing column named "attributes" which is creating a conflict. I removed the entry from the _search.php and commented out all instances in the model hoping to at least get it working but no luck. Any suggestions would be appreciated.
Thanks.
Every CActiveRecord instance (or CModel instance for that matter) has a getter/setter named attributes with which all the attributes can be set. This leads to a conflict because the generated crud code uses the attributes attribute expecting it works as described before.
The controller does something like:
$model->attributes=$_POST['ModelClassName'];
// or
$model->attributes=$_GET['ModelClassName'];
This is meant to set al the (safe) attributes of the model at once. Instead this overwrites the database attribute attributes of your legacy DB model.
This in turn leads to the error you describe, because $_GET['ModelClassName'] and $_POST['ModelClassName'] typically contain arrays of data.
I guess the easiest fix would be to directly call the setter function for the "normal" attributes behavior which would lead to replacing the lines mentioned above with something like the following:
// in the controller
$model->setAttributes($_POST['ModelClassName']);
// and
$model->setAttributes($_GET['ModelClassName']);
I think rest of the generated CRUD code (the views) could and should be left untouched to make it work.
If you want to know how and why this works, it's best to do some research into the __get and __set magic functions and how they're used in the yii framework.

Yii: zii.widgets.CDetailView - declare attrs in model or just in arguments of the widget?

I have a model (let's call it M). This model has a relation (let's call it R) to an other model (X).
By default Gii generates the code which shows R as a numeric ID (primary key in the DB). I want to show it as a hyperlink.
I consider two ways to do it with zii.widgets.CDetailView:
provide extra arguments to $this->widget('zii.widgets.CDetailView');
define method getHyperlink() in my model class and refer to the property as ->hyperlink.
Which of these two ways is better?
The first way may require duplicate code (say in index.php with zii.widgets.CListView generated by Gii).
The second way requires attributeLabels() with new attribute hyperlink which would have the same title as an other (non-hyperlinked numeric) attribute. So I write the same title two times.
So, what of these two variants is better?
Generally, I would think that the second method using the model would be better due to being more DRY.
If you decide that you wanted to use the hyperlink inside of another view, then you would not have to redefine the logic with extra arguments to $this->widget('zii.widgets.CDetailView');

When is it a good idea to give an ADT a name data member?

If I have a Store class, I can write
Store starbucks;
Here, the variable name is the name of the object itself.
Alternatively, I can write
Store shop( "starbucks" );
where a name variable inside Store is initialized with "starbucks". Here, the variable name is generic but the object contains the specific store name.
Which is preferable, and when?
I can't think of a real world use for Store starbucks;. Any time you wanted to add a new store you would have to write all new code and recompile. Outside of test data, for unit tests and whatnot, this should never be used.
For similar reasons, hard coding Store shop( "starbucks" ); is also a bad idea. Again, changing instance data should not cause you to recompile your code.
Most code that I've written uses a combination of user input and a data store to create instances. This is done something like Store shop; shop.load(); or more likely Store shop = storeFactory.getStore();
In addition, I prefer to use the type of the object as the name of the object. This would make the example Store store = storeFactory.getStore();. It lowers the cognitive load of the reader, because they don't have to remember that shop is of type Store.

global variables.Best way to approach this project

I am building a game where a player moves between one of 13 regions - which i defined as a global variable "gsCurrentlyIn" - each round that passes some statistics for that region (eg "Region1") need to be updated - which i have also defined as global variables (eg "giRegion1Pop" for population, "giRegion1PlayerPercent" for popularity) etc.
Now the problem is how to pass the value of where the player currently is to update the appropriately named global variables for that region.
So we have:
gsCurrentlyIn = "Region1"
but we cant build the name of the correct global variable to call because it will be a string not the actual name of the variable.
"gi" + gsCurrentlyIn + "Pop" = 4 wont work.
is there a better way to go about this than the 100-odd global variable way I'm doing it?
I could have huge if/then or case statements for each variable - again that doesnt seem right.
Some people suggested reflection but I found it too confusing. If that is the only way maybe someone can dumb it down for me. But I find it hard to believe that this is that complicated.
Use an array or a collection/dictionary instead of all different variables. The current region is than an number pointing to the proper index in the array
Definitely don't use a multi-dimensional array. Build a class with an property for each of the attributes you track: Population, Popularity, etc. Then use either a collection (ie: List(Of T) )or an array to hold an instance for your class for each region.