Sorry I cannot find a clearer title for this question. I have a constraint like the following: $$ \sum_{t \in T} w_{t} \cdot y_{t} \geq A $$ where \(w_t\) are given, positive weights, and \(y_t \in \{0,1\} \). I want to add (if possible) the following constraint: all the nonzero \(y_t\) must be consecutive . That is, this is a valid solution: $$ y_{t} = \dots 0,0,1,1,1,0,0\dots $$ while this is not: $$ y_{t} = \dots 0,1,0,1,1,0,0\dots $$ The problem I find is that I don't know a priori how many \(y\) will be nonzero. asked 01 Feb '14, 18:27 Libra 
\[ T \cdot (1  x_i + x_{i+1}) ~\geq \sum_{j = i+1}^T x_j \quad, ~\forall ~i = 1 \ldots T1 \] \[ \begin{array}[t]{cccc} x_i & x_{i+1} & & LHS & & RHS\\ 0 & 0 & \rightarrow & T & \geq & [0, T1]\\ 0 & 1 & \rightarrow & 2T & \geq & [1, T1]\\ 1 & 0 & \rightarrow & 0 & \geq & 0\\ 1 & 1 & \rightarrow & T & \geq & [1, T1]\\ \end{array} \] doesn't introduce aux. variables (just like the approach @Austin suggested), and requires only \(T1\) constraints. In fact [and contrary to my initial estimate], this approach is both more memory efficient and faster*. *P.S.: Here, "faster" means: the solvers I've tried (CPLEX, Gurobi) solve [AMPL] model A significantly faster than model B. Model A:
Model B:
P.P.S: Anyhow, if @Libra can "afford" \(T\) additional binary variables, @leotac's approach is the way to go. Model C:
performs best among all three discussed approaches [by far!]. answered 02 Feb '14, 07:06 fbahr ♦ nice. n^3 can be a pain
(02 Feb '14, 10:53)
Austin Buchanan
Interesting indeed. Thank you very much for the extra testing.
(02 Feb '14, 12:41)
Libra

You could just use a set of (binary) variables, say \(z_t\), to indicate the first nonzero \(y_t\) variable. You can do this with something like \(y_t\ge z_t\ge y_ty_{t1}\). Then, bound \(\sum z_t\) to be (smaller or) equal to 1. answered 01 Feb '14, 20:53 leotac 
There's no need to introduce new variables. You want to avoid situations where two variables are set to one, but a variable between them is set to zero. So, for each triple \( (i,j,k)\) with \(1 \le i < j < k \le n:= T\), include the inequality \( y_j \ge y_i + y_k  1\). If we have \( y_i = y_k = 1\) then all variables \(x_j\) in between (i.e., with \( i < j < k\)) will be forced to one as well. The number of such inequalities is \( n \choose 3\) which is \( O(n^3)\). answered 01 Feb '14, 23:31 Austin Buchanan 
A couple things. Assuming we ignore the weight A constraint:
answered 05 Feb '15, 11:29 Austin Buchanan 
Could you please tell me what your application is and why it is important to have consecutive ones?
@Austin: https://www.orexchange.org/questions/9189/indexofthefirstlastpositivetimeindexedvariable + "nonpreemptiveness"?
@Austin @fbahr yes, I am trying to model a resource scheduling problem. The non preemptiveness came as a later requirements. I am looking at different formulations.