Firstly I am newly with cplex, i want to define R as a set of two pairs (i,j) and then write the below constrains:
zij ∈ {0, 1}, ∀ i, j s.t.(i, j) ∈ R
fj − fi ≥ 0 − M(1 − zij ), ∀ i, j s.t.(i, j) ∈ R
fi − fj ≥ E − Mzij , ∀ i, j s.t.(i, j) ∈ R
Noting:
E is a very small positive number
M is a “sufficiently large” number
and I have defined f as dvar int+ f[channels];
You could start with
int M=1000;
int E=2;
range R=1..10;
dvar boolean z[R,R];
dvar int+ f[R];
subject to
{
forall(i,j in R)
{
f[j]-f[i]>=0-M*(1-z[i][j]);
f[j]-f[i]>=E-M*z[i,j];
}
}
Related
I'm trying to implement polynomial long division based on polynomials of type int array. Here the highest degree coefficients are at the end. I'm basing my code off of the pseudo code available on Wikipedia:
function n / d is
require d ≠ 0
q ← 0
r ← n // At each step n = d × q + r
while r ≠ 0 and degree(r) ≥ degree(d) do
t ← lead(r) / lead(d) // Divide the leading terms
q ← q + t
r ← r − t × d
return (q, r)
H
This line
q = poly_add q t
is testing for the equality between q and poly_add q t, it is not an assignment and the compiler is warning you that you are ignoring the result of this test. You are also misunderstanding your pseudo-code: t is supposed to be a polynomial of degree degree r - degree d.
You need to use references, or transform your while loop into a recursive function.
Similarly, since r is an array, whose length cannot be changed:
while ((Array.length r) >= n2) && (Array.length r != 0)do
This test does not change during the loop. Structural inequality is <>.
Another issue is that this line
t.(0) <- r.((Array.length r)-1)/p2.(n2-1)
is mutating your zero polynomial which is a bad idea.
Overall, it is not clear if your polynomial are supposed to be mutable or not.
If they are not supposed to be mutable, you should avoid a.( ) <- altogether.
I am trying to prove the following simple theorem over natural numbers:
((i + j) = (i + k)) -> (j = k)
Here is what I have in Coq:
Theorem cancel : forall (i j k : nat),
((add i j) = (add i k)) -> (j = k).
Proof.
intros i j k.
induction i.
simpl.
apply A_IMPLIES_A.
simpl.
And after that I have the sub-goal:
S (add i j) = S (add i k) -> j = k
So I thought I'd apply eq_add_S which states that S m = S n -> m = n.
However, when I try to do so with apply eq_add_S I get the following error:
Error:
In environment
i, j, k : nat
IHi : add i j = add i k -> j = k
Unable to unify "k" with "add i k".
So I guess it can't understand that I want is m = (add i j) and n = (add i k). How come Coq can't read my mind? or more seriously, how can I help him do so? thanks!
The problem is not that Coq can't guess what value to use for m and n, but that your goal does not have the right shape for you to instantiate that theorem. When you write apply eq_add_S, Coq tries to unify S n = S m -> n = m with S (add i j) = S (add i k) -> j = k, which cannot be done.
What you need is to apply eq_add_S to the goal's premise, by introducing it into the context.
Proof.
intros i j k H. (* H : add i j = add i k *)
induction i as [|i IH].
- apply H.
- apply eq_add_S in H.
(* ... *)
I'm posting the solution as a separate answer hoping other users can benefit from it. Here it is:
Theorem cancel : forall (i j k : nat),
((add i j) = (add i k)) -> (j = k).
Proof.
intros i j k H.
induction i.
apply H.
simpl in H.
apply eq_add_S in H.
apply IHi in H.
assumption.
Qed.
The other day I encountered a problem related with queries, but I can't solve it.
Given an array with N integers and a positive integer M, you must answer Q queries. Each query is characterized as ( i , j ), where i and j are each indices of the array. In each query you must answer how many pairs ( r , s ) exist such that
i <= r <= s <= j
the sum of the array elements with indices in [ r , s ] is divisible by M.
Limits:
N <= 50,000
Q <= 50,000
M <= 100
I have a dynamic programming solution that preprocesses every query ( r , s ) in O( N^2 ), but that is not fast enough. Is there a more efficient solution? I have some ideas with Mo's algorithm, or with segment trees, but I can't get it.
Calculate the prefix sums of the original array (assuming it's 1-based) for every i = 1..N.
The equivalence of Sum[r] and Sum[s] for any two indices r and s where r < s means that the sum of the array elements with indices in [r+1, s] is divisible by M (and we need to calculate the number of such equivalences within interval). The time complexity of this step is O(N).
Precalculate the array Count for every i = 1..N, j = 0..M-1:
Count[i][j] stores the number of times that Sum[len] (where len <= i) was equal to j. Time complexity of this step is O(N*M).
For every query (i, j) the answer will be equal to:
For every possible value of the remainder k we find D(k) - the number of times that Sum[len] is equal to k within interval [i, j]. Then we add to the result the number of all possible pairs of D(k) interval boundaries that is D(k)*(D(k)-1)/2. Time complexity: O(M) for every query.
Complexity: O(N) + O(N*M) + O(Q*M) = O((Q+N)*M), that would be ok for given constraints.
First note that for any subarray (r, s) that sums to a multiple of M:
sum(r, s) == sum(i, s) - sum(i, r - 1)
== (qa * M + ra) - (qb * M + rb)
where ra and rb are both less than M and greater than or equal to 0 (i.e. the respective remainders after dividing by M).
Now sum(r, s) is divisible by M so it's remainder is 0 after dividing by M. Therefore:
ra == rb
If we calculate all the remainders after dividing the sums the subarrays (i, i), (i, i + 1), ... ,(i, j) by M as r1, r2, ... , rj then store the count of all these in an array R of size M so that R[k] is the number of remainders equal to k, then:
R[0] == the number of subarrays starting at i that are divisible by M
and for every k >= 0 and k < M such that R[k] > 1 we can count R[k] choose 2:
(R[k] * (R[k] - 1)) / 2
subarrays not starting at i that are divisible by M.
Creating and summing all these values gives us the answer in O( N + M ) for each (r, s) query.
I am trying to compute binomial coefficients using Dynamic Programming. I keep getting this error
fun binomial(m:int, n:int, DP) =
if n = m orelse n = 0 then
Array2.update(DP, m, n, 1)
else if Array2.sub(DP, m, n) = ~1 then (
if Array2.sub(DP, m-1, n) = ~1 then
Array2.update(DP, m-1, n, binomial(m-1, n, DP))
else
Array2.update(DP, m-1, n, Array2.sub(DP, m-1, n));
if Array2.sub(DP, m-1, n-1) = ~1 then
Array2.update(DP, m-1, n-1, binomial(m-1, n-1, DP))
else
Array2.update(DP, m-1, n-1, Array2.sub(DP, m-1, n-1));
let
val k = Array2.sub(DP, m-1, n) + Array2.sub(DP, m-1, n-1);
in
Array2.update(DP, m, n, k)
end
)
else
Array2.update(DP, m, n, Array2.sub(DP, m, n));
Array2.sub(DP, m, n)
I am don't understand what this error is trying to say.
6.5-25.48 Error: right-hand-side of clause doesn't agree with function result type [literal]
expression: unit
result type: int
in declaration:
binomial =
(fn (<pat> : int,<pat> : int,DP) =>
if <exp> orelse <exp>
then Array2.update <exp>
else if <exp> then <exp> else <exp>)
Can anyone help me?
Your function binomial returns the result of Array2.update, which is a unit. But you're passing this return value in to Array2.update as the last argument, which is expecting an int.
EDIT: Oh, now I see... Your only problem is that the last line (Array2.sub(DP,m,n)) isn't part of your function - if you want to build a list of expressions, you need to surround it with brackets. So instead of
fun binomial (m,n,DP) = <update logic>; Array2.sub(DP,m,n)
you want
fun binomial (m,n,DP) = (<update logic>; Array2.sub(DP,m,n))
I knew that converting a regular expression to a NFA, there is a algorithm.
But I was wondering if there is a algorithm to convert a NFA to regular expression.
If there is, what is it?
And if there isn't, I am also wondering if all NFA can convert to a regular expression.
Is there a NFA that a regular expression that cannot represent?
Thank you! :D
Here is an algorithm where each transition is incrementally replaced with a regex, until there is only an initial and final state: https://courses.engr.illinois.edu/cs373/sp2009/lectures/lect_08.pdf [PDF]
An NFA can be written as a system of inequalities (over a Kleene algebra), with one variable for each state, one inequality q ≥ 1 for each final state q and one inequality q ≥ x r for each transition on x from state q to state r. This is a right-affine fixed point system, over a Kleene algebra, whose least fixed point solution gives you, for any q, the regular expression recognized by the NFA that has q as the start state. The system can be collated, so that all the inequalities q ≥ A, q ≥ B, ..., q ≥ Z, for each given q, are combined into q ≥ A + B + ... Z. The result is a matrix system 𝐪 ≥ 𝐚 + H 𝐪, where 𝐪 is the vector of all the variables, 𝐚 the vector of the affine coefficients - 0's for non-final states, 1's for final states, but those details are not important for what follows; and H is the matrix of all the linear coefficients.
To solve a right-affine system, do so one variable at a time. In Kleene algebra, the least fixed point solution to x ≥ a + bx is x = b* a. This applies both singly and matrix-wise, so that the least fixed point solutuion to 𝐪 ≥ 𝐚 + H 𝐪, in matrix form is 𝐪 = H* 𝐚.
Matrices over Kleene algebras form a Kleene algebras, with matrix addition and matrix multiplication, respectively, for the sum and product operators and the matrix star for the Kleene star. Finding the matrix star is one and the same process as solving the corresponding system 𝐪 ≥ 𝐚 + H 𝐪.
A Generic Example:
Consider the NFA with states q, r, s, with q the start state and s the only final state, and with transitions:
a: q → q, b: q → r, c: q → s,
d: r → q, e: r → r, f: r → s,
g: s → q, h: s → r, i: s → s.
Let (x, y, z) = (0, 0, 1) denote the corresponding affine coefficients. Then, the corresponding right-affine system is:
q ≥ x + a q + b r + c s,
r ≥ y + d q + e r + f s,
s ≥ z + g q + h r + i s.
Solve for s, first, to obtain
s = i* (z + g q + h r) = i* z + i* g q + i* h r.
Substitute in the other inequalities to get:
q ≥ x + c i* z + (a + c i* g) q + (b + c i* h) r,
r ≥ y + f i* z + (d + f i* g) q + (e + f i* h) r.
Rewrite this as
q ≥ x' + a' q + b' r,
r ≥ y' + d' q + e' r,
where
x' = x + c i* z, a' = a + c i* g, b' = b + c i* h,
y' = y + f i* z, d' = d + f i* g, e' = e + f i* h.
Solve for r to get
r = e'* (y' + d' q) = e'* y' + e'* d' q.
Substitute into the inequality for q to get
q ≥ (x' + b' e'* y') + (a' + b e'* d') q
and rewrite this as
q ≥ x" + a" q
where
x" = x' + b' e'* y', a" = a' + b e'* d'.
Finally, solve this for q to get
q = a"* x".
This is also the general form for that embodies the generic fail-safe solution for NFA's with 3 states.
Since q is the start state, then a"* x" is the regular expression sought for, with a", x", a', b', d', e', x', y', x, y and z as indicated above. If you try to in-line substitute them all, the expression will blow up to a size that is exponential in the number of states and will be large in size even for three states.
An Optimized Example:
Consider the system for the NFA whose states are q, r, s, with q the start state, s the final state, and the transitions
a: q → r, a: q → q, b: q → q, b: q → s, a: s → s, b: s → s
The corresponding right-affine system is
q ≥ a r + a q + b q
r ≥ b s
s ≥ 1 + a s + b s
Solve for s first:
s ≥ 1 + a s + b s = 1 + (a + b) s ⇒ s = (a + b)*.
Substitute into the inequality for r and solve to find the least fixed point:
r ≥ b (a + b)* ⇒ r = b (a + b)*.
Finally, substitute into the inequality for q and solve to find the least fixed point:
q ≥ a b (a + b)* + (a + b) q ⇒ q = (a + b)* a b (a + b)*.
The resulting regular expression is (a + b)* a b (a + b)*. So, with chess-playing strategizing, simpler and optimal forms for the solution can be found.