Initial commit
This commit is contained in:
parent
ce004720a9
commit
ee6af3ef1a
|
|
@ -0,0 +1,104 @@
|
||||||
|
* ob-lp_solve
|
||||||
|
Evaluate Mixed Integer Linear Programming LP-format model with LP-solve
|
||||||
|
|
||||||
|
** Installation
|
||||||
|
|
||||||
|
The package is not (yet) available on MELPA, so it should be installed directly from the repository.
|
||||||
|
|
||||||
|
*** Dependencies
|
||||||
|
|
||||||
|
To be able to use =ob-lp_solve= you will need to install [[https://github.com/lp-solve/lp_solve][lp_solve]] and make sure it is on your =PATH=.
|
||||||
|
|
||||||
|
*** Installation with Doomemacs
|
||||||
|
|
||||||
|
In your =$DOOMDIR/packages.el= file, you might want to set up the following:
|
||||||
|
#+begin_src emacs-lisp
|
||||||
|
(package! ob-lp_solve
|
||||||
|
:recipe (:host github :repo "samuelortion/ob-lp_solve"))
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
*** Installation with Straight
|
||||||
|
|
||||||
|
#+begin_src emacs-lisp
|
||||||
|
(use-package ob-pasp
|
||||||
|
:straight (ob-pasp :type git :host github :repo "samuelortion/lp_solve"))
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
** Load for testing :noexport:
|
||||||
|
|
||||||
|
#+begin_src emacs-lisp
|
||||||
|
(load "~/Documents/projects/emacs/ob-lp_solve/ob-lp_solve.el")
|
||||||
|
(add-to-list 'org-babel-load-languages '(lp_solve . t))
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+RESULTS:
|
||||||
|
: ((lp-solve . t) (lp_solve . t) (emacs-lisp . t))
|
||||||
|
|
||||||
|
** Some basic examples
|
||||||
|
|
||||||
|
#+begin_src lp_solve :exports both
|
||||||
|
max: a + b;
|
||||||
|
r_1: a + b < 2;
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+RESULTS:
|
||||||
|
:
|
||||||
|
: Value of objective function: 2.00000000
|
||||||
|
:
|
||||||
|
: Actual values of the variables:
|
||||||
|
: a 1
|
||||||
|
: b 1
|
||||||
|
|
||||||
|
*** Linear programming Vertex cover problem relaxation
|
||||||
|
|
||||||
|
#+begin_src lp_solve :exports both
|
||||||
|
min: a + b + c + d + e;
|
||||||
|
|
||||||
|
r_1: a + c >= 1;
|
||||||
|
r_2: d + e >= 1;
|
||||||
|
r_3: c + b >= 1;
|
||||||
|
r_4: a + e >= 1;
|
||||||
|
r_5: a + d >= 1;
|
||||||
|
|
||||||
|
bin a;
|
||||||
|
bin b;
|
||||||
|
bin c;
|
||||||
|
bin d;
|
||||||
|
bin e;
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+RESULTS:
|
||||||
|
:
|
||||||
|
: Value of objective function: 3.00000000
|
||||||
|
:
|
||||||
|
: Actual values of the variables:
|
||||||
|
: a 1
|
||||||
|
: b 0
|
||||||
|
: c 1
|
||||||
|
: d 0
|
||||||
|
: e 1
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src dot :file vertex-cover-example.svg :results file graphics :exports output
|
||||||
|
|
||||||
|
graph G {
|
||||||
|
{
|
||||||
|
node [width=0.25 shape=circle style=filled]
|
||||||
|
a [fillcolor=yellow]
|
||||||
|
b
|
||||||
|
c [fillcolor=yellow]
|
||||||
|
d
|
||||||
|
e
|
||||||
|
}
|
||||||
|
a -- c
|
||||||
|
c -- b
|
||||||
|
d -- e
|
||||||
|
a -- e
|
||||||
|
a -- d
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+RESULTS:
|
||||||
|
[[file:vertex-cover-example.svg]]
|
||||||
|
|
||||||
|
|
@ -0,0 +1,182 @@
|
||||||
|
;;; ob-lp_solve.el --- org-babel functions for lp_solve Mixed Integer Linear Programming evaluation -*- lexical-binding: t; -*-
|
||||||
|
;;
|
||||||
|
;; Copyright (C) 2025 Samuel ORTION
|
||||||
|
;;
|
||||||
|
;; Author: Samuel ORTION <samuel@ortion.fr>
|
||||||
|
;; Maintainer: Samuel ORTION <samuel@ortion.fr>
|
||||||
|
;; Created: November 01, 2025
|
||||||
|
;; Modified: November 01, 2025
|
||||||
|
;; Version: 0.0.1
|
||||||
|
;; Keywords: "integer linear programming" lp-solve
|
||||||
|
;; Homepage: https://github.com/sortion/ob-lp-solve
|
||||||
|
;; Package-Requires: ((emacs "26.1"))
|
||||||
|
;;
|
||||||
|
;; ;; This program is free software; you can redistribute it and/or modify
|
||||||
|
;; it under the terms of the GNU General Public License as published by
|
||||||
|
;; the Free Software Foundation; either version 3, or (at your option)
|
||||||
|
;; any later version.
|
||||||
|
;;
|
||||||
|
;; This program is distributed in the hope that it will be useful,
|
||||||
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
;; GNU General Public License for more details.
|
||||||
|
;;
|
||||||
|
;; You should have received a copy of the GNU General Public License
|
||||||
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
||||||
|
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||||
|
;; Boston, MA 02110-1301, USA.
|
||||||
|
;
|
||||||
|
;;; Commentary:
|
||||||
|
;;
|
||||||
|
;; A Org-babel
|
||||||
|
;;
|
||||||
|
;;; Code:
|
||||||
|
|
||||||
|
;; After that continue by creating a simple code block that looks like e.g.
|
||||||
|
;;
|
||||||
|
;; #+begin_src pasp
|
||||||
|
;;
|
||||||
|
;; parent(vader, luke).
|
||||||
|
;; child(C, P) :- parent(P, C).
|
||||||
|
;; #show child/2.
|
||||||
|
;;
|
||||||
|
;; #+end_src
|
||||||
|
|
||||||
|
;; Finally you can use `edebug' to instrumentalize
|
||||||
|
;; `org-babel-expand-body:lp_solve' and continue to evaluate the code block. You
|
||||||
|
;; try to add header keywords and change the body of the code block and
|
||||||
|
;; reevaluate the code block to observe how things get handled.
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; If you have questions as to any of the portions of the file defined
|
||||||
|
;; below please look to existing language support for guidance.
|
||||||
|
;;
|
||||||
|
;; If you are planning on adding a language to org-babel we would ask
|
||||||
|
;; that if possible you fill out the FSF copyright assignment form
|
||||||
|
;; available at https://orgmode.org/request-assign-future.txt as this
|
||||||
|
;; will make it possible to include your language support in the core
|
||||||
|
;; of Org-mode, otherwise unassigned language support files can still
|
||||||
|
;; be included in the contrib/ directory of the Org-mode repository.
|
||||||
|
|
||||||
|
;;; Requirements:
|
||||||
|
|
||||||
|
;;; Code:
|
||||||
|
(require 'ob)
|
||||||
|
(require 'ob-ref)
|
||||||
|
(require 'ob-comint)
|
||||||
|
(require 'ob-eval)
|
||||||
|
;; possibly require modes required for your language
|
||||||
|
|
||||||
|
;; optionally define a file extension for this language
|
||||||
|
; (add-to-list 'org-babel-tangle-lang-exts '("lp_solve" . "lp_solve"))
|
||||||
|
|
||||||
|
;; optionally declare default header arguments for this language
|
||||||
|
(defvar org-babel-default-header-args:lp_solve '())
|
||||||
|
|
||||||
|
;; This function expands the body of a source code block by doing things like
|
||||||
|
;; prepending argument definitions to the body, it should be called by the
|
||||||
|
;; `org-babel-execute:lp_solve' function below. Variables get concatenated in
|
||||||
|
;; the `mapconcat' form, therefore to change the formatting you can edit the
|
||||||
|
;; `format' form.
|
||||||
|
(defun org-babel-expand-body:lp_solve (body params &optional processed-params)
|
||||||
|
"Expand BODY according to PARAMS, return the expanded body."
|
||||||
|
(require 'inf-lp_solve nil t)
|
||||||
|
(let ((vars (org-babel--get-vars (or processed-params (org-babel-process-params params)))))
|
||||||
|
(concat
|
||||||
|
(mapconcat ;; define any variables
|
||||||
|
(lambda (pair)
|
||||||
|
(format "%s=%S"
|
||||||
|
(car pair) (org-babel-lp_solve-var-to-lp_solve (cdr pair))))
|
||||||
|
vars "\n")
|
||||||
|
"\n" body "\n")))
|
||||||
|
|
||||||
|
;; This is the main function which is called to evaluate a code
|
||||||
|
;; block.
|
||||||
|
;;
|
||||||
|
;; This function will evaluate the body of the source code and
|
||||||
|
;; return the results as emacs-lisp depending on the value of the
|
||||||
|
;; :results header argument
|
||||||
|
;; - output means that the output to STDOUT will be captured and
|
||||||
|
;; returned
|
||||||
|
;; - value means that the value of the last statement in the
|
||||||
|
;; source code block will be returned
|
||||||
|
;;
|
||||||
|
;; The most common first step in this function is the expansion of the
|
||||||
|
;; PARAMS argument using `org-babel-process-params'.
|
||||||
|
;;
|
||||||
|
;; Please feel free to not implement options which aren't appropriate
|
||||||
|
;; for your language (e.g. not all languages support interactive
|
||||||
|
;; "session" evaluation). Also you are free to define any new header
|
||||||
|
;; arguments which you feel may be useful -- all header arguments
|
||||||
|
;; specified by the user will be available in the PARAMS variable.
|
||||||
|
(defun org-babel-execute:lp_solve (body params)
|
||||||
|
"Execute a block of lp_solve LP-format code with org-babel.
|
||||||
|
This function is called by `org-babel-execute-src-block'"
|
||||||
|
(message "executing Lp_Solve source code block")
|
||||||
|
(let* ((processed-params (org-babel-process-params params))
|
||||||
|
;; set the session if the value of the session keyword is not the
|
||||||
|
;; string `none'
|
||||||
|
;(session (unless (string= value "none")
|
||||||
|
; (org-babel-pasp-initiate-session
|
||||||
|
; (cdr (assq :session processed-params)))))
|
||||||
|
;; set the -n option to ask for more models
|
||||||
|
(models (cdr (assoc :n processed-params)))
|
||||||
|
;; variables assigned for use in the block
|
||||||
|
(vars (org-babel--get-vars processed-params))
|
||||||
|
(result-params (assq :result-params processed-params))
|
||||||
|
;; either OUTPUT or VALUE which should behave as described above
|
||||||
|
(result-type "output"); (assq :result-type processed-params))
|
||||||
|
;; expand the body with `org-babel-expand-body:pasp'
|
||||||
|
(full-body (org-babel-expand-body:lp_solve
|
||||||
|
body params processed-params))
|
||||||
|
(temp-file (org-babel-temp-file "lp_solve-"))
|
||||||
|
(lp_solve (executable-find "lp_solve"))
|
||||||
|
(cmd (concat (shell-quote-argument (expand-file-name lp_solve))
|
||||||
|
" " (org-babel-process-file-name temp-file))))
|
||||||
|
;; actually execute the source-code block either in a session or
|
||||||
|
;; possibly by dropping it to a temporary file and evaluating the
|
||||||
|
;; file.
|
||||||
|
;;
|
||||||
|
;; for session based evaluation the functions defined in
|
||||||
|
;; `org-babel-comint' will probably be helpful.
|
||||||
|
;;
|
||||||
|
;; for external evaluation the functions defined in
|
||||||
|
;; `org-babel-eval' will probably be helpful.
|
||||||
|
;;
|
||||||
|
;; when forming a shell command, or a fragment of code in some
|
||||||
|
;; other language, please preprocess any file names involved with
|
||||||
|
;; the function `org-babel-process-file-name'. (See the way that
|
||||||
|
;; function is used in the language files)
|
||||||
|
|
||||||
|
;; ref. https://github.com/arnm/ob-mermaid
|
||||||
|
(unless (file-executable-p lp_solve)
|
||||||
|
(error "Cannot find or execute `lp_solve', please check it is installed and in PATH"))
|
||||||
|
(with-temp-file temp-file (insert full-body))
|
||||||
|
(message "%s" cmd)
|
||||||
|
(org-babel-eval cmd "")
|
||||||
|
))
|
||||||
|
|
||||||
|
;; This function should be used to assign any variables in params in
|
||||||
|
;; the context of the session environment.
|
||||||
|
(defun org-babel-prep-session:lp_solve (session params)
|
||||||
|
"Prepare SESSION according to the header arguments specified in PARAMS."
|
||||||
|
)
|
||||||
|
|
||||||
|
(defun org-babel-lp_solve-var-to-lp_solve (var)
|
||||||
|
"Convert an elisp var into a string of pasp source code
|
||||||
|
specifying a var of the same value."
|
||||||
|
(format "%S" var))
|
||||||
|
|
||||||
|
(defun org-babel-lp_solve-table-or-string (results)
|
||||||
|
"If the results look like a table, then convert them into an
|
||||||
|
Emacs-lisp table, otherwise return the results as a string."
|
||||||
|
)
|
||||||
|
|
||||||
|
(defun org-babel-lp_solve-initiate-session (&optional session)
|
||||||
|
"If there is not a current inferior-process-buffer in SESSION then create.
|
||||||
|
Return the initialized session."
|
||||||
|
(unless (string= session "none")
|
||||||
|
))
|
||||||
|
|
||||||
|
(provide 'ob-lp_solve)
|
||||||
|
;;; ob-lp_solve.el ends here
|
||||||
|
|
@ -0,0 +1,68 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
|
||||||
|
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
||||||
|
<!-- Generated by graphviz version 2.42.4 (0)
|
||||||
|
-->
|
||||||
|
<!-- Title: G Pages: 1 -->
|
||||||
|
<svg width="120pt" height="189pt"
|
||||||
|
viewBox="0.00 0.00 120.06 188.89" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||||
|
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 184.89)">
|
||||||
|
<title>G</title>
|
||||||
|
<polygon fill="white" stroke="transparent" points="-4,4 -4,-184.89 116.06,-184.89 116.06,4 -4,4"/>
|
||||||
|
<!-- a -->
|
||||||
|
<g id="node1" class="node">
|
||||||
|
<title>a</title>
|
||||||
|
<ellipse fill="yellow" stroke="black" cx="71.38" cy="-163.22" rx="17.86" ry="17.86"/>
|
||||||
|
<text text-anchor="middle" x="71.38" y="-159.52" font-family="Times,serif" font-size="14.00">a</text>
|
||||||
|
</g>
|
||||||
|
<!-- c -->
|
||||||
|
<g id="node3" class="node">
|
||||||
|
<title>c</title>
|
||||||
|
<ellipse fill="yellow" stroke="black" cx="18.38" cy="-91.15" rx="16.94" ry="16.94"/>
|
||||||
|
<text text-anchor="middle" x="18.38" y="-87.45" font-family="Times,serif" font-size="14.00">c</text>
|
||||||
|
</g>
|
||||||
|
<!-- a--c -->
|
||||||
|
<g id="edge1" class="edge">
|
||||||
|
<title>a--c</title>
|
||||||
|
<path fill="none" stroke="black" d="M61.17,-148.71C51.66,-136.14 37.59,-117.55 28.21,-105.15"/>
|
||||||
|
</g>
|
||||||
|
<!-- d -->
|
||||||
|
<g id="node4" class="node">
|
||||||
|
<title>d</title>
|
||||||
|
<ellipse fill="lightgrey" stroke="black" cx="71.38" cy="-91.15" rx="18.27" ry="18.27"/>
|
||||||
|
<text text-anchor="middle" x="71.38" y="-87.45" font-family="Times,serif" font-size="14.00">d</text>
|
||||||
|
</g>
|
||||||
|
<!-- a--d -->
|
||||||
|
<g id="edge5" class="edge">
|
||||||
|
<title>a--d</title>
|
||||||
|
<path fill="none" stroke="black" d="M71.38,-145.26C71.38,-134.57 71.38,-120.78 71.38,-109.94"/>
|
||||||
|
</g>
|
||||||
|
<!-- e -->
|
||||||
|
<g id="node5" class="node">
|
||||||
|
<title>e</title>
|
||||||
|
<ellipse fill="lightgrey" stroke="black" cx="94.38" cy="-18.38" rx="17.86" ry="17.86"/>
|
||||||
|
<text text-anchor="middle" x="94.38" y="-14.68" font-family="Times,serif" font-size="14.00">e</text>
|
||||||
|
</g>
|
||||||
|
<!-- a--e -->
|
||||||
|
<g id="edge4" class="edge">
|
||||||
|
<title>a--e</title>
|
||||||
|
<path fill="none" stroke="black" d="M80.91,-148.19C87.25,-137.97 95.01,-123.52 98.38,-109.54 104.45,-84.41 101.03,-54.22 97.84,-35.94"/>
|
||||||
|
</g>
|
||||||
|
<!-- b -->
|
||||||
|
<g id="node2" class="node">
|
||||||
|
<title>b</title>
|
||||||
|
<ellipse fill="lightgrey" stroke="black" cx="18.38" cy="-18.38" rx="18.27" ry="18.27"/>
|
||||||
|
<text text-anchor="middle" x="18.38" y="-14.68" font-family="Times,serif" font-size="14.00">b</text>
|
||||||
|
</g>
|
||||||
|
<!-- c--b -->
|
||||||
|
<g id="edge2" class="edge">
|
||||||
|
<title>c--b</title>
|
||||||
|
<path fill="none" stroke="black" d="M18.38,-74.1C18.38,-63 18.38,-48.26 18.38,-36.87"/>
|
||||||
|
</g>
|
||||||
|
<!-- d--e -->
|
||||||
|
<g id="edge3" class="edge">
|
||||||
|
<title>d--e</title>
|
||||||
|
<path fill="none" stroke="black" d="M76.84,-73.38C80.55,-61.94 85.45,-46.86 89.13,-35.55"/>
|
||||||
|
</g>
|
||||||
|
</g>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 2.7 KiB |
Loading…
Reference in New Issue