Блог пользователя early-morning-dreams

Автор early-morning-dreams, история, 2 года назад, По-английски

When it comes to writing a ternary search, what you usually do is this:

repeat 30/60/100/200 times {
    double lm = l + (r - l) / 3;
    double rm = r - (r - l) / 3;
    if (f(lm) < f(rm))
        r = rm;
    else
        l = lm;
}

Sources (notice the authors): 154861075 154863973

I recommend everyone who still writes like this to read this article . It will improve your ability to fit nested ternary searches. Also, I hope I don't need to explain why it is strictly better than the shown code.

Please, refrain from writing in the comments "but still their code works and is accepted!!1!".

  • Проголосовать: нравится
  • -67
  • Проголосовать: не нравится

»
2 года назад, # |
  Проголосовать: нравится +30 Проголосовать: не нравится

based

»
2 года назад, # |
  Проголосовать: нравится +11 Проголосовать: не нравится

If you want to say something, just say, do not assume that everybody can read your mind.

Do you mean that the correct algo should iterate while the difference is more than a constant? Well, I have bad news for you. One day your solution would fail with a time limit just because such checks will always pass due to a lack of enough precision in float/double.

  • »
    »
    2 года назад, # ^ |
      Проголосовать: нравится +34 Проголосовать: не нравится

    As I know, main idea of golden-section search is not about termination condition, but about total amount of function calculations.

    Due to golden-section ratio properties on each step you need calculate function only for one new point, because second point was calculated on some previous step.

»
2 года назад, # |
Rev. 4   Проголосовать: нравится +20 Проголосовать: не нравится

Thank you for your blog.

I remember that someone told me this way is a slow way to solve ternary search.

However, if the upper and lower bounds are integers, your way is not so proper. see this one(Chinese). PinkieRabbit said that.

Forgive my poor English, thanks very much.

UPD: You added "continuous". Your way is really faster, however, maybe it is difficult to implement it.

»
2 года назад, # |
  Проголосовать: нравится +33 Проголосовать: не нравится

Can you point us to a problem where golden section works and naive ternary search doesn't? Thanks

»
2 года назад, # |
  Проголосовать: нравится +38 Проголосовать: не нравится

There are reasons why strong competitors use a standard ternary search. It's simple and it works.

Golden ratio ternary search is more tricky to implement. You need to remember and pass some value. It's more difficult to analyze the precision error. Integer-based version (ternary search in a sequence) is ugly because of rounding.

You can share your implementation though.

»
2 года назад, # |
  Проголосовать: нравится +59 Проголосовать: не нравится

Ternary search does not use the idea of dividing into three equal parts. You can make it almost binary by keeping the middle part small. After all, you just need to look at the value of the derivative at the midpoint.

    double lm = l + (r - l) / 2.1;
    double rm = r - (r - l) / 2.1;

This way of writing forces it to find answer much faster (perhaps making it a little less stable).

»
2 года назад, # |
  Проголосовать: нравится +14 Проголосовать: не нравится

"but still their code works and is accepted!!1!" or in a normal tone

If something can be done faster it doesn't mean it must be. We usually try to write the most straightforward code that will work for given limitations. In the problem you mentioned time limit is not an issue. For me personally golden ternary search is messy, so I avoid it unless I can't get accepted otherwise.

  • »
    »
    2 года назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Thanks for the opinion. Unfortunately, everyone missed the point that I fit 4 searches and had 0 chance to fuckup while coding circles intersection.

    Yeah, writing circles intersection is the most straightforward way instead of fitting 4 searches.