简体   繁体   English

在 clojure / core.logic 中最小化/最大化可能?

[英]Minimize/maximize possible in clojure / core.logic?

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.我正在寻找一个惯用的约束满足求解器,它可以最大化或最小化目标 function,而不是生成匹配解决方案的列表。

To be precise, I'm more interested in minimization (eg, 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.我目前正在查看 core.logic 并且我的印象是该模块不会做最大值或最小值。 As far as I understand, that functionality would usually be specifically denoted as CLP.据我了解,该功能通常会专门表示为 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. Core.logic 确实提到了 CLP(FD) ( https://github.com/clojure/core.logic/wiki/Features ),但是看着描述,我一直有严重的疑问。

Could someone please comment on this - as my going through the entire Reasoned Schemer book would be too much, I guess?有人可以对此发表评论吗-我想我浏览整本 Reasoned Schemer 的书太多了?

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.然而,工程中出现的许多优化问题包括具有线性和二次项、线性约束、实数和 integer 变量的目标函数。 The ojAlgo library is great for those problems. ojAlgo库非常适合解决这些问题。 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:例如,如果我们想最小化f(x, y) = x + 2y ,其中xy是整数,三个线性约束x >= 2y >= 3x + y >= 14.2 ,这里是如何在 Clojure 中使用 ojAlgo 解决这个问题:

(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 .要使用 ojAlgo,您需要将[org.ojalgo/ojalgo "48.1.0"]依赖项添加到您的 Leiningen project.clj中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM