0

A mathematica program returns as an output a sum of cardinal sines. The first element of the vector is

-19.9959 Sinc[0.0418879 (0. + t)] Sinc[0.0897598 (-65. + u)]

The variable is saved in a text file; however, this has to be read in pyomo as a variable, so a StringReplace is used to adapt this variable to python's grammmar

savedXPython = 
 Import["savedWindX.txt"] // 
  StringReplace[#, {"[" -> "(", "]" -> ")", 
     "t" -> "m.lammda[i]*180/np.pi", "u" -> "m.phi[i]*180/np.pi"}] &

Then, savedXPython was saved to another text file. However, an error appeared while working with pyomo; I asked here and the answer was to save the result in a json file instead of a text.

Export["savedWindXPython.txt", savedXPython];
Export["savedWindXPythonJ.json", savedXPython, "ExpressionJSON"];

Now, in the pyomo part, the text file was originally read as

g = open("savedWindXPython.txt","r")
b=f.readline()
g.close

later on, following this thread, the json has been read as

f = open("savedWindXPythonJ.json","r")
a=f.readline()
f.close

And then, the variable inside the pyomo code is defined as

def Wind_lammda_definition(model, i):
    return m.Wind_lammda[i] == a
m.Wind_lammda_const = Constraint(m.N, rule = Wind_lammda_definition)

in the case of the json file or def Wind_lammda_definition(model, i): return m.Wind_lammda[i] == b m.Wind_lammda_const = Constraint(m.N, rule = Wind_lammda_definition

in the case of the original text file

The code however doesn't work. AttributeError: 'str' object has no attribute 'is_relational', the error which prevented me from just reading the variable from the text file also appears in the json case.

It seems that using the json format has not helped. Can someone tell me if the json implementation has been done wrong?

slow_learner
  • 337
  • 1
  • 2
  • 15

1 Answers1

0

When reading a line from your file, Pyhton will always return a string. If 1 is the only content in your line, the returned value will be equal to "1" and not 1. That can be solved with a = float(a), since you want to use a numeric value in your constraint. This will simply convert your a string into a float.

V. Brunelle
  • 1,038
  • 11
  • 29
  • I am afraid that thist doesn't answer my problem. I cannot just convert what is inside of the .txt file to a float and then proceed. `m.Wind_lammda[i]` is not a float, but a `pyomo.core.base.var._GeneralVarData`. If I attempt to convert to a float, an error appears; ` ValueError: could not convert string to float:` I need a way to read GeneralVarData from a file. – slow_learner Feb 03 '20 at 09:42