How can I get the value of a gurobi variable? - python

How can I access only to the value of the variable?
Example:
When I print forces[0,1], I obtain: gurobi.Var forces[0,1] (value -0.6)
How can access only to the value of the variable: -0.6?
Thanks in advance

I assume you are using python?
If forces[0,1] is a Gurobi variable-object, and a value is available, you can access the value (more precise: current solution) with:
forces[0,1].X
This is of course explained in the docs. Look for variable attributes! (.X)
(Above links are for Gurobi's Python-API; look up the corresponding docs for other APIs if needed)

Related

Python data structure like dict

Online I found some code that used the variable explored.
This variable is implemented like a dictionary: explored = {rootId}, but it doesn't have key-value association, moreover this variabile uses the function .add(elem) while the dict doesn't have this function.
eg. explored.add(elem).
Which data structure is that? (I'm using python 3.6.3)
Might be a set, it has a method "add", but you could simply print the type, such as:
print(type(explored))
It's a set. There are also online examples that use the methods you mentioned.
Next time you wonder, I suggest you just print the type, like this:
print(type(explored));
It's the set datastructure.
Here's a link to it on the official docs: https://docs.python.org/3/tutorial/datastructures.html#sets

How to find the index value of a variable in SPSS Python

Is there a way to find the index of a variable in SPSS Python?
For example, if one of my variables in the SPSS dataset is ID
usually, I would be able to access the variable with the following code:
varObj = datasetObj.varlist[0]
Assuming that ID is the first column in my dataset.
But what if the variable ID is lost somewhere in the middle of a dataset?
Is there a way for me to find the index value of the variable ID?
Thank you very much for your help.
From the documentation of the Variable Class, you can get a reference to the variable by name or by index:
# Create a Variable object, specifying the variable by name
varObj = datasetObj.varlist['bdate']
# Create a Variable object, specifying the variable by index
varObj = datasetObj.varlist[3]
So in your case:
varObj = datasetObj.varlist['ID']
You can, if needed, get the index of the variable by its name, using the index property:
varIndex = datasetObj.varlist['ID'].index
Note also that you can use the spssaux.VariableDict class to get and set (except for type) all the properties of variables.
Also, all the doc for the programmability apis is available under Help > Programmability, and you may find the Programming and Data Management book (pdf) downloadable from the SPSS Community (old) or new Predictive Analytics website at https://developer.ibm.com/predictiveanalytics/

Alternatives for exec to get values of any variable

For debugging purposes I need to know the values of some variables in Python. But, I don't want create a dictionary with all variables and don’t want to add every new variable that I want to test at some point to a dictionary, especially when it comes to lists and their content, which is often procedurally generated.
So is there an alternative to just taking input as a string and then executing it by exec() or using print() and hardcoded variables?
Yes, there is a way. You can use the locals() function to get a dictionary of all variables. For example:
a=5
b=locals()["a"]
Now b will get the value of a, i.e. 5.
However, while you can do this doesn't mean you should do this. There may be something wrong in the structure of your program if you want to access variables by using their name stored in a string.

Return the variable value as string in python

Suppose I have two variables:
_first = True
_second = _first
print (getValue(_second))
>>> _first
Is there a way to implement getValue(or anything else) in that way ? I don't care about the real value (boole\int\string etc..) , just the name of the assigned variable.
Thanks
Variables don't refer to each other. When you use _second = _first, you aren't making _second refer to _first, you're making it refer to the value that _first refers to. This might clear some things up: Facts and Myths about Names and Values in Python.
No; once you've assigned _first to _second, there is no longer any connection between _second and where it got its value from (as opposed to the value itself).
you cannot print out the variable's name in such a way.
But you might try using Dictionary so that you can have a key-value pair for each entry.
That way you can find the values corresponding to certain names as keys and get the key names as well.
I hope that helps you.
to get help on dictionaries you can refer:
http://www.tutorialspoint.com/python/python_dictionary.htm

django-modeltranslation : how to know if the value is defined in a given language?

I am using django-modeltranslation for translation of my content.
If the value of a translated field is not set for a language then it automatically takes the value of the default language.
However, is there a way to know if value for a given language has been set?
If for example title_fr is not set, obj.title_fr will return the value of obj.title.
How to know if the french version has been defined?
You can access original field value with instance.__dict__['title_fr'].
However, you probably want to customize fallback_values option:
https://django-modeltranslation.readthedocs.org/en/latest/usage.html#fallback-values

Categories

Resources