-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathgenerate-equation.js
152 lines (123 loc) · 3.69 KB
/
generate-equation.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* A tiny toy equation generator. It is very naive, and does silly things
* sometimes. Feel free to improve.
*/
var cfProb = 10; // base probability to generate a point.
var probabilityClass = {
POINT: cfProb,
LENGTH: cfProb * 0.5,
TRIGONOMETRY: cfProb * 0.9,
ARITHMETICS: cfProb * 0.6,
MINMAX: cfProb * 0.4,
EXP: cfProb * 0.1,
SIGN: cfProb * 0.01,
}
class BaseFunctionNode {
constructor(className) {
this.probability = 0;
this.className = className;
}
getProbability() {
return probabilityClass[this.className];
}
render() {
return '';
}
}
class SingleArgumentFunction extends BaseFunctionNode {
constructor(operator, p) {
super(p);
this.operator = operator;
}
render() {
var prevP = this.p;
var prevP = this.getProbability();
probabilityClass[this.className] *= 0.25;
normalizeProbabilities();
let args = generateArguments();
probabilityClass[this.className] = prevP;
normalizeProbabilities();
return this.operator(args);
}
}
class DualArgumentFunction extends BaseFunctionNode {
constructor(operator, p) {
super(p);
this.operator = operator;
}
render() {
// Decrease our probability to appear
var prevP = this.getProbability();
probabilityClass[this.className] *= 0.25;
normalizeProbabilities();
var left = generateArguments();
var right = generateArguments();
// revert it back;
probabilityClass[this.className] = prevP;
normalizeProbabilities();
return this.operator(left, right);
}
}
class ConstantFunction extends BaseFunctionNode {
constructor(constant, p) {
super(p);
this.constant = constant;
}
render() {
return this.constant;
}
}
var fList = [
new ConstantFunction('p.x', 'POINT'),
new ConstantFunction('p.y', 'POINT'),
// new DualArgumentFunction((a, b) => `length(vec2(${a}, ${b}))`, 'TRIGONOMETRY'),
new ConstantFunction('length(p)', 'LENGTH'),
new SingleArgumentFunction(a => `sin(${a})`, 'TRIGONOMETRY'),
new SingleArgumentFunction(a => `cos(${a})`, 'TRIGONOMETRY'),
// new SingleArgumentFunction(a => `sqrt(${a})`, cfProb * 0.8),
// new SingleArgumentFunction(a => `inversesqrt(${a})`, cfProb * 0.8),
new DualArgumentFunction((a, b) => `${a}*${b}`, 'ARITHMETICS'),
new DualArgumentFunction((a, b) => `${a}/${b}`, 'ARITHMETICS'),
new DualArgumentFunction((a, b) => `(${a}+${b})`, 'ARITHMETICS'),
new DualArgumentFunction((a, b) => `(${a}-${b})`, 'ARITHMETICS'),
new DualArgumentFunction((a, b) => {
if (a === b) return a;
return `min(${a},${b})`
}, 'MINMAX'),
new DualArgumentFunction((a, b) => {
if (a === b) return a;
return `max(${a},${b})`
} , 'MINMAX'),
new SingleArgumentFunction(a => `log(${a})`, 'EXP'),
new SingleArgumentFunction(a => `exp(${a})`, 'EXP'),
new DualArgumentFunction((a, b) => `pow(${a}, ${b})`, 'EXP'),
new SingleArgumentFunction(a => `abs(${a})`, 'SIGN'),
new SingleArgumentFunction(a => `sign(${a})`, 'SIGN'),
//new ConstantFunction('1.', cfProb * 0.001),
];
function normalizeProbabilities() {
var sum = 0;
fList.forEach(element => (sum += element.getProbability()));
fList.forEach(element => element.probability = element.getProbability()/sum);
}
function generateArguments() {
var p = Math.random();
var cumulativeProbability = 0;
var item;
for (var i = 0; i < fList.length; ++i) {
item = fList[i];
cumulativeProbability += item.probability;
if (p < cumulativeProbability) {
break;
}
}
if (!item) throw new Error('no more items');
return item.render();
}
export default function generate() {
normalizeProbabilities();
var vX = generateArguments();
var vY = generateArguments();
return `v.x = ${vX};
v.y = ${vY};`;
}