Is there a way to index a parameter in Pyomo without creating a set,through the Param() function? Is it possible to do it via validate for example?
No. You need to create a Set object or use a built in container like a list. E.g.,
m.s = Set(initialize=[1,2,3])
m.p = Param(m.s)
or
m.p = Param([1,2,3])
If you do it the second way, then Pyomo will automatically create a Set object and add it to your model with the name p_index.
Related
Given a pyscipopt.Model and its Solution, how to pass it to another model as a primal heuristic?
I'm currently writing the solution to a file via writeSol(), and then calling readSolFile() and addSol(). There should probably be a cleaner way.
This depends a bit on the structure of your two models. If they have the same variables in the same order (which is likely from what you wrote), then you can simply create a new solution in your model and copy all the values, ie:
variables = othermodel.getVars()
newvariables = model.getVars()
nvars = othermodel.getNVars()
newsol = self.model.createSol(self)
for i in range(nvars):
newsol[newvariables[i]] = othermodel.getSolVal(oldsol, variables[i])
self.model.trySol(newsol)
Let me know if this work/ doens't work
When I querying a Model in Django, I want to create a custom field (using "extra" function) which may use as a sorting field. For example,
result = foo_model.objects.filter(active=True).extra(select={'cal_field': pythonFunction()}).extra(order_by=['cal_field'])
I have 2 questions:
Can I create a field which calling a Python function inside "extra"?
If it can, how can I pass the current row into the function?
Thanks a lot!
I have created a parameter to report values after each instance solve in an iterative program.I wan't my parameter to be indexed by the set that defines the number of iteration and have two other free indexes like so:
model.report=Param(model.iter,[],[])
I then wan't to create a function,that will be called into a while loop and that will create my model,solve an instance and give some values to some variables, whose names will be used as indexes in my report parameter, like so:
report(model.iter,'cost',model.i)=model.cost[i]
Where model.cost is my cost variable indexed by set i.
Is it possible to do that?
You are better off simply making model.report a Python dict. Then you can assign it entries with whatever keys you like:
model.report = dict()
...
for j in range(10):
# assumes model.i is a simple built-in type and not a Param
# (otherwise you would need to use model.i.value)
model.report[j,'cost',model.i] = model.cost[j]
There is no reason to use a Param in this context unless you are using those values in some kind of expression and you want to be able to update the expression at a later time by changing the value of the Param (i.e., you would use mutable=True when you declare the Param).
I need to check if a variable's index exist,while calling a Constraint initialization and if it does not exist i want to set the variable's value to 0.In a python dictionary you can do so with something like that: dict.get('not-a-key',0).Is there something similar for Pyomo objects?
We haven't added this method because one might expect to use it to return a new variable (that would not be owned by the container, because the get method does not modify a dictionary). Perhaps something like the setdefault method would make more sense here, but this is also not something that is currently built into the modeling interface.
One piece of functionality that you might be able to use is that Pyomo will implicitly construct a new variable object at a particular index if that index was added to the variable's indexing set after the initial declaration. Example:
model = ConcreteModel()
model.x_index = Set(initialize=[1])
model.x = Var(model.x_index)
model.x[1] # OK
model.x[2] # KeyError
model.x_index.add(2)
model.x[2] # OK (implicitly creates this object on the fly)
I am testing object coverage for certain reporting solution. I have hundreds of reports and I need to see if set of objects used in those reports covers the set of all possible objects. I figured out that I could use set collection to store distinct object names and then handle it in some way. As I use free version of SOAPui for time being, structure of my test is first invoking method to get XML view of single report, then use Groovy Script to append found object names into a csv file (File append method). However I would like to append those object after I get rid of duplicates. So suitable solution would be a Set variable where I could store object names from all reports and in last step store this set in a file.
How to create such reusable collection? Is there any other way I missed?
You could just declare a set like below
def setOfNames = [] as Set
// set manipulation
setOfNames.add("a")
//
Or just declare a list first, manipulate it, then finally make a set out of it
def listOfNames = []
// list manipulation
def setOfNames = listOfNames as Set
Refer http://groovy.codehaus.org/JN1015-Collections for more details