简体   繁体   English

Excel Solver:有没有办法遍历2个变化的变量?

[英]Excel Solver: Is there a way to iterate over 2 changing variables?

I have an issue with solver as follows (simplified version): So I have a nested If statement that describes condition for 2 changing variables(x,y). 我的求解器有一个问题,如下所示(简化版本):所以我有一个嵌套的If语句,该语句描述2个变化变量(x,y)的条件。 For example: In one cell: IF(AND((x<=2),(x>=0.5),(y<=10),(y>=5)),1,0 例如:在一个单元格中:IF(AND((x <= 2),(x> = 0.5),(y <= 10),(y> = 5)),1,0
The cell below it: IF(AND((x<=2.5),(x>=1.9),(y<=11),(y>=9)),1,0 它下面的单元格:IF(AND((x <= 2.5),(x> = 1.9),(y <= 11),(y> = 9)),1,0
The objective function is the sum of these 2 variables 目标函数是这两个变量的总和
Solver or goal seek (unless i give it the awnser) can't seem to get an awnser other than 0,0. 求解器或目标搜索器(除非我给它提供遮篷)似乎无法获得除0,0以外的遮篷。 My actual problem is that i have 6 of these IF cells and I'm trying to find an (x,y) that maximizes my objective function. 我的实际问题是我有6个这样的IF单元,我试图找到一个最大化目标函数的(x,y)。 I want excel to go through as many combinations it can. 我希望excel能够进行尽可能多的组合。

Any thoughts or other ways to do this? 有什么想法或其他方式吗? Thanks. 谢谢。

The reason that the Solver does not find the optimal solution in this toy problem is because the use of IF and AND statements make the problem non convex. 求解器未在此玩具问题中找到最佳解的原因是因为使用IFAND语句使问题不凸。 For non-convex problems, the GRG Nonlinear solution method (the default used by solver) does not guarantee an optimal solution, as it can be trapped in locally best solutions which are not optimal. 对于非凸问题, GRG Nonlinear求解方法(求解器使用的默认值)不能保证最优解,因为它可能会陷入非最优局部最优解中。

Having said that, there is a way to formulate your problem as a mixed integer program, which, although still non-convex, can be solved with the "Simplex LP" method of Solver, and give a guaranteed maximum. 话虽如此,但是有一种方法可以将问题表达为混合整数程序,尽管它仍然是非凸的,但可以使用Solver的“ Simplex LP”方法进行求解,并给出最大的保证值​​。

Model Setup 模型设定

Here is a screenshot of the spreadsheet setup: 这是电子表格设置的屏幕截图:

在此处输入图片说明

For convenience, I have used named ranges for the several quantities. 为了方便起见,我对多个数量使用了命名范围。 In particular: 尤其是:

 - B2 --> x_var
 - C2 --> x_UB1
 - D2 --> x_LB1
 - E2 --> x_UB2
 - F2 --> x_LB2

and for row 3 I use the same convention, but instead of x_ we have y_ . 和第3行我使用相同的约定,但不是x_我们y_

The red cells ( B4 and E4 ) have the conditions you described, and the blue cell ( B5 ) has their sum. 红色单元格( B4E4 )具有您描述的条件,蓝色单元格( B5 )具有它们的总和。

For example, the condition for B4 reads 例如, B4的条件为

 =IF(AND(x_var<=x_UB1,x_var>=x_LB1,y_var<=y_UB1,y_var>=y_LB1),1,0)

We are going to replace these expressions with two binary variables, which equal one if each expression is satisfied and zero otherwise. 我们将用两个二进制变量替换这些表达式,如果满足每个表达式,则它们等于1,否则等于0。

The logic is that instead of an IF expression we can impose the constraints: 逻辑是,我们可以施加约束来代替IF表达式:

LB_x * z <= x <= UB_x * z
LB_y * z <= y <= UB_y * z
z is binary

then z = 1 ==> LB_x <= x <= UB_x LB_y <= y <= UB_y 然后z = 1 ==> LB_x <= x <= UB_x LB_y <= y <= UB_y

and because we maximize the sum of the two z variables, the x and y will try to fit i the corresponding ranges so that as many z as possible equal 1. 并且由于我们最大化了两个z变量的总和,因此xy将尝试使i对应于相应的范围,从而使尽可能多的z等于1。

在此处输入图片说明

The green cells H2, J2 have the two new binary varibles, called cond1_true, cond2_true respectively. 绿色单元格H2, J2具有两个新的二进制cond1_true, cond2_true分别称为cond1_true, cond2_true The other cells have the constraints described above: 其他单元格具有上述约束:

For example, for the first expression: 例如,对于第一个表达式:

J2: =x_var-cond1_true*x_UB1
J3: =y_var-cond1_true*y_UB1
K2: =x_LB1*cond1_true-x_var
K3: =y_LB1*cond1_true-y_var

All these cells need to be <= 0 in the solver model. 在求解器模型中,所有这些像元都必须<= 0等于<= 0

Solver Model: 求解器模型:

在此处输入图片说明

In the mode, the objective function cell is the sum of the binary variables. 在该模式下,目标函数单元是二进制变量的总和。 The decision variables are x_var, y_yar, cond1_true, cond2_true . 决策变量是x_var, y_yar, cond1_true, cond2_true The constraints are all in expression <= 0 format. 约束全部为expression <= 0格式。 Here is the worksheet: https://www.dropbox.com/s/uek2k9gownhh3ni/excel-solver-is-there-a-way-to-iterate-over-2-changing-variables.xlsx?dl=0 这是工作表: https : //www.dropbox.com/s/uek2k9gownhh3ni/excel-solver-is-there-a-way-to-iterate-over-2-changing-variables.xlsx?dl=0

Using this formulation, the solver goes through many combinations of variables and tries to pick up the best one. 使用此公式,求解器可以遍历变量的多种组合,并尝试选择最佳变量。 It can often guarantee an optimal solution (which is almost always the case for small problems) 它通常可以保证最佳解决方案(小问题几乎总是这样)

UPDATE 更新

If the intervals are non overlapping we need to modily LB_x * z <= x <= UB_x * z to min(LB_x) * (1-z) + LB_x * z <= x <= UB_x * z + max(UB_x) * (1-z) 如果间隔不重叠,则需要将LB_x * z <= x <= UB_x * z取为min(LB_x)*(1-z)+ LB_x * z <= x <= UB_x * z + max(UB_x)* (1-z)

Where min(LB_x) is the minimum lower bound across all intervals (likewise for UB and for y). 其中min(LB_x)是所有间隔的最小下限(对于UB和y同样)。 This way, if an x does not fall into the interval (z=0) it is only forced to fall in some other interval. 这样,如果x不落入间隔(z = 0),则仅被迫落入某个其他间隔。

I hope this helps! 我希望这有帮助!

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

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