New Inventory type for TD
[optf/has.git] / conductor / conductor / solver / optimizer / random_pick.py
index 79750a6..7fb2ced 100644 (file)
@@ -45,9 +45,15 @@ class RandomPick(search.Search):
             _decision_path.current_demand = demand
             candidate_list = self._solve_constraints(_decision_path, _request)
 
-            # random pick one candidate
-            r_index = randint(0, len(candidate_list) - 1)
-            best_resource = candidate_list[r_index]
-            _decision_path.decisions[demand.name] = best_resource
+            # When you have two demands and for one there are no candidates left after filtering by constraints
+            # the code tries to randomly choose index randint(0, -1) what raises an exception. None is returned
+            # in order to prevent that and this case is considered in the Optimizer respectively
+            if len(candidate_list) > 0:
+                # random pick one candidate
+                r_index = randint(0, len(candidate_list) - 1)
+                best_resource = candidate_list[r_index]
+                _decision_path.decisions[demand.name] = best_resource
+            else:
+                return None
 
         return _decision_path