Skip to content

Instantly share code, notes, and snippets.

@paulk-asert
Created November 22, 2020 12:02
Show Gist options
  • Save paulk-asert/6f4b349f51d8a3e59d56dfefcb2edbff to your computer and use it in GitHub Desktop.
Save paulk-asert/6f4b349f51d8a3e59d56dfefcb2edbff to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "We use the `choco-solver` constraint programming library and `tablesaw-beakerx` to get a nice table output."
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "%%classpath add mvn\norg.choco-solver choco-solver 4.10.5\ntech.tablesaw tablesaw-beakerx 0.38.1",
"execution_count": 11,
"outputs": [
{
"output_type": "display_data",
"data": {
"method": "display_data",
"application/vnd.jupyter.widget-view+json": {
"version_minor": 0,
"model_id": "",
"version_major": 2
}
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "%%latex\nConstraint programming solution for pythagorean triples, i.e. $x^2 + y^2 = z^2$ for $x$, $y$, $z$ less than 30."
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import org.chocosolver.solver.Model\nimport tech.tablesaw.api.*\n\ndef table = Table.create('triples', *['x', 'y', 'z'].collect{ IntColumn.create(it) })\nnew Model('Pythagorean-triple').with {\n def x = intVar(1, 30)\n def y = intVar(1, 30)\n def z = intVar(1, 30)\n y.gt(x).post() // y > x to remove duplicates\n z.gt(y).post() // z > y for efficiency\n x.mul(x).add(y.mul(y)).eq(z.mul(z)).post()\n while (solver.solve()) {\n table.appendRow().with {\n setInt('x', x.value)\n setInt('y', y.value)\n setInt('z', z.value)\n }\n }\n}\n\ntable",
"execution_count": 12,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 12,
"data": {
"text/plain": " triples \n x | y | z |\n--------------------\n 3 | 4 | 5 |\n 6 | 8 | 10 |\n 5 | 12 | 13 |\n 9 | 12 | 15 |\n 8 | 15 | 17 |\n 7 | 24 | 25 |\n 12 | 16 | 20 |\n 10 | 24 | 26 |\n 15 | 20 | 25 |\n 20 | 21 | 29 |\n 18 | 24 | 30 |"
},
"metadata": {}
}
]
}
],
"metadata": {
"kernelspec": {
"name": "groovy",
"display_name": "Groovy",
"language": "groovy"
},
"toc": {
"nav_menu": {},
"number_sections": false,
"sideBar": false,
"skip_h1_title": false,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": false,
"toc_window_display": false
},
"language_info": {
"nbconverter_exporter": "",
"codemirror_mode": "groovy",
"name": "Groovy",
"mimetype": "",
"file_extension": ".groovy",
"version": "2.4.3"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"version_major": 2,
"version_minor": 0,
"state": {
"1c544f94-7be4-4b7b-8084-6dbcefd58af1": {
"model_name": "LayoutModel",
"model_module": "@jupyter-widgets/base",
"model_module_version": "*",
"state": {
"overflow_y": "",
"max_height": "",
"justify_content": "",
"overflow_x": "",
"max_width": "",
"_model_module_version": "*",
"align_self": "",
"overflow": "",
"msg_throttle": 3,
"top": "",
"flex": "",
"height": "",
"min_width": "",
"align_content": "",
"border": "",
"padding": "",
"margin": "",
"bottom": "",
"right": "",
"min_height": "",
"_view_module_version": "*",
"left": ""
}
},
"0040457e-cacd-4381-8fd2-c386f306f9d4": {
"model_name": "LayoutModel",
"model_module": "@jupyter-widgets/base",
"model_module_version": "*",
"state": {
"overflow_y": "",
"max_height": "",
"justify_content": "",
"overflow_x": "",
"max_width": "",
"_model_module_version": "*",
"align_self": "",
"overflow": "",
"msg_throttle": 3,
"top": "",
"flex": "",
"height": "",
"min_width": "",
"align_content": "",
"border": "",
"padding": "",
"margin": "",
"bottom": "",
"right": "",
"min_height": "",
"_view_module_version": "*",
"left": ""
}
},
"fd02f011-fc8d-42d8-8228-3f4f88dc3dde": {
"model_name": "FoldoutModel",
"model_module": "beakerx",
"model_module_version": "*",
"state": {
"font_weight": "",
"visible": true,
"color": null,
"headerLabel": "Added jars: ",
"font_size": "",
"description": "",
"font_style": "",
"layout": "IPY_MODEL_0040457e-cacd-4381-8fd2-c386f306f9d4",
"background_color": null,
"msg_throttle": 3,
"children": [
"IPY_MODEL_35db4d97-7929-4c38-9152-826f206ce852"
],
"disabled": false,
"font_family": "",
"hidePreview": false
}
},
"a0bde121-ddfa-4c9a-b886-52c0720f4744": {
"model_name": "LayoutModel",
"model_module": "@jupyter-widgets/base",
"model_module_version": "*",
"state": {
"overflow_y": "",
"max_height": "",
"justify_content": "",
"overflow_x": "",
"max_width": "",
"_model_module_version": "*",
"align_self": "",
"overflow": "",
"msg_throttle": 3,
"top": "",
"flex": "",
"height": "",
"min_width": "",
"align_content": "",
"border": "",
"padding": "",
"margin": "",
"bottom": "",
"right": "",
"min_height": "",
"_view_module_version": "*",
"left": ""
}
},
"35db4d97-7929-4c38-9152-826f206ce852": {
"model_name": "BxHTMLModel",
"model_module": "beakerx",
"model_module_version": "*",
"state": {
"font_weight": "",
"visible": true,
"color": null,
"font_size": "",
"font_style": "",
"layout": "IPY_MODEL_a0bde121-ddfa-4c9a-b886-52c0720f4744",
"background_color": null,
"msg_throttle": 3,
"font_family": "",
"placeholder": "",
"value": "VectorGraphics2D-0.13.jar, choco-sat-4.10.5.jar, jheaps-0.11.jar, graphics2d-0.27.jar, cpprof-java-1.3.0.jar, commons-logging-1.2.jar, choco-solver-4.10.5.jar, fontbox-2.0.19.jar, trove4j-3.0.3.jar, automaton-1.11-8.jar, xchart-3.6.4.jar, protobuf-java-2.6.1.jar, jgrapht-core-1.4.0.jar, pdfbox-2.0.19.jar, animated-gif-lib-1.4.jar"
}
},
"1ac5bc30-8640-424f-bf4b-44c77967584d": {
"model_name": "LayoutModel",
"model_module": "@jupyter-widgets/base",
"model_module_version": "*",
"state": {
"overflow_y": "",
"max_height": "",
"justify_content": "",
"overflow_x": "",
"max_width": "",
"_model_module_version": "*",
"align_self": "",
"overflow": "",
"msg_throttle": 3,
"top": "",
"flex": "",
"height": "",
"min_width": "",
"align_content": "",
"border": "",
"padding": "",
"margin": "",
"bottom": "",
"right": "",
"min_height": "",
"_view_module_version": "*",
"left": ""
}
},
"5ede4171-d518-44be-8c0d-abdfbe939cf5": {
"model_name": "LayoutModel",
"model_module": "@jupyter-widgets/base",
"model_module_version": "*",
"state": {
"overflow_y": "",
"max_height": "",
"justify_content": "",
"overflow_x": "",
"max_width": "",
"_model_module_version": "*",
"align_self": "",
"overflow": "",
"msg_throttle": 3,
"top": "",
"flex": "",
"height": "",
"min_width": "",
"align_content": "",
"border": "",
"padding": "",
"margin": "",
"bottom": "",
"right": "",
"min_height": "",
"_view_module_version": "*",
"left": ""
}
},
"d8e5ce6f-cf32-4d56-830c-63eda82ddc56": {
"model_name": "FoldoutModel",
"model_module": "beakerx",
"model_module_version": "*",
"state": {
"font_weight": "",
"visible": true,
"color": null,
"headerLabel": "Added jars: ",
"font_size": "",
"description": "",
"font_style": "",
"layout": "IPY_MODEL_5ede4171-d518-44be-8c0d-abdfbe939cf5",
"background_color": null,
"msg_throttle": 3,
"children": [
"IPY_MODEL_db3a7cae-f231-4f77-acfa-b3278bcc0cf2"
],
"disabled": false,
"font_family": "",
"hidePreview": false
}
},
"91f13017-aff9-459c-9c1f-af80d2ee0b0f": {
"model_name": "LayoutModel",
"model_module": "@jupyter-widgets/base",
"model_module_version": "*",
"state": {
"overflow_y": "",
"max_height": "",
"justify_content": "",
"overflow_x": "",
"max_width": "",
"_model_module_version": "*",
"align_self": "",
"overflow": "",
"msg_throttle": 3,
"top": "",
"flex": "",
"height": "",
"min_width": "",
"align_content": "",
"border": "",
"padding": "",
"margin": "",
"bottom": "",
"right": "",
"min_height": "",
"_view_module_version": "*",
"left": ""
}
},
"db3a7cae-f231-4f77-acfa-b3278bcc0cf2": {
"model_name": "BxHTMLModel",
"model_module": "beakerx",
"model_module_version": "*",
"state": {
"font_weight": "",
"visible": true,
"color": null,
"font_size": "",
"font_style": "",
"layout": "IPY_MODEL_91f13017-aff9-459c-9c1f-af80d2ee0b0f",
"background_color": null,
"msg_throttle": 3,
"font_family": "",
"placeholder": "",
"value": "RoaringBitmap-0.8.12.jar, icu4j-65.1.jar, failureaccess-1.0.1.jar, error_prone_annotations-2.3.4.jar, tablesaw-core-0.38.1.jar, univocity-parsers-2.8.4.jar, checker-qual-2.10.0.jar, commons-math3-3.6.1.jar, classgraph-4.8.60.jar, guava-28.2-jre.jar, j2objc-annotations-1.3.jar, slf4j-api-1.7.30.jar, tablesaw-beakerx-0.38.1.jar, listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar, fastutil-8.3.0.jar, jsr305-3.0.2.jar, shims-0.8.12.jar"
}
},
"a7909ac3-ef8f-44f8-96c4-20a3cb9fcd94": {
"model_name": "LayoutModel",
"model_module": "@jupyter-widgets/base",
"model_module_version": "*",
"state": {
"overflow_y": "",
"max_height": "",
"justify_content": "",
"overflow_x": "",
"max_width": "",
"_model_module_version": "*",
"align_self": "",
"overflow": "",
"msg_throttle": 3,
"top": "",
"flex": "",
"height": "",
"min_width": "",
"align_content": "",
"border": "",
"padding": "",
"margin": "",
"bottom": "",
"right": "",
"min_height": "",
"_view_module_version": "*",
"left": ""
}
},
"ddc46a79-921f-4be5-9af3-75d69a94f3d5": {
"model_name": "LayoutModel",
"model_module": "@jupyter-widgets/base",
"model_module_version": "*",
"state": {
"overflow_y": "",
"max_height": "",
"justify_content": "",
"overflow_x": "",
"max_width": "",
"_model_module_version": "*",
"align_self": "",
"overflow": "",
"msg_throttle": 3,
"top": "",
"flex": "",
"height": "",
"min_width": "",
"align_content": "",
"border": "",
"padding": "",
"margin": "",
"bottom": "",
"right": "",
"min_height": "",
"_view_module_version": "*",
"left": ""
}
},
"4d5235e5-1d29-41f5-b33d-c97b2d650ed9": {
"model_name": "LayoutModel",
"model_module": "@jupyter-widgets/base",
"model_module_version": "*",
"state": {
"overflow_y": "",
"max_height": "",
"justify_content": "",
"overflow_x": "",
"max_width": "",
"_model_module_version": "*",
"align_self": "",
"overflow": "",
"msg_throttle": 3,
"top": "",
"flex": "",
"height": "",
"min_width": "",
"align_content": "",
"border": "",
"padding": "",
"margin": "",
"bottom": "",
"right": "",
"min_height": "",
"_view_module_version": "*",
"left": ""
}
},
"ee10adc6-0c83-4bd5-a258-1d62ac73fa29": {
"model_name": "LayoutModel",
"model_module": "@jupyter-widgets/base",
"model_module_version": "*",
"state": {
"overflow_y": "",
"max_height": "",
"justify_content": "",
"overflow_x": "",
"max_width": "",
"_model_module_version": "*",
"align_self": "",
"overflow": "",
"msg_throttle": 3,
"top": "",
"flex": "",
"height": "",
"min_width": "",
"align_content": "",
"border": "",
"padding": "",
"margin": "",
"bottom": "",
"right": "",
"min_height": "",
"_view_module_version": "*",
"left": ""
}
}
}
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment