I have the following problem. I would like to model the following relationship. I have three binary variables $x_1$, $x_2$ and $x_3$. These become either 0 or 1. I need a constraint for a mathemstical optimization model that expresses the following. Here, y=0 is to become if only one of the three x variables takes the value 1. If two of the x variables take the value 1, then y=1 should hold and also if none of the x variables take the value 1. How can I formulate the whole thing?
EDIT: $x_2$ and $x_3$ can never both be $=1$ at the same time. Either both are 0 or only one $=1$. $x_1$ can always take any value independently.
You want to enforce six logical implications: \begin{align} (x_1 \land \lnot x_2 \land \lnot x_3) &\implies \lnot y \tag1\label1 \\ (\lnot x_1 \land x_2 \land \lnot x_3) &\implies \lnot y \tag2\label2 \\ (\lnot x_1 \land \lnot x_2 \land x_3) &\implies \lnot y \tag3\label3 \\ (x_1 \land \lnot x_2 \land x_3) &\implies y \tag4\label4 \\ (x_1 \land x_2 \land \lnot x_3) &\implies y \tag5\label5 \\ (\lnot x_1 \land \lnot x_2 \land \lnot x_3) &\implies y \tag6\label6 \end{align} You can derive linear constraints somewhat automatically by rewriting in conjunctive normal form. For example, \eqref{1} yields $$ \lnot (x_1 \land \lnot x_2 \land \lnot x_3) \lor \lnot y \\ \lnot x_1 \lor x_2 \lor x_3 \lor \lnot y \\ (1-x_1)+x_2+x_3+(1-y) \ge 1 \\ -x_1+x_2+x_3-y \ge -1 $$ The other five are similar, although you can strengthen \eqref{4} and \eqref{5} by using the fact that $x_2 + x_3 \le 1$. Explicitly, \eqref{4} yields $$ \lnot (x_1 \land \lnot x_2 \land x_3) \lor y \\ \lnot x_1 \lor x_2 \lor \lnot x_3 \lor y \\ (1-x_1)+x_2+(1-x_3)+y \ge 1\\ -x_1+x_2-x_3+y \ge -1 $$ and \eqref{5} yields $$ \lnot (x_1 \land x_2 \land \lnot x_3) \lor y \\ \lnot x_1 \lor \lnot x_2 \lor x_3 \lor y \\ (1-x_1)+(1-x_2)+x_3+y \ge 1\\ -x_1-x_2+x_3+y \ge -1 $$ whereas the valid inequality $$ -x_1-x_2-x_3+y \ge -1 $$ dominates both of these. It turns out that a similar strengthening applies to \eqref{2} and \eqref{3}.
Here's an alternative approach that yields the strengthened formulation. Note that $x_2+x_3 \in \{0,1\}$, and introduce binary variable $x_{23}$ to represent $x_2+x_3$. Now you want to enforce four logical implications: \begin{align} (x_1 \land \lnot x_{23}) &\implies \lnot y \tag7\label7 \\ (\lnot x_1 \land x_{23}) &\implies \lnot y \tag8\label8 \\ (x_1 \land x_{23}) &\implies y \tag9\label9 \\ (\lnot x_1 \land \lnot x_{23}) &\implies y \tag{10}\label{10} \end{align} Applying conjunctive normal form then yields \begin{align} -x_1 + x_{23} - y &\ge -1 \\ x_1 - x_{23} - y &\ge -1 \\ -x_1 - x_{23} + y &\ge -1 \\ x_1 + x_{23} + y &\ge 1 \end{align} Finally, substituting $x_{23}=x_2+x_3$ yields \begin{align} -x_1 + x_2 + x_3 - y &\ge -1 \\ x_1 - x_2 - x_3 - y &\ge -1 \tag{8'}\label{8'}\\ -x_1 - x_2 - x_3 + y &\ge -1 \tag{9'}\label{9'}\\ x_1 + x_2 + x_3 + y &\ge 1 \end{align} Notice that \eqref{8'} and \eqref{9'} together imply $x_2 + x_3 \le 1$, so you do not need to explicitly enforce that constraint separately.