convexengineering / gpkit

Geometric programming for engineers
http://gpkit.readthedocs.org
MIT License
206 stars 40 forks source link

JSON Solution Save and Diff Save Format #1552

Open Alexander-Da-Great opened 3 years ago

Alexander-Da-Great commented 3 years ago

A feature I discussed with @pgkirsch.

As noted in the docs, tracking the changes to complex models is aided by the ability to save solutions. In particular, the ability to save solutions via sol.savetxt() has been very useful for analyzing "mid-sized" complex models. However, as the scale of the model increases these txt files can become incredibly cumbersome to read. Pickling solutions and creating diff files to analyze changes has been incredibly useful, but when thousands of variables are changing it is difficult to debug.

Certain data visualization tools within gpkit, such as the reference plots, are very effective for understanding the results of complex models and benefit from json file formats. In an effort to do more with data visualization, I have been parsing diff strings and generating my own json files to use in web apps. It would be great if there were a sol.savejson() option for solution saving and an additional parameter to sol.diff() for json file output.

Potential Avenues for JSON file saving:

JSON diffs:

json_diff_string = self.diff(pickledsol, ..., JSON=true)

json_diff_string = { array(Variables that have changed), array(Variables under Absolute Only), 
                     array(Variables under Relative Only), dict(Absolute Changes), dict(Relative Changes) }

For the diff json string it would be useful to have arrays for the sets of variables that would traditionally appear under the absolute and relative differences section of the diff txt files. The dictionaries to store the changes would ideally contain keys of variable names and values with both the value and unit of the change/variable.

JSON sols:

json_sol_string = sol.savejson()

json_sol_string = { array(Variables), dict(Values) }

For the solutions json files I was thinking we could have something simple like an array of variable names and a dictionary that contains variable name keys and values with the value from the solution as well as its units.

Hopefully this seems like a useful addition to gpkit. I think that it would make gpkit more friendly to web apps and enable more data visualization while developing larger / complex models. I would love to hear more about the implementation of these jsons as well as how to improve / flesh out these ideas. I assume there will be issues with vectorized variables and maybe a few other edge cases.

whoburg commented 3 years ago

I like this idea. Making it easier for web apps to use GPkit is a good thing.

bqpd commented 3 years ago

@Alexander-Da-Great, this looks great! JSON solutions seem like they could be made pretty directly from the solution dictionary with some good use of JSONencoder, but the diffing will require generalizing SolutionArray.diff a bit, adding a function that returns the various differences and changing the diff method to use it.