prometheus800

ВИ Лаб 2: Менаџирање на состаноци (CSP)

May 15th, 2021
1,336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. from constraint import *
  2.  
  3. if __name__ == '__main__':
  4.     problem = Problem(BacktrackingSolver())
  5.  
  6.     # ---------- Add variables and their domain ----------
  7.     problem.addVariable("Marija_prisustvo", [0, 1])
  8.     problem.addVariable("Simona_prisustvo", [0, 1])
  9.     problem.addVariable("Petar_prisustvo", [0, 1])
  10.     problem.addVariable("vreme_sostanok", range(12, 21))
  11.     # ----------------------------------------------------
  12.  
  13.     # Time schedules
  14.     simona_free_time = list(range(13, 15)) + list(range(16, 17)) + list(range(19, 20))
  15.     marija_free_time = list(range(14, 16)) + list(range(18, 19))
  16.     petar_free_time = list(range(12, 14)) + list(range(16, 20))
  17.  
  18.     # ------------------------------------- Constraints begin here -----------------------------------------
  19.     # Simona must be there
  20.     problem.addConstraint(InSetConstraint({1}), ["Simona_prisustvo"])
  21.  
  22.     # At least one other member should be there with Simona
  23.     problem.addConstraint(SomeInSetConstraint({1}), ["Petar_prisustvo", "Marija_prisustvo"])
  24.  
  25.     # Constraints for the time schedule (e.g simona is available at those times)
  26.     problem.addConstraint(lambda person, time: person == 1 and time in simona_free_time,
  27.                           ["Simona_prisustvo", "vreme_sostanok"])
  28.  
  29.     problem.addConstraint(lambda person, time: person == 1 if time in marija_free_time else person == 0,
  30.                           ["Marija_prisustvo", "vreme_sostanok"])
  31.  
  32.     problem.addConstraint(lambda person, time: person == 1 if time in petar_free_time else person == 0,
  33.                           ["Petar_prisustvo", "vreme_sostanok"])
  34.     # ------------------------------------------------------------------------------------------------------
  35.  
  36.     [print(solution) for solution in problem.getSolutions()]
  37.  
Add Comment
Please, Sign In to add comment