musawar32ali commited on
Commit
5f76176
·
verified ·
1 Parent(s): 9c4777d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Automatic Time Table Generation Agent (Genetic Algorithm)
2
+ return "\n".join(lines)
3
+
4
+
5
+ # Gradio app - interactive UI
6
+
7
+
8
+ def parse_courses(text: str) -> List[Course]:
9
+ # expects lines like: C1,T1,30
10
+ lines = [l.strip() for l in text.splitlines() if l.strip()]
11
+ out = []
12
+ for ln in lines:
13
+ parts = [p.strip() for p in ln.split(",")]
14
+ if len(parts) >= 3:
15
+ out.append(Course(parts[0], parts[1], int(parts[2])))
16
+ return out
17
+
18
+
19
+ def parse_rooms(text: str) -> List[Room]:
20
+ # expects lines like: R1,40
21
+ lines = [l.strip() for l in text.splitlines() if l.strip()]
22
+ out = []
23
+ for ln in lines:
24
+ parts = [p.strip() for p in ln.split(",")]
25
+ if len(parts) >= 2:
26
+ out.append(Room(parts[0], int(parts[1])))
27
+ return out
28
+
29
+
30
+
31
+
32
+ def generate_handler(courses_text, rooms_text, pop_size, generations, mutation_rate):
33
+ courses = parse_courses(courses_text)
34
+ rooms = parse_rooms(rooms_text)
35
+ if not courses:
36
+ courses = DEFAULT_COURSES
37
+ if not rooms:
38
+ rooms = DEFAULT_ROOMS
39
+
40
+
41
+ best = run_ga(courses, rooms, int(pop_size), int(generations), elite_frac=0.05, mutation_rate=float(mutation_rate))
42
+ score, genome = best
43
+ timetable = format_timetable(genome)
44
+ # Also return a compact list
45
+ assignments = "\n".join([f"{c}@{DAYS[d]} S{slot+1} in {r}" for c,d,slot,r in genome])
46
+ return f"Fitness: {score}\n\nTimetable:\n{timetable}", assignments
47
+
48
+
49
+ with gr.Blocks(title="Automatic Time Table Generation Agent (Genetic Algorithm)") as demo:
50
+ gr.Markdown("# Automatic Time Table Generation Agent (Genetic Algorithm)")
51
+ with gr.Row():
52
+ with gr.Column(scale=2):
53
+ courses_input = gr.Textbox(lines=8, label="Courses (format: id,teacher,size)" , value='\n'.join([f"{c.id},{c.teacher},{c.size}" for c in DEFAULT_COURSES]))
54
+ rooms_input = gr.Textbox(lines=6, label="Rooms (format: id,capacity)", value='\n'.join([f"{r.id},{r.capacity}" for r in DEFAULT_ROOMS]))
55
+ pop_size = gr.Slider(10, 500, value=200, step=10, label="Population Size")
56
+ generations = gr.Slider(10, 1000, value=200, step=10, label="Generations")
57
+ mutation_rate = gr.Slider(0.0, 1.0, value=0.1, step=0.01, label="Mutation Rate")
58
+ run_btn = gr.Button("Generate Timetable")
59
+ with gr.Column(scale=1):
60
+ out_text = gr.Textbox(lines=20, label="Result (timetable)")
61
+ out_assign = gr.Textbox(lines=12, label="Assignments (compact)")
62
+
63
+
64
+ run_btn.click(generate_handler, inputs=[courses_input, rooms_input, pop_size, generations, mutation_rate], outputs=[out_text, out_assign])
65
+
66
+
67
+ if __name__ == "__main__":
68
+ demo.launch(server_name="0.0.0.0", server_port=7860)