-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolver.py
69 lines (52 loc) · 2.19 KB
/
solver.py
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
from constraint import Problem, AllDifferentConstraint, MinConflictsSolver
class Solver(object):
problem = None
pilots, missions, pilot_prefs = None, None, None
uas_max = None
# variables
pilot_vars, mission_vars = [], []
max_obj = 0
def __init__(self, pilot_map=None, mission_map=None, uas_max=None, pilot_prefs=None):
self.problem = Problem(MinConflictsSolver())
self.pilots = pilot_map
self.missions = mission_map
self.uas_max = uas_max
self.pilot_prefs = pilot_prefs
def build_variables(self):
# pilot-uas variable
for key, val in self.pilots.iteritems():
self.problem.addVariable(key, val)
self.pilot_vars.append(key)
# mission-uas variable
for key, val in self.missions.iteritems():
self.problem.addVariable(key, val)
self.mission_vars.append(key)
def build_constraints(self):
# all pilots assigned to different
self.problem.addConstraint(AllDifferentConstraint(), self.pilot_vars)
# missions must be assigned uas only if a pilot has been assigned to it
for mission in self.mission_vars:
mission_and_all_pilots = [mission] + self.pilot_vars
self.problem.addConstraint(
self.__assign__mission_from_assigned_pilots_only, mission_and_all_pilots)
# no uas can be assigned to more than 3 missions
self.problem.addConstraint(self.__limit_mission_uas, self.mission_vars)
def __assign__mission_from_assigned_pilots_only(self, *mission_and_all_pilots):
mission = mission_and_all_pilots[0]
i = 1
for i in range(1, len(mission_and_all_pilots)):
if mission == mission_and_all_pilots[i]:
return True
return False
def __limit_mission_uas(self, *missions):
mission_count = {}
for i in range(0, self.uas_max+1):
mission_count[i] = 0
for mission in missions:
mission_count[mission] += 1
for val in mission_count.itervalues():
if val > 3:
return False
return True
def get_solution(self):
return self.problem.getSolution()