Git Product home page Git Product logo

bryan-lunt / snot_lang Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 1.0 87 KB

Simple Numerical Object Transcription (SNOT) is a language similar to JSON meant for numerical computing.

License: MIT License

Makefile 1.35% C++ 90.51% ANTLR 0.71% M4 0.61% Python 2.10% R 2.64% Dockerfile 0.35% Shell 0.54% CMake 1.19%
numerical-optimization numerical-modelling numerical-methods numerical-analysis language snot json json-parser

snot_lang's Introduction

SNOT_lang

Simple Numerical Object Transcription (SNOT) is a language similar to JSON meant for numerical computing.

PURPOSE

Nearly all optimizers expect parameters in a vector, array, or list type. They expect to optimize a scalar valued function with a vector input.

val = func(x_0,x_1,x_2,...; some_other_non_parameter_arguments)

usually as:

val = func(double[] in ; some_other_non_parameter_arguments)

However, when functions or models become more complicated, it may become difficult for programmers to keep track of where in the array each argument is. This is much more difficult when the program in question creates the model/function dynamically. In these cases, it may simply not be known in advance which position in the array contains which argument. It needs to be found dynamically at runtime.

It would be very nice if the code within func(...) can access/view the parameters in a meaningful hierarchical structure.

In the experience of this author, people end up rolling their own hard-coded datatypes / data structures for this. This can become unwieldy, and it may make future maintenance difficult, in particular when new parameters are introduced.

It would be nice to create the structure dynamically (think like Python dictionaries/lists or a JSON object.) then be able to move back and forth between an easily viewable/usable hierarchical structure and a vector representation. Code within the objective function can be written cleanly, while the optimizer gets parameters in its preferred view.

THIS PROJECT

This project implements a C++98 compliant, header-only dynamic data structure that addresses precisely this problem. The code is licensed under an MIT license, so you can simply copy the header param_storage.h into your optimization project and use it.

The API is largely STL/C++ like, with an object gsparams::DictList that can store one of three things:

  • A primitive value. This can currently only be a double, but you can change the typedef and use whatever, including a struct or union.
  • A list. The list can contain any of these three types.
  • A dictionary mapping std::string to values (any of these three types.). This dictionary preserves the order in which objects are added to it.

The key thing is that the dictionary preserves the order. This allows traversals to go between vector and structured representation. A traversal and an iterator are implemented/included.

EXAMPLES

You can create a structure dynamically.

    DictList top_dict;

    DictList tfs;

    DictList a_tf;
    DictList b_tf;

    a_tf.push_back(1.23);
    a_tf.push_back(1.24);
    a_tf.push_back(1.25);
    tfs["A"] = a_tf;

    b_tf.push_back(2.1);
    b_tf.push_back(2.2);
    b_tf.push_back(2.3);


    tfs["B"] = b_tf;

    top_dict["tfs"] = tfs;

Which is a structure equivalent to:

{"tfs":{
    "A":[1.23,1.24,1.25],
    "B":[2.1,2.2,2.3]
    }
}

Now, you can access values out of that like this:

    top_dict["tfs"]["A"][0] = 1.2345; //Should only do this once it has a zeroth element.

    double foobar = 5.0*top_dict["tfs"]["A"][0];
    dictlist_primitive_t foobaz = 3.3/top_dict["tfs"]["A"][0];

Here's an example (from example1.cpp) where it is used inside a Rosenbrock function. It is just a simple example, but you can imagine functions with a much more complicated set of parameters.

return pow(a-tmpvals["x"],2.0) + b*pow(tmpvals["y"]-pow(tmpvals["x"],2.0),2.0);

Of course you could convert them to something more static if you feel that the subscripting is too slow, perhaps in very tight-looped numerical functions. (However, a branch predicting complier probably makes this unnecessary. Don't optimize too early!)

    double x = tmpvals["x"];
    double y = tmpvals["y"];

    return some_iterated_function_that_uses_x_and_y_many_times(x,y);

Bibliography

The SNOT grammar was adapted from the JSON grammer available here: https://gist.github.com/justjkk/436828/

I benefit from this tutorial: https://www.usna.edu/Users/cs/lmcdowel/courses/si413/F10/labs/L04/calc1/ex1.html (and parts of the code come from there, with heavy modifications.)

In future versions, I hope to benefit from this tutorial and make the grammar create better C++ code. https://www.gnu.org/software/bison/manual/html_node/A-Complete-C_002b_002b-Example.html

https://github.com/linse/flex-bison-cpp-example

https://www.gnu.org/software/automake/manual/html_node/Yacc-and-Lex.html

The ANTLR4 grammar was cut down from the JSON grammar at https://raw.githubusercontent.com/antlr/grammars-v4/master/json/JSON.g4

snot_lang's People

Contributors

bryan-lunt avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

uiucsinhalab

snot_lang's Issues

Create a pretty printer in param_storage.h .

Create a pretty-printer utility function.
It could be a static function or a member function, either way. I suppose I prefer a static function for some reason.
Do not alter the existing operator<<

HDF5 support / standard

Currently any storage of one or more SNOT vectors and the associated structure into HDF5 is done on an ad-hoc basis.

It might be useful to:

  • Establish a standard way to store SNOT objects/structure into HDF5.
  • Implement some code to do this easily.

(Isn't HDF5 extensible? Even if not, a function/object that can be pointed at an HDF5 subobject would be useful.)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.