Interpretation of string as equation?

Rafid
2012-06-29
2013-04-11
  • Rafid

    Rafid - 2012-06-29

    I using Python to read equations and variables from a txt file for so to use CasADi to output the derivatives in another txt.
    However, I'm running into an annoying problem where my equations are interpreted by Python as strings(naturally), which means that CasADi is not able to interpret them as equations. Is there a solution to this? Is it possible to do some sort of type conversion when importing from the txt file?

    Thanks!

     
  • Joel Andersson

    Joel Andersson - 2012-06-29

    Hello!

    I don't quite understand what you are trying to do. You are trying to import equations from a text file? And are they equations (a = b) or just expressions?

    What is the format of this text file? And then you are outputting them to another file, but why as Python string? Remember that if you just print an expression using Python's print-command, it will not show how the expression will be actually calculated by CasADi's interpretors. A better idea is to generate C-code from the expressions.

    Joel

     
  • Rafid

    Rafid - 2012-07-02

    This is what I'm trying to do.

    Input txt:
    f: x**2+7y*sin(x)+z
    v: x
    v: y
    v: z

    Output txt:
    diff(f,x): ((x+x)+(cos(x)*(7*y)))
    diff(f,y): (7*sin(x))
    diff(f,z): 1

    But in my Python script I'm not able to feed casadi the function as anything other than a string.

    I'll look into using C , because the software that this utility will ultimately be implemented in is written in C++.

    Thanks

     
  • Joel Andersson

    Joel Andersson - 2012-07-02

    If you want to read in expression of this form from a text file, you would have to write the parser yourself, for example in Python. Should be trivial. Just start by reading all the variables:

    x = ssym("x")
    y = ssym("y")
    z = ssym("z")
    

    Then form the expression by "executing" the string, see e.g. .

    Then perform whatever operation that you want to perform on the expression.

    Then output the new expressions in the form above by simple printing. This is not efficient if you want to actually use the output for calculations, then you have to exploit all the common subexpressions. One way of doing that is to output C-code, and parse the C-code… Or modify the C-code generator in CasADi…

    Anyway, I think you should really ask yourself if you want to parse text-files of a format like this. I see no sense in it. Your expression already contain Python-code (** for power for example) - why not make it proper Python scripts?

    Alternatively, you can use a standard format (not text-files) for representing expressions based on either XML or the .nl format from AMPL.

    Finally, maybe CasADi isn't the software you're looking for. If you just want to differentiate small expressions, why not use something like SymPy? CasADi is for constructing very large expressions with millions of intermediate variables for fast execution, not for small expressions for pretty-printing.

    Kind regards,
    Joel

     
  • Rafid

    Rafid - 2012-07-02

    Thanks for the link! That's what I was missing in my code, because I have the rest of it.

    Also thank you for your recommendation of SymPy. I did actually build a prototype using SymPy which went very smoothly. However, my supervisor wants me to use CasADi because it can be integrated with IPOPT which is one of the solvers used in the Optimizer.

    Do you happen to know if IPOPT is friendly with SymPy?

    Again thanks for the help, I really appreciate it

     
  • Joel Andersson

    Joel Andersson - 2012-07-02

    Hello!

    IPOPT is integrated with CasADi and the link is very efficient. You basically do not have to worry about any derivatives, CasADi forms expressions for these in an efficient way and passes these directly to IPOPT.

    I don't think there is anything similar for SymPy.

    So, in the end you want to solve an optimization problem? Then why do you want to work with textfiles at all?

    Regards,
    Joel

     
  • Rafid

    Rafid - 2012-07-02

    I'm working with txt files right now only to do the main coding and because I'm new to Python. The optimizer uses XML so I'll be changing the code to input and output xml files.

     
  • Joel Andersson

    Joel Andersson - 2012-07-02

    But these text files are generated from somewhere, right?

     
  • Rafid

    Rafid - 2012-07-02

    The input file is user generated. Eventually it will just pull data from the optimizer xml file with all the info.

     
  • Joel Andersson

    Joel Andersson - 2012-07-02

    So you have basically created your own modelling language and now you are writing a parser for it… Are you sure you want to make your life so hard? In which language is the optimizer written? Can't you just generate CasADi-code instead? Or even better, CasADi expressions?

     
  • Rafid

    Rafid - 2012-07-02

    The optimizer is written in C++. What you are saying does seem more straightforward by cutting out the redundant middle step. The main reason I'm doing it in Python is because I'm suppose to just make a prototype that will be easy for the main developer to implement in the optimizer in C++. Also I'm not familiar with C++.

     
  • Joel Andersson

    Joel Andersson - 2012-07-02

    OK. Since CasADi is written completely in C++, I think that the easiest solution by far is not generate any code or text files, simply to build up the expressions from inside the C++. CasADi's C++ front-end is very similar to the Python front-end, since the Python front-end is automatically generated from the C++ header files.

     

Log in to post a comment.