Why does cvxpy return unbounded here? - linear-programming

I'm using cvxpy 0.4.9 and Python 2.7.14 and getting a surprising unbounded status from the example below.
Slight variations (say, drop the last constraint) correctly report infeasible status.
This happens in both Windows and Linux environments. Why?
import cvxpy
import numpy
def main():
yld = numpy.array([[12.],[11.],[17.],[13.],[7.]])
wts = cvxpy.Variable(5)
obj = cvxpy.Maximize(yld.T * wts)
cons = []
cons.append(0.0 <= wts)
cons.append(numpy.ones(5).T * wts == 1.0)
cons.append(wts <= 2.5 * numpy.ones(5))
cons.append(wts <= 0.25)
cons.append(numpy.array([[0.],[0.],[1.],[1.],[1.]]).T * wts <= 0.0)
cons.append(numpy.array([[1.],[0.],[0.],[0.],[0.]]).T * wts <= 0.1 )
cons.append(numpy.array([[0.],[1.],[0.],[0.],[0.]]).T * wts <= 0.1 )
cons.append(numpy.array([[0.],[0.],[1.],[0.],[0.]]).T * wts <= 0.1 )
cons.append(numpy.array([[0.],[0.],[0.],[1.],[0.]]).T * wts <= 0.1 )
prob = cvxpy.Problem(obj, cons)
prob.solve()
print(prob.status)

With cvxpy==1.0.10 I get infeasible.
The feasible set is clearly empty. Why?
numpy.array([[0.],[0.],[1.],[1.],[1.]]).T * wts <= 0.0 and 0.0 <= wts imply that wts[2] == wts[3] == wts[4] == 0.
numpy.array([[0.],[1.],[0.],[0.],[0.]]).T * wts <= 0.1, [1.],[0.],[0.],[0.],[0.]]).T * wts <= 0.1 and 0.0 <= wts imply that 0 <= wts[0] == 0.1 and 0 <= wts[1] == 0.1
Thus it it impossible to satisfy numpy.ones(5).T * wts == 1.0 (sum of all elements of wts is 1).

Related

Sympy can't solve inequality with radicals

I need to solve this inequality:
enter image description here
import sympy as sp
from sympy.abc import x,n
epsilon = 0.01
a = 2/3
xn = (n**3 + n**2)**(1/3) - (n**3 - n**2)**(1/3)
i tried to solve it like this:
ans = sp.solve_univariate_inequality(sp.Abs(xn-a) < epsilon,n,relational=False)
but received:
NotImplementedError: The inequality, Abs((x3 -
x2)0.333333333333333 - (x3 + x**2)**0.333333333333333 + 2/3) <
0.01, cannot be solved using solve_univariate_inequality.
And tried so, but it didn't work
ans = sp.solve_poly_inequality(sp.Poly(xn-2/3-0.01, n, domain='ZZ'), '==')
but received:
sympy.polys.polyerrors.PolynomialError: (n3 +
n2)**0.333333333333333 contains an element of the set of generators.
Same:
ans = sp.solveset(sp.Abs(xn-2/3) < 0.01, n)
ConditionSet(n, Abs((n3 - n2)0.333333333333333 - (n3 +
n**2)**0.333333333333333 + 0.666666666666667) < 0.01, Complexes)
How can this inequality be solved?
from sympy import *
from sympy.abc import x,n
a = S(2) / 3
epsilon = 0.01
xn = (n**3 + n**2)**(S(1)/3) - (n**3 - n**2)**(S(1)/3)
ineq = Abs(xn-a) < epsilon
Let's verify where ineq is satisfied with plot_implicit. Note that the left hand side of the inequality is valid only for n>=1.
plot_implicit(ineq, (n, 0, 5), adaptive=False)
So, the inequality is satisfied for values of n greater than 3.something.
We can use a numerical approach to find the root of this equation: Abs(xn-a) - epsilon.
from scipy.optimize import bisect
func = lambdify([n], Abs(xn-a) - epsilon)
bisect(func, 1, 5)
# out: 3.5833149415284424

What conventions does IPOPT use to construct its Lagrangian?

I am using IPOPT via Pyomo (the AMPL interface) to solve a simple problem and am trying to validate that the primal Lagrangian gradient is zero at the solution. I'm running the following script, in which I construct what I would expect to be the gradient of the Lagrangian with respect to primal variables.
import pyomo.environ as pyo
from pyomo.common.collections import ComponentMap
m = pyo.ConcreteModel()
m.ipopt_zL_out = pyo.Suffix(direction=pyo.Suffix.IMPORT)
m.ipopt_zU_out = pyo.Suffix(direction=pyo.Suffix.IMPORT)
m.ipopt_zL_in = pyo.Suffix(direction=pyo.Suffix.EXPORT)
m.ipopt_zU_in = pyo.Suffix(direction=pyo.Suffix.EXPORT)
m.dual = pyo.Suffix(direction=pyo.Suffix.IMPORT_EXPORT)
m.v1 = pyo.Var(initialize=-2.0)
m.v2 = pyo.Var(initialize=2.0)
m.v3 = pyo.Var(initialize=2.0)
m.v1.setlb(-10.0)
m.v2.setlb(1.5)
m.v1.setub(-1.0)
m.v2.setub(10.0)
m.eq_con = pyo.Constraint(expr=m.v1*m.v2*m.v3 - 2.0 == 0)
obj_factor = 1
m.obj = pyo.Objective(
expr=obj_factor*(m.v1**2 + m.v2**2 + m.v3**2),
sense=pyo.minimize,
)
solver = pyo.SolverFactory("ipopt")
solver.solve(m, tee=True)
grad_lag_map = ComponentMap()
grad_lag_map[m.v1] = (
(obj_factor*2*m.v1) + m.dual[m.eq_con]*m.v2*m.v3 +
m.ipopt_zL_out[m.v1] + m.ipopt_zU_out[m.v1]
)
grad_lag_map[m.v2] = (
(obj_factor*2*m.v2) + m.dual[m.eq_con]*m.v1*m.v3 +
m.ipopt_zL_out[m.v2] + m.ipopt_zU_out[m.v2]
)
grad_lag_map[m.v3] = (
(obj_factor*2*m.v3) + m.dual[m.eq_con]*m.v1*m.v2
)
for var, expr in grad_lag_map.items():
print(var.name, pyo.value(expr))
According to this, however, the gradient of the Lagrangian is not zero when constructed in this way. I can get the gradient of the Lagrangian to be zero by using the following lines to construct grad_lag_map
grad_lag_map[m.v1] = (
-(obj_factor*2*m.v1) + m.dual[m.eq_con]*m.v2*m.v3 +
m.ipopt_zL_out[m.v1] + m.ipopt_zU_out[m.v1]
)
grad_lag_map[m.v2] = (
-(obj_factor*2*m.v2) + m.dual[m.eq_con]*m.v1*m.v3 +
m.ipopt_zL_out[m.v2] + m.ipopt_zU_out[m.v2]
)
grad_lag_map[m.v3] = (
-(obj_factor*2*m.v3) + m.dual[m.eq_con]*m.v1*m.v2
)
With a minus sign in front of the objective gradient, the gradient of the Lagrangian is zero. This is surprising to me. I would not expect to see this factor of -1 for minimization problems. Can anybody confirm whether IPOPT constructs its Lagrangian with this -1 factor for minimization problems, or whether this is the artifact of some other convention I am unaware of?
This is the Gradient of the Lagrangian w.r.t. x computed in Ipopt (https://github.com/coin-or/Ipopt/blob/2b1a2f9a60fb3f8426b47edbe3b3520c7335d201/src/Algorithm/IpIpoptCalculatedQuantities.cpp#L2018-L2023):
tmp->Copy(*curr_grad_f());
tmp->AddTwoVectors(1., *curr_jac_cT_times_curr_y_c(), 1., *curr_jac_dT_times_curr_y_d(), 1.);
ip_nlp_->Px_L()->MultVector(-1., *z_L, 1., *tmp);
ip_nlp_->Px_U()->MultVector(1., *z_U, 1., *tmp);
This corresponds to an Ipopt-internal representation of a NLP, which has the form
min f(x) dual vars:
s.t. c(x) = 0, y_c
d(x) - s = 0, y_d
d_L <= s <= d_U, v_L, v_U
x_L <= x <= x_U z_L, z_U
The Lagragian for Ipopt is then
f(x) + y_c c(x) + y_d (d(x) - s) + v_L (d_L-s) + v_U (s-d_U) + z_L (x_L-x) + z_U (x-x_U)
and the gradient w.r.t. x is thus
f'(x) + y_c c'(x) + y_d d'(x) - z_L + z_U
The NLP that is used by most Ipopt interfaces is
min f(x) duals:
s.t. g_L <= g(x) <= g_U lambda
x_L <= x <= x_U z_L, z_U
The Gradient of the Lagrangian would be
f'(x) + lambda g'(x) - z_L + z_U
In your code, you have a wrong sign for z_L.

OpenCL - double is neither <= 300 or >= 200? C++

double test = 0.0;
.... some code
char qwe = ...
.. some code
test += qwe;
if (test >= 200 || test <= 300) {
test = 7.0;
}
// For some reason, test seems to equal 0?
Why does (test >= 200 || test <= 300) == false?
It's very strange behaviour.
And when I print the output, it's 0?
Notwithstanding any undefined behaviour in your program, (test >= 200 || test <= 300) == false if, and only if, test is NaN. You can get a NaN (not a number) by some numerical error, such as 0.0 / 0.0 or by calling sqrt with a negative input.
Use std::isnan(test) to check for sure.

bandpass FIR filter

I need to make a simple bandpass audio filter.
Now I've used this simple C++ class: http://www.cardinalpeak.com/blog/a-c-class-to-implement-low-pass-high-pass-and-band-pass-filters
It works well and cut off the desired bands. But when I try to change upper or lower limit with small steps, on some values of limit I hear the wrong result - attenuated or shifted in frequency (not corresponding to current limits) sound.
Function for calculating impulse response:
void Filter::designBPF()
{
int n;
float mm;
for(n = 0; n < m_num_taps; n++){
mm = n - (m_num_taps - 1.0) / 2.0;
if( mm == 0.0 ) m_taps[n] = (m_phi - m_lambda) / M_PI;
else m_taps[n] = ( sin( mm * m_phi ) -
sin( mm * m_lambda ) ) / (mm * M_PI);
}
return;
}
where
m_lambda = M_PI * Fl / (Fs/2);
m_phi = M_PI * Fu / (Fs/2);
Fs - sample rate (44.100)
Fl - lower limit
Fu - upper limit
And simple filtering function:
float Filter::do_sample(float data_sample)
{
int i;
float result;
if( m_error_flag != 0 ) return(0);
for(i = m_num_taps - 1; i >= 1; i--){
m_sr[i] = m_sr[i-1];
}
m_sr[0] = data_sample;
result = 0;
for(i = 0; i < m_num_taps; i++) result += m_sr[i] * m_taps[i];
return result;
}
Do I need to use any window function (Blackman, etc.)? If yes, how do I do this?
I have tried to multiply my impulse response to Blackman window:
m_taps[n] *= 0.42 - 0.5 * cos(2.0 * M_PI * n / double(N - 1)) +
0.08 * cos(4.0 * M_PI * n / double(N - 1));
but the result was wrong.
And do I need to normalize taps?
I found a good free implementation of FIR filter:
http://www.iowahills.com/A7ExampleCodePage.html
...This Windowed FIR Filter C Code has two parts, the first is the
calculation of the impulse response for a rectangular window (low
pass, high pass, band pass, or notch). Then a window (Kaiser, Hanning,
etc) is applied to the impulse response. There are several windows to
choose from...
y[i] = waveform[i] × (0.42659071 – 0.49656062cos(w) + 0.07684867cos(2w))
where w = (2)i/n and n is the number of elements in the waveform
Try this I got the code from:
http://zone.ni.com/reference/en-XX/help/370592P-01/digitizers/blackman_window/
I hope this helps.

Round down the nearest integer power of a float [duplicate]

This question already has an answer here:
Rounding to the nearest integer power of a given base
(1 answer)
Closed 9 years ago.
Given two floating point values zoomAmount and zoomFactor, I need to calculate a newZoomAmount such that:
(newZoomAmount <= zoomAmount) && (newZoomAmount == pow( zoomFactor, i ))
for any integer i. I can easily loop through the values or binary search through a table to find the answer. However: is there a closed form to accomplish this?
Motivation: The zoomFactor is 2⅕ ≅ 1.148698354997035, so that each 5 "zoom in" events result in ~exactly a power of two increase. When zooming a diagram to fit on screen I want the zoom level to be one of these notches so that zooming out eventually lands exactly on the 'base' 1.0 zoom level.
Denoting:
A = zoomAmount
F = zoomFactor
newA = newZoomAmount
we have:
newA = pow(F, i)
=> log(newA) = i*log(F)
=> i = log(newA)/log(F)
and as newA <= A, and log is non-decreasing,
i = floor(log(A)/log(F))
newZoomAmount = pow( zoomFactor, floor( log(zoomAmount)/log(zoomFactor) ) );
Basically, logarithms. I'll ignore the fact that the underlying implementation of log probably iterates because you probably don't mind.
Using the following:
zoomFactor = 21/5
= 1.148698354997035
zoomAmount = 2.25
You need to find the following (note that I use less than instead of less than or equal, see end for why):
(newZoomAmount < 2.25) && (newZoomAmount == 2i/5)
By inspection, we know (since zoomFactor5 == 2 and zoomFactor6 == 2.297... > 2.25)
newZoomAmount == 2
i == 5
So, to get the current zoomAmount in terms of an exponent, we take:
zoomExponent = log(zoomAmount) / log(zoomFactor)
= 0.81093... / 0.13862...
= 5.84962...
To get the next lowest integer, you should subtract 1, then take the ceiling.
newZoomExponent = ⌈zoomExponent - 1⌉
= ⌈4.84962...⌉
= 5
Finally:
newZoomAmount = zoomFactornewZoomExponent
The reason we use the ceiling of the decrement instead of just the floor is to handle the special case where zoomAmount is a perfect power of zoomFactor, in which case
⌊zoomExponent⌋ == zoomExponent
newZoomAmount == zoomAmount
Which we obviously don't want.
Just substitute and re-evaluate:
(newZoomAmount <= zoomAmount) && (newZoomAmount == pow( zoomFactor, i )) => pow( zoomFactor, i ) <= zoomAmount => i * ln(zoomFactor) <= ln(zoomAmount) => i <= ln(zoomAmount) / ln(zoomFactor) which may or may not be faster than just iterating.
Then just assign newZoomAmount = pow( zoomFactor, i );
exponent=log(zoomAmount)/log(zoomFactor); /* zoomFactor^exponent == zoomAmount */
newZoomAmount=pow(zoomFactor,floor(exponent)); /* round down exponent */