📄 logic.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<title>YALMIP Example : Logic programming</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<meta content="Microsoft FrontPage 6.0" name="GENERATOR">
<meta name="ProgId" content="FrontPage.Editor.Document">
<link href="yalmip.css" type="text/css" rel="stylesheet">
<base target="_self">
</head>
<body leftMargin="0" topMargin="0">
<div align="left">
<table border="0" cellpadding="4" cellspacing="3" style="border-collapse: collapse" bordercolor="#000000" width="100%" align="left" height="100%">
<tr>
<td width="100%" align="left" height="100%" valign="top">
<h2>Logic programming</h2>
<hr noShade SIZE="1" color="#000000">
<p>By using <a href="extoperators.htm">nonlinear operators</a>,
logic programming is easily added to YALMIP. The logic programming
module is not intended to be used for large logic programs, but is
only implemented to help the user to add a small number of logic
constraints to a standard model. Note that logic constraints leads
to a problem with binary variables, hence mixed integer programming
is needed to solve the problem. The logic functionality has not been
tested much (actually not at all...), and was mainly implemented to
show the strength of nonlinear operators. A lot of features are
still lacking, but are typically easy to add. If you need it,
make a feature request! </p>
<h3>Simple logic constraints</h3>
<p>As a first example, let us solve a simple
satisfiability problem. Note the use of binary variables
and the <b>TRUE</b> operator (the <b>true</b> operator
takes a variable <b>x </b>and returns the constraint <b>
x≥1</b>.)</p>
<table cellPadding="10" width="100%">
<tr>
<td class="xmpcode">
<pre>binvar a b c d e f
F = set(true((a & b & c) | (d & e & f)));
solvesdp(F);
double([a b c d e f])
<font color="#000000">ans =
0 0 0 1 1 1</font></pre>
</td>
</tr>
</table>
<p>A constraint with a scalar logic expression, generated by
the operators <b>AND</b> or <b>OR</b>, is automatically handled as a truth
constrained variable (although it is strongly adviced to
use the truth operator.) Hence the following constraint is
equivalent.</p>
<table cellPadding="10" width="100%">
<tr>
<td class="xmpcode">
<pre>F = set((a & b & c) | (d & e & f));
solvesdp(F);
double([a b c d e f])
<font color="#000000">ans =
0 0 0 1 1 1</font></pre>
</td>
</tr>
</table>
<p>A truth constraint on a standard variable must however be
explicitely stated</p>
<table cellPadding="10" width="100%">
<tr>
<td class="xmpcode">
<pre>F = set((a & b & c) | (d & e & f)) + set(true(d));
solvesdp(F);
double([a b c d e f])
<font color="#000000">ans =
0 0 0 1 1 1</font></pre>
</td>
</tr>
</table>
<p>or equivalently</p>
<table cellPadding="10" width="100%">
<tr>
<td class="xmpcode">
<pre>F = set((a & b & c) | (d & e & f)) + set(d >= 1);
solvesdp(F);
double([a b c d e f])
<font color="#000000">ans =
0 0 0 1 1 1</font></pre>
</td>
</tr>
</table>
<p>To constrain an expression to be false, use the <b>
FALSE</b> operator,</p>
<table cellPadding="10" width="100%">
<tr>
<td class="xmpcode">
<pre>F = set(false((a & b & c) | (d & e & f)));
solvesdp(F);
double([a b c d e f])
<font color="#000000">ans =
1 0 0 1 0 0</font></pre>
</td>
</tr>
</table>
<p>or simply add the corresponding numerical constraint</p>
<table cellPadding="10" width="100%">
<tr>
<td class="xmpcode">
<pre>F = set(((a & b & c) | (d & e & f)) == 0);
solvesdp(F);
double([a b c d e f])
<font color="#000000">ans =
1 0 0 1 0 0</font></pre>
</td>
</tr>
</table>
<p>Objective functions are allowed.</p>
<table cellPadding="10" width="100%">
<tr>
<td class="xmpcode">
<pre>F = set(((a & b & c) | (d & e & f)) == 0);
solvesdp(F,-a-b-c-d-e-f);
double([a b c d e f])
<font color="#000000">ans =
1 0 1 1 0 1</font></pre>
</td>
</tr>
</table>
<p>In addition to the operators <b>AND</b> (&), <b>OR</b>
(|) and <b>NOT</b> (~), the operator <b>IMPLIES(X,Y)</b>
is implemeted. To force <b>e</b> to be true if <b>f</b> is
true, we use <b>IMPLIES</b>.</p>
<table cellPadding="10" width="100%">
<tr>
<td class="xmpcode">
<pre>F = set(((a & b & c) | (d & e & f)) == 0) + set(implies(f,e));
solvesdp(F,-a-b-c-d-e-f);
double([a b c d e f])</pre>
</td>
</tr>
</table>
<p>An equivalent formulation is</p>
<table cellPadding="10" width="100%" id="table2">
<tr>
<td class="xmpcode">
<pre>F = set(((a & b & c) | (d & e & f)) == 0) + set(e >= f);
solvesdp(F,-a-b-c-d-e-f);
double([a b c d e f])</pre>
</td>
</tr>
</table>
<h3>Mixed logic and conic constraints</h3>
<p>The first construction for creating mixed constraints
is <b>IMPLIES</b>. The following trivial program ensures the
symmetric matrix <b>X</b> to have eigenvalues larger than 1 if
<b>a</b> or <b>b</b> is true. The result is a mixed
integer semidefinite problem which will be solved using
YALMIPs mixed integer solver. Be warned, a lot of the
functionality here is experimental. Please validate your
results and report any oddities as usual.</p>
<table cellPadding="10" width="100%" id="table1">
<tr>
<td class="xmpcode">
<pre>binvar a b
X = sdpvar(3,3);
F = set(implies(a | b, X > eye(3)));
solvesdp(F)</pre>
</td>
</tr>
</table>
<p>The following construction ensures the variable <b>x</b>
to be contained in a polytope if <b>d</b> is true</p>
<table cellPadding="10" width="100%" id="table3">
<tr>
<td class="xmpcode">
<pre>A = randn(5,2);
b = rand(5,1);
x = sdpvar(2,1);
d = binvar(1,1);
F = set(implies(d,A*x <=b))</pre>
</td>
</tr>
</table>
<p>A related command is if-and-only-if, <b>IFF</b>, i.e.
logic equivalence. The following code force the
variable <b>x</b> to be contained in a polytope if <b>y</b> is
contained in the polytope, and vice versa.</p>
<table cellPadding="10" width="100%" id="table4">
<tr>
<td class="xmpcode">
<pre>A = randn(5,2);
b = rand(5,1);
x = sdpvar(2,1);
y = sdpvar(2,1);
F = set(iff(A*x <= b, A*y <= b))</pre>
</td>
</tr>
</table>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -