Finding the first 1 in a binary set using optimization
Show older comments
Hello everybody,
Assume that a binary set V is given and the task is to find the first 1 in V. Note that
1) V is not sorted (so, we cannot benifit from binary search)
2) Although V is binary it is not known beforehand. It would be computationally expensive to tell
whether an element of V is 0 or 1. If I would know V then I could simple use the command find(V,1)
Therefore, I need to search efficiently. It seems that optimization is one efficient way to solve this problem. But,
unfortunately, it seems that the underlying optimization problem is not that easy to tackle. For instance, In
bellow, you see an example where I used genetic algorithm (I used this algorithm since it can tackle integer optimization problems)
clc;
A=[zeros(1,10^4) 1 zeros(1,100) 1 1 zeros(1,10^3)];
cost=@(i) i+10^8*(1-A(i)); % Here, in theory instead of 10^8, we should use 'infinity'
options = optimoptions('ga','Display','iter');
[x,f] = ga(cost,1,[],[],[],[],1,length(A),[],1,options)
Unfortunately, it often always fails to find the true solution which is x = 10^4+1. Rather it finds x = 1 as the
solution which is clearly wrong.
Any idea?
Thanks for your kind help in advance!
Babak
3 Comments
Mohammad Shojaei Arani
on 27 Aug 2023
I'm attempting to visualize your situation as the quest for searching the first Calculus book relative to the first call number order at the George Peabody Library. Ordinarily, you would peruse the library's catalogue, and after pinpointing the book, you would employ the call number to find it.
Let's assume that the library's homepage is inaccessible, and the digital catalogue is unavailable as well. Consequently, you engage with multiple librarians to aid you in physically locating the book. Is the metaheuristic optimization approach you're utilizing comparable to this scenario?

Image by Matthew Petroff / CC BY-SA.
Accepted Answer
More Answers (3)
Why not simply do
V=[zeros(1,10^4) 1 zeros(1,100) 1 1 zeros(1,10^3)];
index = find(V == 1, 1, 'first')
???
6 Comments
Bruno Luong
on 27 Aug 2023
" Although V is binary it is not known beforehand"
Mohammad Shojaei Arani
on 27 Aug 2023
Obviously V has to exist or else you can't do anything at all. And if it exists, then you "know" it (all its values are defined). However my code does not depend on it being binary or floating point, though it has to be numerical (not a structure or cell array or some other class). So I'm not sure why you say there is a problem with it or why it takes 30 seconds.
V=[zeros(1,10^4) 1 zeros(1,100) 1 1 zeros(1,10^3)];
tic
index = find(V == 1, 1, 'first')
toc
See, 0.016 seconds, not 30.
Mohammad Shojaei Arani
on 27 Aug 2023
Image Analyst
on 27 Aug 2023
Not really. Is there any difference between A and V? And if you want to somehow access A(27), like assign it to a new variable, that is virtually instant. Even generating a logical vector telling if A or V is exactly equal to 1 is virtually instant (unless A or V is hundreds of millions of elements long).
But it looks like your responses to @Bruno Luong that you don't really want find() regardless because you are trying to learn/use optimization, like a genetic algorithm or something: "I want to tackle it (via optimization) " and so are not interested in using standard, traditional functions.
Mohammad Shojaei Arani
on 27 Aug 2023
Bruno Luong
on 27 Aug 2023
Edited: Bruno Luong
on 27 Aug 2023
0 votes
You ask the same question several time. Without any other a priori knowledge, scan your array until 1 is meet.
There is no miracle algorithm to find it since there is not other way to guess where 1 first appears.
6 Comments
Mohammad Shojaei Arani
on 27 Aug 2023
Bruno Luong
on 27 Aug 2023
"Your way of tavkling the problem is linear search whose complexity os O(n)."
Incorrect this is the worse case complexity.
The average complexity is O(p*n) where p is probailty of 1 to appeas.
There is no miracle algorithm, you waste your time.
Mohammad Shojaei Arani
on 27 Aug 2023
Bruno Luong
on 27 Aug 2023
Edited: Bruno Luong
on 27 Aug 2023
" because, using optimization there is no need to inspect all the elements of V. Right?"
Wrong if your array is random without autocorrelation the optimization willl fail. That is so simple to build a count example.
Let assume the first 1 found by your miracle algo is presumed to be at position x. If your optmization evalutes less than x, meaning there is a hole somewhere in 1:x that is NOT evalute. Then I put a 1 there. Your optimzation fail to detect it.
So any algorithm needs at least x evaluations to detect correctly the first 1 at position x.
Mohammad Shojaei Arani
on 27 Aug 2023
Bruno Luong
on 27 Aug 2023
Whatever until the lesson is learned...
John D'Errico
on 27 Aug 2023
Edited: John D'Errico
on 27 Aug 2023
0 votes
If V is given, then find(V,1) is perfect, and it will not be improved upon. And you say that V is given! So what is your question?
MAYBE, MAYBE, you mean that V is given, but unknown in a sense, that you cannot know the value of V unless you interrogate that element? That seems consistent with your comments.
Now, your goal is to somehow magically use optimization to find that first element. The problem is, suppose you sample element 100, this V(100), and you find that is is a 1. Is that the FIRST element of V that is 1? You can never know. And nothing will tell you that is the case, unless you evaluate EVERY element that precedes that element. This tells me if your goal is to find the FIRST element of this unknown binary vector that is a 1, then you need to simply start at the beginning, and test every element. Optimization cannot help you, since the unit elements of V are presumed to be random as far as we know. Optimization is meaningless in this case. I'm sorry, but it is.
Ok, is there anything we can do? Suppose V has length 1024. Evaluate V at some intermediate position., say V(512). If V(512) is a 1, then you know that the first element that is a 1 must lie no further than element 512. But if V(512)==0, then you know nothing. Again, this suggests your best scheme is to simply start at the very begnning.
It feels like it might help if we know the probability that any element of V is a 1. Now you could use probability theory to know the distribution of the first non-zero element, if any element has probability P of being a 1. That could tell you where to start looking, but does it really help? NO. In the end, you will always need to test the first element of V, and if is zero, then test the second element, and so on, because knowing if a later element of V is 1 does not tell you anything.
I'm sorry, but if you absolutely need to know the first element of V, then you need to start your search at element 1, and then proceed. This is the only way to know if you have found the first 1.
4 Comments
Mohammad Shojaei Arani
on 27 Aug 2023
Bruno Luong
on 27 Aug 2023
"Does this clarify my claim that optimization is the way to proceed? "
No, because it will fail to find the first 1. This is bound to happen as I and John told to you with logical argument.
Now if you accept that occasional failure then it's OK.
But why ask the question then?
John D'Errico
on 27 Aug 2023
Edited: John D'Errico
on 27 Aug 2023
No. It does not at all justify your claim. Not even remotely.
An optimization will evaluate the vector V at multiple points. That you don't see that happening is irrelevant. You are not a little child, where if you don't see something happen in front of your nose, it does not happen. Function evals called by GA are no less costly than function calls in a loop, but in fact more costly, since you now have the overhead of GA.
A simple loop would have been more efficient, and the ONLY possible way to know that you have found the first true element in V.
An optimizer like GA will not know that no earlier element is 1, UNLESS IT TESTS ALL PREVIOUS VALUES. GA stops sometimes early, because it got lucky. And as you have seen, often GA will get it wrong. If you are looking for a probabilistic scheme, that MIGHT be different. But even then, I see no better scheme than to start at the beginning.
Mohammad Shojaei Arani
on 27 Aug 2023
Categories
Find more on Sensitivity Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!