-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
192 lines (161 loc) · 7.11 KB
/
Copy pathscript.js
File metadata and controls
192 lines (161 loc) · 7.11 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Neural Network Trip Decision Script
class TripDecisionNetwork {
constructor() {
// Define weights for each input factor
this.weights = {
weather: 0.3,
budget: 0.4,
time: 0.2,
mood: 0.1
};
// Threshold for step activation function
this.threshold = 5.0;
// Initialize the network
this.initializeInputs();
this.calculate();
}
initializeInputs() {
// Get all input sliders and add event listeners
const inputs = ['weather', 'budget', 'time', 'mood'];
inputs.forEach(input => {
const slider = document.getElementById(input);
const valueDisplay = document.getElementById(`${input}-value`);
// Update display when slider changes
slider.addEventListener('input', (e) => {
const value = parseFloat(e.target.value);
valueDisplay.textContent = value.toFixed(1);
this.calculate();
this.updateVisualization();
});
// Initialize display
valueDisplay.textContent = parseFloat(slider.value).toFixed(1);
});
this.updateVisualization();
}
getInputValues() {
return {
weather: parseFloat(document.getElementById('weather').value),
budget: parseFloat(document.getElementById('budget').value),
time: parseFloat(document.getElementById('time').value),
mood: parseFloat(document.getElementById('mood').value)
};
}
calculateWeightedSum(inputs) {
let sum = 0;
for (const [factor, value] of Object.entries(inputs)) {
sum += value * this.weights[factor];
}
return sum;
}
stepActivationFunction(sum) {
return sum >= this.threshold ? 1 : 0;
}
calculate() {
// Get current input values
const inputs = this.getInputValues();
// Calculate weighted sum
const weightedSum = this.calculateWeightedSum(inputs);
// Apply step activation function
const output = this.stepActivationFunction(weightedSum);
// Update the display
this.updateDisplay(inputs, weightedSum, output);
}
updateDisplay(inputs, weightedSum, output) {
// Update calculation display
const calculationDisplay = document.getElementById('calculation-display');
calculationDisplay.textContent = weightedSum.toFixed(2);
// Update decision message
const decisionMessage = document.getElementById('decision-message');
const decisionViz = document.getElementById('decision-viz');
const outputNodeViz = document.getElementById('output-node-viz');
if (output === 1) {
decisionViz.textContent = 'YES';
decisionMessage.textContent = 'Go on the trip!';
decisionMessage.className = '';
outputNodeViz.classList.remove('no');
outputNodeViz.classList.add('yes');
} else {
decisionViz.textContent = 'NO';
decisionMessage.textContent = 'Stay home this time';
decisionMessage.className = 'no';
outputNodeViz.classList.remove('yes');
outputNodeViz.classList.add('no');
}
}
updateVisualization() {
const inputs = this.getInputValues();
const weightedSum = this.calculateWeightedSum(inputs);
const output = this.stepActivationFunction(weightedSum);
// Update input node intensities
Object.entries(inputs).forEach(([factor, value]) => {
const nodeViz = document.getElementById(`${factor}-node-viz`);
const connection = document.getElementById(`${factor}-connection`);
const intensity = value / 10;
// Update node opacity
nodeViz.style.opacity = 0.4 + (intensity * 0.6);
// Update connection thickness and activity
if (value > 5) {
connection.classList.add('active');
connection.style.strokeWidth = 2 + (intensity * 3);
} else {
connection.classList.remove('active');
connection.style.strokeWidth = 3;
}
});
// Update processing node intensity
const processingNodeViz = document.getElementById('processing-node-viz');
const intensity = Math.min(weightedSum / 10, 1);
processingNodeViz.style.opacity = 0.5 + (intensity * 0.5);
// Update output connection
const outputConnection = document.getElementById('output-connection');
if (output === 1) {
outputConnection.classList.add('active');
outputConnection.style.strokeWidth = 5;
} else {
outputConnection.classList.remove('active');
outputConnection.style.strokeWidth = 3;
}
}
}
// Add some interactive animations
function addInteractiveEffects() {
// Add hover effects to SVG nodes
const svgNodes = document.querySelectorAll('.input-node, .processing-node, .output-node');
svgNodes.forEach(node => {
node.addEventListener('mouseenter', function() {
this.style.transform = 'scale(1.1)';
this.style.transition = 'transform 0.2s ease';
this.style.transformOrigin = 'center';
});
node.addEventListener('mouseleave', function() {
this.style.transform = 'scale(1)';
});
});
// Add hover effects to connections
const connections = document.querySelectorAll('.connection');
connections.forEach(connection => {
connection.addEventListener('mouseenter', function() {
this.style.strokeWidth = (parseFloat(this.style.strokeWidth) || 3) + 2;
this.style.strokeOpacity = '1';
});
connection.addEventListener('mouseleave', function() {
this.style.strokeWidth = (parseFloat(this.style.strokeWidth) || 5) - 2;
this.style.strokeOpacity = '0.7';
});
});
}
// Initialize the neural network when the page loads
document.addEventListener('DOMContentLoaded', function() {
console.log('Initializing Trip Decision Neural Network...');
const network = new TripDecisionNetwork();
addInteractiveEffects();
// Add some helpful tips
console.log('Neural Network Tips:');
console.log('- Weather: How good is the weather? (0=terrible, 10=perfect)');
console.log('- Budget: How much money do you have? (0=broke, 10=rich)');
console.log('- Free Time: How much time do you have? (0=none, 10=lots)');
console.log('- Mood: How do you feel? (0=terrible, 10=amazing)');
console.log('- Threshold: If weighted sum ≥ 5.0, the network says GO!');
});
// Export for potential future use
window.TripDecisionNetwork = TripDecisionNetwork;