简体   繁体   English

Excel 求解器样式约束优化/约束编程 R

[英]Excel solver style constrained optimisation/constraint programming in R

I'm trying to write some R code to perform constrained optimisation, where the constraints are not of the usual (g(x) < c) type.我正在尝试编写一些 R 代码来执行约束优化,其中约束不是通常的 (g(x) < c) 类型。

An example of the type of problem I am trying to solve is the following:我试图解决的问题类型示例如下:

A company is trying to determine how much to produce each year for the next 5 years, given an assumed price for each year.给定每年的假定价格,一家公司正试图确定未来 5 年每年生产多少。 It can produce upto X per year, but over any given 3 year window can produce no more than 2X.它每年最多可以生产 X 次,但在任何给定的 3 年中,window 最多只能生产 2 次。 In addition, in any given year, it cannot produce less than 0.5X unless it produces 0X.此外,在任何给定年份,它的产量都不能低于 0.5X,除非它产生 0X。 Other constraints may be equally impossible to write as a set equation (eg if X > a, Y < c).其他约束可能同样不可能写成一个集合方程(例如,如果 X > a,Y < c)。

I'm very comfortable solving this type of thing in Excel Solver, as it's relatively easy to be very flexible with these sort of restrictions (eg each year have an if statement that looks at the quantity produced in current, past, and next year, and returns 0 if less than 2X, sum the if statements in a cell, and set the constraint of that variable equal to 0).我很乐意在 Excel Solver 中解决这类问题,因为通过这些限制非常灵活(例如,每年都有一个 if 语句来查看当前、过去和明年生产的数量,如果小于 2X,则返回 0,将单元格中的 if 语句相加,并将该变量的约束设置为等于 0)。

It's just gotten too large to realistically do in excel.在 excel 中,它变得太大而无法实际执行。 I'm pretty comfortable in R but haven't done this sort of thing.我对 R 很满意,但还没有做过这种事情。 If anyone can point me to a useful package or a good way of managing a lot of dynamic constraints rely on If statements etc, I would be super appreciative如果有人可以向我指出有用的 package 或管理大量动态约束的好方法依赖于 If 语句等,我将非常感激

You can use IF statements in optimization problems.可以在优化问题中使用 IF 语句。 This will make the problem nonlinear, so you need a nonlinear solver (Excel solver contains GRG2 which is quite a good NLP solver).这将使问题非线性,因此您需要一个非线性求解器(Excel 求解器包含 GRG2,它是一个非常好的 NLP 求解器)。 However, IFs may make the problem discontinuous or non-differentiable.但是,IF 可能会使问题不连续或不可微。 This can cause problems.这可能会导致问题。 Eg, the solver may get stuck and report a local optimum, instead of a global one.例如,求解器可能会卡住并报告局部最优值,而不是全局最优值。

It may be better to reformulate the model using binary variables.使用二进制变量重新制定 model 可能会更好。 Often this will make the model otherwise linear.通常这会使 model 线性化。 Disadvantage: it often requires some skill and experience to come up with and implement these reformulations.缺点:通常需要一些技能和经验来提出和实施这些重新制定。 The exact form of these reformulations is often solver-dependent (eg it may depend on whether the solver supports indicator constraints or SOS sets) and very problem-dependent.这些重新表述的确切形式通常取决于求解器(例如,它可能取决于求解器是否支持指示符约束或 SOS 集)并且非常依赖于问题。

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

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