4

I'm generating a grid of objects, each of which has one of 3 colors. Assume I'm filling a particular grid cell. I know that around this cell there are, for example, 1 object of color 0, 1 of color 1 and 2 of color 2, so:

const surroundings = { 0: 1, 1: 1, 2: 2 }

Now I want to assign a color to the current cell I'm working with. I need to count probabilities for each color to be used. To make it look pleasing, I want it more likely to be different from its surroundings. So in the case above the probabilities could be { 0: 0.4, 1: 0.4, 2: 0.2 }.

I'm sure there is an implementation of such operation in the probability theory, but I'm having trouble finding the correct term for it. In the example I gave the probabilities could be different, because I have no idea how to calculate them. But for colors 0 and 1 they should certainly be equal and for color 2 it should be the smallest.

Alex Chashin
  • 3,129
  • 1
  • 13
  • 35
  • do you have another example? how do you go from the first surroundings to probabilities? – Nina Scholz Nov 28 '19 at 08:08
  • Shouldn't probability of 2 be the greatest as it has got 2 objects? it should be {0: 0.25, 1: 0.25, 2: 0.5} right? – Atishay Jain Nov 28 '19 at 08:10
  • You mean propability of Gaussian distribution? There are more distributions (as Student's [fun fact: came from beer brewing]), we don't know your whole background. So here's awiki article https://en.wikipedia.org/wiki/Probability_distribution . – Poselsky Nov 28 '19 at 08:11
  • @NinaScholz, the point is to make the least common color the most probable and the most common color - the least probable. Like in `{0: 1, 1: 1, 2: 1}` all probabilities should be equal `0.33` and in `{0: 2, 1: 4, 2: 6}` they could be something like `{0: 0.7, 1: 0.2, 2: 0.1}` – Alex Chashin Nov 28 '19 at 08:11
  • @AtishayJain, no, that's the point. I want the new color to have more probability to be different from its surroundings – Alex Chashin Nov 28 '19 at 08:12
  • @OlegMusijenko, I guess it has to do with discrete probability distribution. But I'm having a hard time trying to find how to do this calculation on the wiki and on the internet, because my background in probability theory is very poor – Alex Chashin Nov 28 '19 at 08:17
  • 1
    Find the original probability and subtract that by 1, this would invert your probabilities right – pavan kumar Nov 28 '19 at 08:22
  • 1
    @SriVenkataPavanKumarMHS, do you mean that for the given example I should do `probabilityOf0 = 1 - 1/4 = 0.75`? If I do this, than the distribution will be `{0: 0.75, 1: 0.75, 2: 0.5}` so the sum will be more than 1 which is impossible – Alex Chashin Nov 28 '19 at 08:24

2 Answers2

5

You could get the reciprocal and the sum and return the part of it.

function getPro(array) {
    var inv = array.map(v => 1 / v),
        sum = inv.reduce((a, b) => a + b);
    return inv.map(v => v / sum);
}


console.log(getPro([1, 1, 2]));
console.log(getPro([1, 1, 1]));
console.log(getPro([2, 4, 6]));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

I would use the sum of surrounding frequency (usually 4, except on grid boundaries) to get the inverse:

let surrounding = [1, 2, 1];
let sum = surrounding[0] + surrounding[1] + surrounding[2];
let inverted = [sum-surrounding[0], sum-surrounding[1], sum-surrounding[2]];
console.log(inverted);

// Pick a random color according to inverted probabilities:
let invSum = 2*sum;
let rnd = Math.floor(Math.random() * invSum);
let color = rnd < inverted[0] ? 0
          : rnd < inverted[0] + inverted[1] ? 1
          : 2;
console.log(color); 

This will also work when a particular color does not occur at all in the surroundings, for example with surrounding = { 0: 0, 1: 2, 2: 2 }

trincot
  • 317,000
  • 35
  • 244
  • 286