📄 logic.htm
字号:
<p>This functionality can be used to model various logic
constraints. Consider for instance the constraint that a
variable is in one of several polytopes.
<img border="0" src="polytopes.jpg" width="809" height="472"></p>
<p>The data and plot was generated with the following
code (assuming <a href="solvers.htm#mpt">MPT</a> is
installed)</p>
<table cellPadding="10" width="100%" id="table5">
<tr>
<td class="xmpcode">
<pre>A1 = randn(8,2);
b1 = rand(8,1)*2-A1*[3;3];
A2 = randn(8,2);
b2 = rand(8,1)*2-A2*[-3;3];
A3 = randn(8,2);
b3 = rand(8,1)*2-A3*[3;-3];
A4 = randn(8,2);
b4 = rand(8,1)*2-A4*[-3;-3];
plot(polytope(A1,b1),polytope(A2,b2),polytope(A3,b3),polytope(A4,b4))</pre>
</td>
</tr>
</table>
<p>We will now try to find a point <b>x</b> as far up in
the figure as possible, but still in one of the
polytope. First, we define 4 binary
variables, each one describing whether we are in the
corresponding polytope, and add the constraint that we
are in at least one polytope.</p>
<table cellPadding="10" width="100%" id="table6">
<tr>
<td class="xmpcode">
<pre>binvar inp1 inp2 inp3 inp4
F = set(inp1 | inp2 | inp3 | inp4);</pre>
</td>
</tr>
</table>
<p>To connect the binary variables with the polytopes, we
use the <b>IFF</b> operator.</p>
<table cellPadding="10" width="100%" id="table7">
<tr>
<td class="xmpcode">
<pre>x = sdpvar(2,1);</pre>
<pre>F = F + set(iff(inp1,A1*x < b1));
F = F + set(iff(inp2,A2*x < b2));
F = F + set(iff(inp3,A3*x < b3));
F = F + set(iff(inp4,A4*x < b4));</pre>
</td>
</tr>
</table>
<p>Finally, we solve the problem by maximizing the second
coordinate of x.</p>
<table cellPadding="10" width="100%" id="table8">
<tr>
<td class="xmpcode">
<pre>solvesdp(F,-x(2));
double(x)
<font color="#000000">ans =</font></pre>
<pre><font color="#000000"> 4.5949
4.2701</font></pre>
<pre>double([inp1 inp2 inp3 inp4])
<font color="#000000">ans =</font></pre>
<pre><font color="#000000"> 0 0 0 1</font></pre>
</td>
</tr>
</table>
<p>The <b>IFF</b> is overloaded as equality, hence the
following code generates the same problem.</p>
<table cellPadding="10" width="100%" id="table12">
<tr>
<td class="xmpcode">
<pre>F = set(inp1 | inp2 | inp3 | inp4);
F = F + set(inp1 == (A1*x < b1));
F = F + set(inp2 == (A2*x < b2));
F = F + set(inp3 == (A3*x < b3));
F = F + set(inp4 == (A4*x < b4));
solvesdp(F,-x(2));</pre>
</td>
</tr>
</table>
<p>We should mention that it is not necessary to
use the <b>IFF</b> operator in this problem, the <b>
IMPLIES</b> operator suffice. The implies operator
should be used when possible, since it for most cases is
more efficiently implemented (less number of additional
binary variables) than the <b>IFF</b> function.</p>
<table cellPadding="10" width="100%" id="table10">
<tr>
<td class="xmpcode">
<pre>F = set(inp1 | inp2 | inp3 | inp4);
F = F + set(implies(inp1,A1*x < b1));
F = F + set(implies(inp2,A2*x < b2));
F = F + set(implies(inp3,A3*x < b3));
F = F + set(implies(inp4,A4*x < b4));
solvesdp(F,-x(2));
double(x)
<font color="#000000">ans =</font></pre>
<pre><font color="#000000"> 4.5949
4.2701</font></pre>
</td>
</tr>
</table>
<p>The <b>IFF</b> operator is used internally to construct
mixed constraints involving <b>OR</b>.
With this functionality, we can solve the previous
problem using a more intuitive code (albeit
possibly inefficient since it will use <b>IFF</b>
instead of <b>IMPLIES</b>).</p>
<table cellPadding="10" width="100%" id="table11">
<tr>
<td class="xmpcode">
<pre>F = set( (A1*x < b1) | (A2*x < b2) | (A3*x < b3) | (A4*x < b4));
solvesdp(F,-x(2));
double(x)
<font color="#000000">ans =</font></pre>
<pre><font color="#000000"> 4.5949
4.2701</font></pre>
</td>
</tr>
</table>
<h3>Improving the Big-M formulation</h3>
<p>The conversion from logic constraints to mixed integer
constraints is done in YALMIP using a standard big-M
formulation. By default, YALMIP uses a big-M value of
10<sup>4</sup>, but it is recommended to use the command <a target="topic" href="reference.htm#bounds">bounds</a>
to improve this. By explicitly adding bounds on
variables, YALMIP can exploit these bounds to compute a
reasonably small approximation of a "sufficiently large"
constant.</p>
<table cellPadding="10" width="100%" id="table13">
<tr>
<td class="xmpcode">
<pre>x = sdpvar(2,1);
bounds(x,-100,100);
F = set( (A1*x < b1) | (A2*x < b2) | (A3*x < b3) | (A4*x < b4));
solvesdp(F,-x(2));
double(x)
<font color="#000000">ans =</font></pre>
<pre><font color="#000000"> 4.5949
4.2701</font></pre>
</td>
</tr>
</table>
<p>Adding bounds can significantly improve the strength of
the relaxations and corresponding reduction of
computation time. Hence, if you know lower and/or upper
bounds, let YALMIP know this through the
<a target="topic" href="reference.htm#bounds">bounds</a>
command.</p>
<h3>Behind the scenes</h3>
<p>The logic <b>AND</b> and <b>OR</b> operators are implemented using
<a href="extoperators.htm">nonlinear operators</a>. As an
example, to implement the <b>OR</b>
operator <b>z=or(x,y)</b> for binary <b>x</b> and <b>y</b>, the simple code below is
sufficient (the code actually used is slightly different
to improve performance for expressions with more than
two variables,
but the idea is the same). The first constraint ensures that <b>z</b> is
false if both <b>x</b> and <b>y</b> are false, the two
following constraints ensure that <b>z</b> is true if <b>x</b>
or <b>y</b> is true, while the last constraint constrains
<b>z</b> to be a binary (true/false) variable.</p>
<table cellPadding="10" width="100%">
<tr>
<td class="xmpcode">
<pre>function varargout = or(varargin)
% SDPVAR/OR
switch class(varargin{1})
case 'char'
z = varargin{2};
x = varargin{3};
y = varargin{4};
varargout{1} = set(x + y >= z) + set(z >= x) + set(z >= y) + set(binary(z));
varargout{2} = 0; % Neither convex or concave</pre>
<pre> case 'sdpvar'
x = varargin{1};
y = varargin{2};
varargout{1} = yalmip('addextendedvariable','or',varargin{:});</pre>
<pre> otherwise
end</pre>
</td>
</tr>
</table>
<p>The operators <b>IFF</b> and <b>IMPLIES</b> are just
standard implementations of big-M reformulations.</td>
</tr>
</table>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -