Linear Objective function - nonlinear constraints different results for different initial values - matlab

I have an optimization problem.
My objective function is a linear function which is:
f=h(1)+h(2)+h(3)+h(4)+h(5)
where h(i)=t(i+1)-t(i)
My constraints are non-linear:
v_{ji}-v_{max,j}<=0 and a_{ji}-a_{max,j}<=0
where
for m=1:6
for i=2:4
v_{ji}=abs(((ddq(m,i+1)*(t(i+1)-time(j))^2)/(2*h(i)))+((ddq(m,(i+3))*(time(j)-t(i))^2)/h(i))+((path(m,i+1)/h(i))-(h(i)*ddq(m,(i+3)))/6)-((path(m,i)/h(i))-(h(i)*(ddq(m,i+1))/6)))-v_{max,j};
end
end
and
for m=1:6
for i=2:4
a_{ji}=abs(((ddq(m,(i+2))/h(i))*(time(j)-t(i)))-((ddq(m,i+1)/h(i))*(time(j)-t(i+1))))-a_max(j);
end
end
time(j) is a random value between t(i) and t(i+1). I used fmincon function to obtain the optimized h values where I set the options as:
options=optimset('Algorithm','sqp','Display','iter','DiffMinChange',1e-6,'DiffMaxChange',1e-1,'TolFun',1e-10,'TolX',1e-16,'MaxFunEvals',10000,'MaxIter',250);
When I run the code I got different results for different initial values such as:
for h_0=[1 1 1 1 1], h=[0.052 0.087 0.104 0.007 0.002]
for h_0=[0.5 0.5 0.5 0.5 0.5], h=[0.020 0.059 0.106 0.011 0.003]
for h_0=[0.1 0.1 0.1 0.1 0.1], h=[0.017 0.05 0.107 0.017 0.042]
I also tried to solve the same problem with active-set and interior-point algorithms and the values obtained from active set is
for h_0=[0.1 0.1 0.1 0.1 0.1], h=[0.094 0.106 0.1 0.1 0.1]
and it didn't work for different values.
interior-point algorithm also didn't work and I couldn't get any results.
My questions are:
1. Why did I get different results for each different initial value?
2. Why didn't other algorithms work for this question?
3. If I had to change algorithm which one is the best for this problem and why?

Related

What's the easiest way of making a histogram in KDB?

If I've got a list of values x, what's the easiest way to make a histogram with bin-size b?
It seems like I could hack a complicated solution, but I feel like there must be a built-in function for this somewhere that I don't know about.
I haven't heard about built-in histogram so far. But I would approach this task like below.
For fixed bucket size:
a: 0.39 0.51 0.51 0.4 0.17 0.3 0.78 0.53 0.71 0.41;
b: 0.1;
{count each group x xbar y}[b;a]
// returns 0.3 0.5 0.4 0.1 0.7!2 3 2 1 2j
For "floating" buckets:
a: 0.39 0.51 0.51 0.4 0.17 0.3 0.78 0.53 0.71 0.41;
b: -1 0.5 0.7 1;
{count each group x#x bin y}[b;a]
// returns -1 0.5 0.7!5 3 2j
Above functions return dictionary with bucket starts as keys and number of bucket occurrences as values.
Assuming you have a list of x values (let's assume x = 1000):
v:1000?1.0;
You can achieve what you need as follows:
b:0.1;
hist:(count') group xbar[b;v];
There are two points:
the keys in hist are not sorted
For the bucket, do you prefer to output the left or the right delimiter?
To solve for 1), you simply do:
hist:(asc key hist)#hist;
To solve for 2) - I mean, if you want to have the right delimiter:
hist:(+[b;key hist])!value hist;

Neural network - exercise

I am currently learning for myself the concept of neural networks and I am working with the very good pdf from
http://neuralnetworksanddeeplearning.com/chap1.html
There are also few exercises I did, but there is one exercise I really dont understand, at least one step
Task:
There is a way of determining the bitwise representation of a digit by adding an extra layer to the three-layer network above. The extra layer converts the output from the previous layer into a binary representation, as illustrated in the figure below. Find a set of weights and biases for the new output layer. Assume that the first 3 layers of neurons are such that the correct output in the third layer (i.e., the old output layer) has activation at least 0.99, and incorrect outputs have activation less than 0.01.
I found also the solution, as can be seen on the second image
I understand why the matrix has to have this shape, but I really struggle to understand the step, where the user calculates
0.99 + 3*0.01
4*0.01
I really don't understand these two steps. I would be very happy if someone can help me understand this calculation
Thank you very much for help
Output of previous layer is 10x1(x). Weight matrix is 4x10. New output layer will be 4x1. There are two assumption first:
x is 1 only at one row. xT= [1 0 0 0 0 0 0 0 0 0]. If you multiple this vector with matrix W your output will be yT=[0 0 0 0], because there is only 1 in x. After multiplication by W will be this only 1 multiple by 0th column of W which are zeroes.
Second assumption is, what if x is not 1 anymore, instead of one x can be xT=[0.99 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01]. And if you perform multiplication of x with first row of W result is 0.05(I believe here is typo). When xT=[0.01 0.99 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01] after multiplication with first row of W result is 1.03. Because:
0.01*0 + 0.99*1 + 0.01*0 + 0.01*1 + 0.01*0 + 0.01*1 + 0.01*0 + 0.01*1 + 0.01*0 + 0.01*1 = 1.03
So I believe there is a typo, because author probably assume 4 ones at first row of W, which is not true, because there is 5 ones. Because if there was 4 ones at first first row, than really results will be 0.04 for 0.99 at first row of x and 1.02 for 0.99 at second row of x.

How to select whole numbers in a row and its adjacent numbers in Matlab

Good day!
I would like to select the whole numbers in my random data, at the same time it will also choose the adjacent numbers.
For example, I have this raw data
A = [0.1 0.2
0.2 0.1
1 0.3
0.3 0.2
0.4 0.4
2 0.5]
so would like to select the (1, 0.3) and (2, 0.5). then my final ouptut will be,
B= [1 0.3
2 0.4]
Thanks in advance!
You can use modulo:
B=A(sum(mod(A,1),2)==0,:)
========== EDIT ====================
Editing w.r.t. comments, if you are only checking for integers in the first column then you do not need to sum results:
B=A(mod(A(:,1),1)==0,:)
Alternative ways would use logicals instead of numericals:
B=A(all(A==round(A),2),:)
or if only the 1st column is checked:
B=A(A==round(A(:,1)),:)

How to make constrained combinations of scenarios in NetLogo Behavior Space?

In my problem I have seven global variables to be controlled by Behavior Space.
Variable weights are in a range of 0 to 1 with an increment of 0.1 like,
[“A” [0 0.1 1]],
[“B” [0 0.1 1]],
[“C” [0 0.1 1]],
[“D” [0 0.1 1]],
[“E” [0 0.1 1]],
[“F” [0 0.1 1]],
[“G” [0 0.1 1]]
The restriction in my problem is that in each scenario the combination of all seven weights must be equal to “1” e,g;
0.2+0.2+0.2+0.1+0.1+0.1+0.1=1
However Behavior Space, by default, makes all possible scenarios whether the sum of weights is equal to, greater than or lesser than “1”.
Can you please help me how to extract and use only those variable combinations in which the sum of weights is always equal to “1”.
Bundle of Thanks in anticipation.
I am a new NetLogo user without any programming background and in a desperate need of your help.
You can use the stop condition in Behavior Space
like
A + B + C + D != 1

matlab: Error/precision when evaluating vector

I noticed what was for me an unpredicted behavior when evaluating vectors. It seems is really different doing it directly than indexing within a loop. Can anyone give me some help with this?
i know is probably explained in how it makes each operation, so i need some keys about how to look for it
thanks for advise
thanks in advance
example:
x=[0.05:.01:3];
n=size(x,2);
y1 = (8*x-1)/(x)-(exp(x));
for i=1:n
y2(i)=(8*x(i)-1)/(x(i))-(exp(x(i)));
end
a1=[x;y1];
a2=[x;y2];
plot(x,y1, 'red')
hold on
plot(x,y2, 'blue')
here the plot:
http://i.stack.imgur.com/qAHD6.jpg
results:
a2:
0.05 -13.0513
0.06 -9.7285
0.07 -7.3582
0.08 -5.5833
0.09 -4.2053
0.10 -3.1052
0.11 -2.2072
0.12 -1.4608
0.13 -0.8311
0.14 -0.2931
0.15 0.1715
0.16 0.5765
a1:
0.05 6.4497
0.06 6.4391
0.07 6.4284
0.08 6.4177
0.09 6.4068
0.10 6.3958
0.11 6.3847
0.12 6.3734
0.13 6.3621
0.14 6.3507
0.15 6.3391
0.16 6.3274
What you want is:
y1 = (8*x-1)./(x)-(exp(x)); % note the ./
instead of:
y1 = (8*x-1)/(x)-(exp(x));
As a side note, you can type help / to see what your original first statement was really doing. It was effectively doing (x'\(8*x-1)')' (note the backslash).
The error is in your first vector calculation of y1. The problem is in the part where you divide by x. Vector/Matrix division is different from single element division and will return a vector/matrix result.
The solution is to do element-by-element division of these vectors. The vector notation shortcut for this is to use ./ (dot-divide):
y3 = (8*x-1)./(x)-(exp(x));
Now you should find that y2 and y3 are identical.