Data of abstract model resolution - pyomo

I'm starting with pyomo and I have some questions.
I create an abstract model and the correspondenting data file. I would like to solve it in the script in order to use the solutions of the variables later.
I tried to do it with the next code:
data = DataPortal()
data.load(filename="Datos\Datos_reactor2.dat")
instance = model.create_instance(data)
opt = SolverFactory("ipopt")
results = opt.solve(instance)
And it solve it perfectly, but I had some problems with the data. In my model there aren't set, so the data are simple constants defined as:
param qv := 2.832;
param ci := 14.46;
...
However if I use this data file python tell me an error:
ValueError: Parameter 'qv' defined with '1' dimensions, but data has '1' values: [2.832]. Are you missing a value for a 1-dimensional index?
When I change my data file defining the parameters like:
param qv := 1 2.832;
param ci := 1 14.46;
...
I got another error:
RuntimeError: Failed to set value for param=qv, index=1, value=2.832. source error message="Error setting parameter value: Cannot treat the scalar Param 'qv' as an array"
Finally, to can solve the problem I have to define the constants as tables (see beolw), but I don't understand the previous errors.
table qv := 2.832;
table ci := 14.46;
...
To highlight just say that if I try to solve the problem with:
!pyomo solve Reactor2.py Datos_reactor.dat --solver=ipopt --summary
With the data defined as in the first case (param qv := 2.832; ...) I don't have any problem and I got the same solution.
Can anyone tell me the difference and explain me the errors?
Thank you!!
Maria

Related

ArcGIS Python Toolbox Parameter Dependencies

I'm trying to connect two parameters. In the first, the user inputs a file which contains a list (each line is one item). In the second, I'm hoping to set a parameter of type Field or GPValueTable. Here's a look at how this part of the code currently looks like:
def getParameterInfo(self):
#Define parameter definitions
# Input Features parameter
in_features = arcpy.Parameter(
displayName="Input Features",
name="in_features",
datatype="DETextFile",
parameterType="Required",
direction="Input")
# User selection
selection_field = arcpy.Parameter(
displayName="Selection list",
name="selection_field",
datatype="Field",
parameterType="Required",
direction="Input")
selection_field.parameterDependencies = [in_features]
# Derived Output Features parameter
out_features = arcpy.Parameter(
displayName="Output Features",
name="out_features",
datatype="GPFeatureLayer",
parameterType="Derived",
direction="Output")
out_features.parameterDependencies = [in_features.name]
parameters = [in_features, selection_field]
return parameters
The text file looks like this:
A
B
C
The toolbox dialog output is just A. I'm having a hard time understanding what ArcGIS intended to create here. Perhaps I'm using the wrong data types, but their parameter explanation doesn't make it very clear.
Any ideas?
You are confused with the arcpy terminology, I think. The second parameter's datatype="Field" does not imply data can be read/parsed from a text file where the data columns are written, in fact it is the schema/fields of a proper table and expectation is not a simple text but the field objects of a table/feature class (in the toolbox's run-time, assignment to arcpy.Parameter yields a geoprocessing value object but it is a different discussion). If you look at the "Creating value table parameters" section here, GPFeatureLayer's (param0) fields are populated in param1 automatically when the dependency is set.
The solution is setting your selection_field to GPValueTable and populating the selection_field.filters[0].list by reading the in_features's content after the parameter is altered but has not validated in def updateParameters(...). Have a look at https://gis.stackexchange.com/questions/370250/updating-valuetable-parameter-of-arcpy-python-toolbox-tool.

How to use a mutable Param pyomo?

I'm trying to make Param mutable with initializer zero but when I tried to read the Param in the constraint its doesn't understand what is reading, the only way that I found to read is using .value but when the problem finishes declaring the whole constraint. the solver found the first solution but when I defined the new param value, it doesn't change the value of the param. I know if I am doing bad the declared or I haven't found the correct white to do that.
As a solver im using CPLEX
This is the way i defined the Param:
model.s_value = pe.Param(mutable=True, initialize=0)
But if I use the Param as a normal Param is a constraint promo sent this message:
ValueError: Constraint 'def_constraint[0]' encountered a strict inequality expression ('>' or '<'). All constraints must be formulated using using '<=', '>=', or '=='.
I think is because when pyomo tired to read the param that sends an object like this:
pyomo.core.base.param.IndexedParam object at 0x1939C6A0
After declaring all the variable I put the solver inside a for and there I redefine the value of the Param:
model.s_value.value= new_value
Please. Someone can explain to me who can I use correctly the mutable Param and how can I iterate the model.
This is a simple example of changing the value of a mutable parameter. You didn't post a full set of executable code above, so it is difficult to figure out what is going on. If this below doesn't answer your question, request you update (edit) your post above with a minimal reproducible example.
import pyomo.environ as pyo
m = pyo.ConcreteModel()
m.x = pyo.Var(domain=pyo.NonNegativeReals)
m.p = pyo.Param(mutable=True, initialize = 10)
# the problem
m.OBJ = pyo.Objective(expr=m.x)
m.c1 = pyo.Constraint(expr=m.x >= m.p)
solver = pyo.SolverFactory('glpk')
# solve it...
results = solver.solve(m)
m.display() # x=10
# change the mutable parameter
m.p = 5
# re-solve it
results = solver.solve(m)
m.display() # x=5

How to convert list into DataFrame in Python (Binance Futures API)

Using Binance Futures API I am trying to get a proper form of my position regarding cryptocurrencies.
Using the code
from binance_f import RequestClient
request_client = RequestClient(api_key= my_key, secret_key=my_secet_key)
result = request_client.get_position()
I get the following result
[{"symbol":"BTCUSDT","positionAmt":"0.000","entryPrice":"0.00000","markPrice":"5455.13008723","unRealizedProfit":"0.00000000","liquidationPrice":"0","leverage":"20","maxNotionalValue":"5000000","marginType":"cross","isolatedMargin":"0.00000000","isAutoAddMargin":"false"}]
The type command indicates it is a list, however adding at the end of the code print(result) yields:
[<binance_f.model.position.Position object at 0x1135cb670>]
Which is baffling because it seems not to be the list (in fact, debugging it indicates object of type Position). Using PrintMix.print_data(result) yields:
data number 0 :
entryPrice:0.0
isAutoAddMargin:True
isolatedMargin:0.0
json_parse:<function Position.json_parse at 0x1165af820>
leverage:20.0
liquidationPrice:0.0
marginType:cross
markPrice:5442.28502271
maxNotionalValue:5000000.0
positionAmt:0.0
symbol:BTCUSDT
unrealizedProfit:0.0
Now it seems like a JSON format... But it is a list. I am confused - any ideas how I can convert result to a proper DataFrame? So that columns are Symbol, PositionAmt, entryPrice, etc.
Thanks!
Your main question remains as you wrote on the header you should not be confused. In your case you have a list of Position object, you can see the structure of Position in the GitHub of this library
Anyway to answer the question please use the following:
df = pd.DataFrame([t.__dict__ for t in result])
For more options and information please read the great answers on this question
Good Luck!
you can use that
df = pd.DataFrame([t.__dict__ for t in result])
klines=df.values.tolist()
open = [float(entry[1]) for entry in klines]
high = [float(entry[2]) for entry in klines]
low = [float(entry[3]) for entry in klines]
close = [float(entry[4]) for entry in klines]

glmmadmb no longer supported in lsmeans. Alternative packages?

My data is zero inflated so I'm running a zero-inflated model using glmmamdb:
Model3z <- glmmadmb(Count3 ~ Light3 + (1|Site3), zeroInflation = T, family= "poisson", data = dframe3)
However, when I try and do pairwise comparisons of the different light types in this model pwcs3 <- lsmeans(Model3z, "Light") I get the error message:
Error in ref_grid(object, ...) :
Can't handle an object of class “glmmadmb”
Use help("models", package = "emmeans") for information on supported models.
When I go on the emmeans package website it says that glmmadmb is no longer supported.
I've switched to pscl and the zeroinfl function but am unsure on how to restructure my code to fit the pscl format. Typing in P <- zeroinfl(Count3 ~ Light3 + (1|Site3), family = poisson, data = dframe3) gets the error message:
Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
In addition: Warning message:
In Ops.factor(1, Site3) : ‘|’ not meaningful for factors
Is there another way of using glmmadmb with lsmeans? If not, does anyone know how a zero-inflated model code in pscl is supposed to look? Thanks.

TypeError during executemany() INSERT statement using a list of strings

I am trying to just do a basic INSERT operation to a PostgreSQL database through Python via the Psycopg2 module. I have read a great many of the questions already posted regarding this subject as well as the documentation but I seem to have done something uniquely wrong and none of the fixes seem to work for my code.
#API CALL + JSON decoding here
x = 0
for item in ulist:
idValue = list['members'][x]['name']
activeUsers.append(str(idValue))
x += 1
dbShell.executemany("""INSERT INTO slickusers (username) VALUES (%s)""", activeUsers
)
The loop creates a list of strings that looks like this when printed:
['b2ong', 'dune', 'drble', 'drars', 'feman', 'got', 'urbo']
I am just trying to have the code INSERT these strings as 1 row each into the table.
The error specified when running is:
TypeError: not all arguments converted during string formatting
I tried changing the INSERT to:
dbShell.executemany("INSERT INTO slackusers (username) VALUES (%s)", (activeUsers,) )
But that seems like it's merely treating the entire list as a single string as it yields:
psycopg2.DataError: value too long for type character varying(30)
What am I missing?
First in the code you pasted:
x = 0
for item in ulist:
idValue = list['members'][x]['name']
activeUsers.append(str(idValue))
x += 1
Is not the right way to accomplish what you are trying to do.
first list is a reserved word in python and you shouldn't use it as a variable name. I am assuming you meant ulist.
if you really need access to the index of an item in python you can use enumerate:
for x, item in enumerate(ulist):
but, the best way to do what you are trying to do is something like
for item in ulist: # or list['members'] Your example is kinda broken here
activeUsers.append(str(item['name']))
Your first try was:
['b2ong', 'dune', 'drble', 'drars', 'feman', 'got', 'urbo']
Your second attempt was:
(['b2ong', 'dune', 'drble', 'drars', 'feman', 'got', 'urbo'], )
What I think you want is:
[['b2ong'], ['dune'], ['drble'], ['drars'], ['feman'], ['got'], ['urbo']]
You could get this many ways:
dbShell.executemany("INSERT INTO slackusers (username) VALUES (%s)", [ [a] for a in activeUsers] )
or event better:
for item in ulist: # or list['members'] Your example is kinda broken here
activeUsers.append([str(item['name'])])
dbShell.executemany("""INSERT INTO slickusers (username) VALUES (%s)""", activeUsers)