Advertisement
metalni

VI Prv Kolokvium - Schedule [CSP]

Jun 30th, 2021
1,408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.75 KB | None | 0 0
  1. from constraint import *
  2.  
  3. def what(x, y):
  4.     split = x.split('_')
  5.     split1 = y.split('_')
  6.     if split[0] == split1[0]:
  7.     #print(split[0] + '_' + str(int(split[1]) + 1) == y)
  8.         return y != (split[0] + '_' + str(int(split[1])+1)) and x != (split1[0] + '_' + str(int(split1[1])+1))
  9.     return True
  10.    
  11.    
  12.  
  13. if __name__ == '__main__':
  14.     problem = Problem(BacktrackingSolver())
  15.     casovi_AI = int(input())
  16.     casovi_ML = int(input())
  17.     casovi_R = int(input())
  18.     casovi_BI = int(input())
  19.    
  20.     AI_predavanja_domain = ["Mon_11", "Mon_12", "Wed_11", "Wed_12", "Fri_11", "Fri_12"]
  21.     ML_predavanja_domain = ["Mon_12", "Mon_13", "Mon_15", "Wed_12", "Wed_13", "Wed_15", "Fri_11", "Fri_12", "Fri_15"]
  22.     R_predavanja_domain = ["Mon_10", "Mon_11", "Mon_12", "Mon_13","Mon_14", "Mon_15", "Wed_10", "Wed_11", "Wed_12", "Wed_13","Wed_14", "Wed_15", "Fri_10", "Fri_11", "Fri_12", "Fri_13","Fri_14", "Fri_15"]
  23.     BI_predavanja_domain = ["Mon_10", "Mon_11", "Wed_10", "Wed_11", "Fri_10", "Fri_11"]
  24.    
  25.     AI_vezbi_domain = ["Tue_10", "Tue_11", "Tue_12", "Tue_13", "Thu_10", "Thu_11", "Thu_12", "Thu_13"]
  26.     ML_vezbi_domain = ["Tue_11", "Tue_13", "Tue_14", "Thu_11", "Thu_13", "Thu_14"]
  27.     BI_vezbi_domain = ["Tue_10", "Tue_11", "Thu_10", "Thu_11"]
  28.    
  29.     # ---Tuka dodadete gi promenlivite--------------------
  30.     for i in range(casovi_AI):
  31.         problem.addVariable("AI_cas_" + str(i+1), AI_predavanja_domain)
  32.     for i in range(casovi_ML):
  33.         problem.addVariable("ML_cas_" + str(i+1), ML_predavanja_domain)
  34.     for i in range(casovi_R):
  35.         problem.addVariable("R_cas_" + str(i+1), R_predavanja_domain)
  36.     for i in range(casovi_BI):
  37.         problem.addVariable("BI_cas_" + str(i+1), BI_predavanja_domain)
  38.    
  39.     problem.addVariable("AI_vezbi", AI_vezbi_domain)
  40.     problem.addVariable("ML_vezbi", ML_vezbi_domain)
  41.     problem.addVariable("BI_vezbi", BI_vezbi_domain)
  42.     # ---Tuka dodadete gi ogranichuvanjata----------------
  43.  
  44.     for i in range(casovi_AI):
  45.         for j in range(casovi_ML):
  46.                 problem.addConstraint(what, ('AI_cas_' + str(i+1), 'ML_cas_' + str(j+1)))
  47.                
  48.     for i in range(casovi_AI):
  49.         for j in range(casovi_R):
  50.                 problem.addConstraint(what, ('AI_cas_' + str(i+1), 'R_cas_' + str(j+1)))
  51.                
  52.     for i in range(casovi_AI):
  53.         for j in range(casovi_BI):
  54.                 problem.addConstraint(what, ('AI_cas_' + str(i+1), 'BI_cas_' + str(j+1)))
  55.                
  56.     for i in range(casovi_ML):
  57.         for j in range(casovi_R):
  58.                 problem.addConstraint(what, ('ML_cas_' + str(i+1), 'R_cas_' + str(j+1)))
  59.                
  60.     for i in range(casovi_ML):
  61.         for j in range(casovi_BI):
  62.                 problem.addConstraint(what, ('ML_cas_' + str(i+1), 'BI_cas_' + str(j+1)))
  63.                
  64.     for i in range(casovi_R):
  65.         for j in range(casovi_BI):
  66.                 problem.addConstraint(what, ('R_cas_' + str(i+1), 'BI_cas_' + str(j+1)))
  67.                
  68.     problem.addConstraint(what, ('AI_vezbi', 'ML_vezbi'))
  69.     problem.addConstraint(what, ('AI_vezbi', 'BI_vezbi'))
  70.     problem.addConstraint(what, ('ML_vezbi', 'BI_vezbi'))
  71.    
  72.                
  73.     problem.addConstraint(AllDifferentConstraint())
  74.     for i in range(casovi_BI):
  75.         problem.addConstraint(lambda r1, r2: (r1 == 'Mon_10' and r2 == 'Tue_10') or (r1 == 'Mon_10' and r2 == 'Thu_10') or (r1 == 'Wed_10' and r2 == 'Tue_10') or (r1 == 'Wed_10' and r2 == 'Thu_10') or (r1 == 'Fri_10' and r2 == 'Tue_10') or (r1 == 'Fri_10' and r2 == 'Thu_10') or (r1 == 'Mon_11' and r2 == 'Tue_11') or (r1 == 'Mon_11' and r2 == 'Thu_11') or (r1 == 'Wed_11' and r2 == 'Tue_11') or (r1 == 'Wed_11' and r2 == 'Thu_11') or (r1 == 'Fri_11' and r2 == 'Tue_11') or (r1 == 'Fri_11' and r2 == 'Thu_11'), ('BI_cas_'+str(i+1), 'BI_vezbi'))
  76.     # ----------------------------------------------------
  77.     solution = problem.getSolution()
  78.    
  79.     print("{'BI_vezbi': '%s', " % (solution['BI_vezbi']), end='')
  80.     for i in range(casovi_BI):
  81.         line = "BI_cas_" + str(i+1)
  82.         print("'%s': '%s', " % (line, solution[line]), end='')
  83.     for i in range(casovi_AI):
  84.         line = "AI_cas_" + str(i+1)
  85.         print("'%s': '%s', " % (line, solution[line]), end='')  
  86.     print("'AI_vezbi': '%s', " % (solution['AI_vezbi']), end='')
  87.     print("'ML_vezbi': '%s', " % (solution['ML_vezbi']), end='')
  88.     for i in range(casovi_ML):
  89.         line = "ML_cas_" + str(i+1)
  90.         print("'%s': '%s', " % (line, solution[line]), end='')  
  91.     for i in range(casovi_R):
  92.         line = "R_cas_" + str(i+1)
  93.         if i == (casovi_R - 1):
  94.             print("'%s': '%s'}" % (line, solution[line]))
  95.         else:
  96.             print("'%s': '%s', " % (line, solution[line]))  
  97.    
  98.     # print(solution)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement