Rating Rotator Algorithm Design

    Recently, I was faced with the task of writing a “rotator” of some objects, the priority of which I should set myself. The bottom line is simple: more priority - more impressions of the object.
    However, at the design stage of the algorithm, several unpleasant features were found.
    Well, firstly, I wanted to get by with minimal changes to the database. I really did not want to make a separate table in mysql to count the number of impressions. But resigned to this fact, he continued designing. The first algorithm that I built was all stupid but on paper a couple of rotations went through and worked very well.
    Its essence was that in the rotation table we record not only the number of real impressions, but also a certain number according to the formula (hereinafter the lowering formula)

    20 - 2i

    where i is the rotation priority. The meaning of this formula is that when I show the object, I will select from the base an instance with a minimum lowering formula and add one more unit to the current amount of previous lowering formulas. Thus, it turns out that an object with a large rotation coefficient (the one I want to show more) will be more common, because a lower number will be added to the lowering formula than objects with a lower coefficient.
    The next problem that I have examined is the "20" problem. That is, on this algorithm it would be possible to set the rotation coefficient only from 0 to 9 (if we take only numbers from a number of naturals). The lowering formula has now been transformed depending on the number of objects:

    2n - i

    where n is the number of objects in the database suitable for rotation. But now some restrictions have been imposed: the rotation coefficient cannot exceed the total number of rotation objects. That is, when looking ahead, I’ll say that this actually kills one and a half rabbits. And it is killed by the fact that one of the problems is solved when adding a new object. Now, when adding an object, it remains only to make small manipulations: 1 - decide whether to increase all current object coefficients by 1; 2- write down the initiative lowering formula in the rotary record, or take the amount from any record already. After a few iterations, the shows will go as they should.

    2n - i
    0 <= i <= n





    In general, what this form is good for is that by equipping the coefficients in the lowering formula, you can rank the power of the effect that gives the rotation coefficient (that is, how much more will the coefficient 2 affect the impressions than 1 or 0)

    But as I said, I was not satisfied with the way where I would have to create a separate table in the database or add garbage fields. The final, working solution came to me when I thought about the following: “The coefficient should show how much more the object should be shown in the sample. If there are 2 3 1 1 coefficients, objects from 7 times should be shown approximately 2 3 1 1 times, respectively. ”And then another thought came to me, quick-coded, but satisfying my main requirement. All I needed was to add a rotation coefficient to the object. Further, it is all about the selection: during the selection, I select all the rotation objects and fill the array with duplicates according to the rotation coefficient. That is, having objects and coefficients: a rotation array will be like this:

    о1 о2 о3 о4
    2 3 1 1




    o1 o1 o2 o2 o2 o3 o4


    Now it remains only to mix it, and select a random element. According to probability theory, everything is correct: the larger the rotation coefficient, the more chances to choose this object. At first glance, it seems that ranking the power of the influence of the coefficient on the rotation will not work, but no one bothers to make a factor for the coefficient, or even put it in a square or cube (if there are a lot of objects). Notice that the new facility will fit into such a rotation scheme without any crutches.

    Also popular now: