Spaces:
Runtime error
Runtime error
Create Reward.py
Browse files
Reward.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, jsonify
|
2 |
+
from flask_socketio import SocketIO
|
3 |
+
import random
|
4 |
+
import time
|
5 |
+
import threading
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
socketio = SocketIO(app)
|
9 |
+
|
10 |
+
# Zone Pressure Monitor
|
11 |
+
class ZonePressureMonitor:
|
12 |
+
def __init__(self):
|
13 |
+
self.pressures = {'Zone A': 30, 'Zone B': 45}
|
14 |
+
|
15 |
+
def update_pressure(self):
|
16 |
+
# Simulate real-time pressure updates
|
17 |
+
for zone in self.pressures:
|
18 |
+
self.pressures[zone] = random.randint(20, 50)
|
19 |
+
|
20 |
+
def get_pressures(self):
|
21 |
+
return self.pressures
|
22 |
+
|
23 |
+
# Reward System
|
24 |
+
class RewardSystem:
|
25 |
+
def __init__(self):
|
26 |
+
self.units = {}
|
27 |
+
|
28 |
+
def record_efficiency(self, unit_id, efficiency):
|
29 |
+
self.units[unit_id] = efficiency
|
30 |
+
|
31 |
+
def calculate_rewards(self, pressures):
|
32 |
+
rewards = {}
|
33 |
+
for unit, efficiency in self.units.items():
|
34 |
+
pressure_factor = sum(pressures.values()) / len(pressures)
|
35 |
+
reward = efficiency - pressure_factor / 10
|
36 |
+
rewards[unit] = max(0, reward)
|
37 |
+
return rewards
|
38 |
+
|
39 |
+
zone_pressure_monitor = ZonePressureMonitor()
|
40 |
+
reward_system = RewardSystem()
|
41 |
+
|
42 |
+
def update_pressures():
|
43 |
+
while True:
|
44 |
+
zone_pressure_monitor.update_pressure()
|
45 |
+
pressures = zone_pressure_monitor.get_pressures()
|
46 |
+
socketio.emit('update_pressure', pressures)
|
47 |
+
time.sleep(5) # Update every 5 seconds
|
48 |
+
|
49 |
+
@app.route('/pressure')
|
50 |
+
def pressure_dashboard():
|
51 |
+
pressures = zone_pressure_monitor.get_pressures()
|
52 |
+
reward_system.record_efficiency('Vehicle1', 80)
|
53 |
+
reward_system.record_efficiency('Vehicle2', 45)
|
54 |
+
rewards = reward_system.calculate_rewards(pressures)
|
55 |
+
return render_template('pressure.html', pressures=pressures, rewards=rewards)
|
56 |
+
|
57 |
+
@socketio.on('connect')
|
58 |
+
def handle_connect():
|
59 |
+
pressures = zone_pressure_monitor.get_pressures()
|
60 |
+
socketio.emit('update_pressure', pressures)
|
61 |
+
|
62 |
+
if __name__ == '__main__':
|
63 |
+
threading.Thread(target=update_pressures).start()
|
64 |
+
socketio.run(app, debug=True)
|