1

I'm looking for an idiomatic constraint satisfaction solver that can maximize or minimize a goal function, rather than producing a list of matching solutions.

To be precise, I'm more interested in minimization (e.g., of gasoline consumption on a route that must also satisfy other constraints), but the question is:

I'm currently looking at core.logic and am under the impression that the module will not do either max or min. As far as I understand, that functionality would usually be specifically denoted as CLP. Core.logic does mention CLP(FD) (https://github.com/clojure/core.logic/wiki/Features), but looking at the description, I keep having serious doubts.

Could someone please comment on this - as my going through the entire Reasoned Schemer book would be too much, I guess?

9
  • Can you please provide an example optimization problem that is representative of the type of problem you want to solve? What is the objective function? What are the constraints? Are the unknowns real or integer? Commented May 10, 2020 at 7:51
  • @Rulle E.g., a Bill of Materials where you have to find an optimal mix from a universe of potential suppliers each of whom offers some required item, but price and quality vary in each case. So, you want to minimize the cost, while meeting your quality standards. Commented May 10, 2020 at 20:22
  • This sounds like a linear programming (en.wikipedia.org/wiki/Linear_programming) problem to me. And ojAlgo that I mentioned in my answer is good for that. If you state specifically and precisely what the objective function is, what the items are, what the quality standards are, etc, then someone might be able to help. Commented May 10, 2020 at 20:36
  • Thank you. Guess, I'll have to play around with the library you suggest before I can ask a good question. Commented May 10, 2020 at 20:46
  • You are welcome. I suggest you start by listing your variables, which is probably going to be the quantity of each item from each supplier. The objective function to minimize will be the sum over all quantities weighted by their unit price. And then there will be one linear inequality constraint per type of item for the minimum acceptable quality. You can use the example in my answer as a template. Commented May 10, 2020 at 21:04

1 Answer 1

1

The original question is not very specific about what kind optimization problem it wants to solve. Usually, the type of optimization problem will determine which solver is adequate.

However, many optimization problems that arise in engineering consist of objective functions with linear and quadratic terms, linear constraints, real and integer variables. The ojAlgo library is great for those problems. For example, if we want to minimize f(x, y) = x + 2y with x and y being integers, and three linear constraints x >= 2, y >= 3 and x + y >= 14.2, here is how to solve this problem using ojAlgo in Clojure:

(ns playground.ojalgo
  (:import [org.ojalgo.optimisation ExpressionsBasedModel]))

(defn demo []
  (let [model (ExpressionsBasedModel.)
        ;; Declare variables

        ;; X has lower limit 2
        x (-> (.addVariable model)
              (.lower 2)
              (.integer true))

        ;; Y has lower limit 3
        y (-> (.addVariable model)
              (.lower 3)
              (.integer true))]

    ;; Objective function: Minimize x + 2y
    (-> (.addExpression model)
        (.set x 1.0)
        (.set y 2.0)
        (.weight 1.0)) ;; <-- Terms in the objective function
                       ;;     need a weight.

    ;; Linear constraint: x + y >= 14.2
    (-> (.addExpression model)
        (.set x 1.0)
        (.set y 1.0)
        (.lower 14.2))

    (let [result (.minimise model)]
      (println "Result" result)
      (println "Objective function value: " (.getValue result))
      (println "Optimal X value: " (.getValue x))
      (println "Optimal Y value: " (.getValue y)))))

Which will display

Result #object[org.ojalgo.optimisation.Optimisation$Result 0xb755873 OPTIMAL 18.0 @ { 12, 3 }]
Objective function value:  18.0
Optimal X value:  12M
Optimal Y value:  3M

Chances are your problem (vaguely worded as "gasoline consumption on a route that must also satisfy other constraints") can be expressed in a format that this solver can handle. But with no more details, it is hard to provide a more accurate answer than this.

To use ojAlgo, you need to add the [org.ojalgo/ojalgo "48.1.0"] dependency to your Leiningen project.clj.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I think, this functionality is sufficient The problem I'm solving is quite simple from a mathematical perspective - I was just surprised that the native Clojure tool didn't cover the case where someone would be interested in an optimal solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.