-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecisionAlgorithm.js
71 lines (67 loc) · 2.05 KB
/
decisionAlgorithm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { View, Text } from "react-native";
import React from "react";
import { useSelector } from "react-redux";
export default function decisionAlgorithm({ items }) {
//math operations used in choice algorithm
const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
const round = (number) => Math.round(number * 100) / 100;
const divideArr = (arr, amount) => {
let newArr = [];
arr.forEach((e) => {
newArr.push(round(e / amount));
});
return newArr;
};
//random "target score"
const rand = Math.random();
//console.log("RAND: " + rand);
//weighted "scores"
const getWeightTable = () => {
let total = 0;
let weightTable = [];
items.forEach((item) => {
let ratingFactor = item.rating * 2;
let reviewsAccuracy =
((0.001 * (item.rating - 2.5)) / 2.5) * clamp(item.reviews, 0, 1000);
let priceFactor =
-0.5 * (item.price !== undefined ? item.price.length : 2);
let relevance = (2 * clamp(item.distance, 0, 5000)) / 5000;
let itemWeight = round(
ratingFactor + reviewsAccuracy + priceFactor + relevance
);
{
/*console.log(
"relavance of " +
item.name +
": " +
relevance +
"(actual distance: " +
item.distance +
")"
);*/
}
weightTable.push(itemWeight);
total += itemWeight;
});
//reduce from scores to average
return divideArr(weightTable, total);
};
//console.log("WEIGHT TABLE: " + getWeightTable());
//gets index where weight sum is greater than target "score"
const getChosenItemIndex = () => {
let arr = getWeightTable();
let sum = 0;
for (let i = 0; i < arr.length; i++) {
sum += arr[i];
//console.log("sum at index" + i + ": " + sum + " > " + round(rand) + "?");
if (sum >= rand) {
//console.log("TARGET INDEX: " + i);
return i;
}
}
return arr.length - 1;
};
let chosenItemIndex = getChosenItemIndex();
const chosenRestaurant = items[chosenItemIndex];
return chosenRestaurant;
}