def _Crop(self, c, cnear): """Go from cnear in the direction of (c - cnear) until either reaching c or one of the robot reaching its goal. Returns ------- cnew : Config cropped : bool True if some robots have reached their goals, else False. """ dc = (c - cnear) / Norm(c - cnear) # unit vector from cnear to c cropped = False # indicates if any DOF has been cropped reachingIndex = None # the index of the robot which reaches its goal first minMult = np.infty # Check if any robot has reached its goal for i in c.activeIndices: if c[i] >= self.configGoal[i]: if abs(dc[i]) < EPSILON: continue cropped = True k = (self.configGoal[i] - cnear[i]) / dc[i] if k < minMult: minMult = k reachingIndex = i activeIndices = c.activeIndices if not cropped: cnew = Config(c, activeIndices) else: activeIndices.remove(reachingIndex) cnew = Config(cnear + dc * minMult, sorted(activeIndices)) return cnew, cropped
def test_get_solutions_json(self): with self.unpack_named_src('problem.json') as conf_file: cfg = Config() self.assertCountEqual(cfg.get_solutions(), [('ks_fast', 'solutions/floors_ks_fast.cpp'), ('floors_ks.cpp', 'solutions/floors_ks.cpp'), ('floors_ks_slow.cpp', 'solutions/floors_ks_slow.cpp')])
def test_get_solutions_ini(self): with self.unpack_named_src('problem.conf') as conf_file: cfg = Config() self.assertCountEqual(cfg.get_solutions(), [('ks_py', 'solutions/floors_ks.py'), ('ks', 'solutions/floors_ks.cpp'), ('ks_slow', 'solutions/floors_ks_slow.cpp')])
def _Initialize(self): """ """ self.nrobots = len(self.cd.robots) configStart = Config([0.] * self.nrobots) self.treeStart = Tree(Vertex(configStart)) self.currentRoot = configStart self.currentActiveIndices = self.currentRoot.activeIndices # nearest neighbor computation considers only vertices with # their indices greater than _offsetIndex. This is basically a # way to removed finished robots. self._offsetIndex = 0 self.configGoal = Config( [robot.pathLength for robot in self.cd.robots])
def SampleConfig(self): """Sample a new configuration (in the coordination diagram). Elements at inactive indices are filled with the corresponding elements in the goal configuration. """ if self.params.useVirtualBoxes: upperBounds = [max(self.configGoal) * self.params.expansionCoeff ] * self.nrobots else: upperBounds = self.configGoal config = Config([ self._RNG(u, l) if i in self.currentActiveIndices else self.configGoal[i] for i, (u, l) in enumerate(zip(upperBounds, self.currentRoot)) ], activeIndices=self.currentActiveIndices) return config
def Extend(self, crand): status = TRAPPED nnIndices = self.treeStart.GetKNNIndices(self.params.nn, crand, self._offsetIndex) # Iterate though the list of nearest neighbors for index in nnIndices: vnear = self.treeStart[index] cnear = vnear.config ################################################################################ # Manage spacial robots dc = crand - cnear # Robots with velocity constraints for indices in vnear.constrainedIndexSets: # vmin = min([dc[i] for i in indices]) # Sometimes, vmin is too little such that robots get # stuck. Maybe we need to add some randomness when selecting a # value. vconstrained = max([dc[i] for i in indices]) for i in indices: dc[i] = vconstrained # Waiting robots for i in vnear.waitingIndices: dc[i] = 0 q = [x + y for (x, y) in zip(cnear, dc)] crand_new = Config(q, crand.activeIndices) ################################################################################ dist = Distance(cnear, crand_new) # Limit the new Config to be within rrtStepsize units from its neighbor if dist <= self.params.rrtStepSize: cnew = crand_new else: dc = crand_new - cnear # direction from cnear to cnew if Norm(dc) <= EPSILON: # import IPython; IPython.embed(header="Extend (norm(dc) == 0)") return TERMINATED cnew = cnear + dc * (self.params.rrtStepSize / Norm(dc)) if Norm(cnew - cnear) <= EPSILON: # import IPython; IPython.embed(header="Extend (norm(ctest - cnear) == 0)") return TERMINATED # Check for collisions. # Note that vnear is also an input to this collision checking # procedure in order for us to be able to update waitingIndices. if self.cd.CheckCollisionConfig(cnew, self.currentActiveIndices, vnear): continue if self.cd.CheckCollisionSegment(cnear, cnew, self.currentActiveIndices, vnear): continue # Add the new vertex to the tree vnew = Vertex(cnew) self.treeStart.AddVertex(index, vnew) status = ADVANCED if np.random.random() <= self.params.goalBias: # Attempt connection. Note that we use CheckCollisionSegment2 here. if not self.cd.CheckCollisionSegment2( cnew, self.configGoal, self.currentActiveIndices): vgoal = Vertex(self.configGoal) self.treeStart.AddVertex(vnew.index, vgoal) status = REACHED return status # Check if the newly added vertex is near any conflict zone self.CheckAndPreventConflicts() return status return status
import sockjs.tornado from zmq.utils import jsonapi try: from sage.all import gap, gp, maxima, r, singular tab_completion = { "gap": gap._tab_completion(), "gp": gp._tab_completion(), "maxima": maxima._tab_completion(), "r": r._tab_completion(), "singular": singular._tab_completion() } except ImportError: tab_completion = {} from misc import sage_json, Config config = Config() class RootHandler(tornado.web.RequestHandler): """ Root URL request handler. This renders templates/root.html, which optionally inserts specified preloaded code during the rendering process. There are three ways currently supported to specify preloading code: ``<root_url>?c=<code>`` loads 'plaintext' code ``<root_url>?z=<base64>`` loads base64-compressed code ```<root_url>?q=<uuid>`` loads code from a database based
def CheckCollisionConfig(self, config, activeIndices, vnear=None): """Check for both virtual and actual collisions. """ inCollision = False positions = [ self.robots[i].Locate(config[i])[0] for i in activeIndices ] # Check for virtual collisions (trapped in deadlocks). a = self._a b = self._b for (iconflict, conflict) in enumerate(self.conflicts): if conflict.type != ConflictType.DEADLOCK: continue if not set(conflict.indices).issubset(set(activeIndices)): continue indices = conflict.indices svect = Config([config[rindex] for rindex in indices]) # svect_start = Config([conflict.intervals[rindex][0] - a # for rindex in indices]) # svect_end = Config([conflict.intervals[rindex][1] + b # for rindex in indices]) svect_start = Config( [conflict.intervals[rindex][0] for rindex in indices]) svect_end = Config( [conflict.intervals[rindex][1] for rindex in indices]) if svect.Dominates(svect_start) and svect_end.Dominates(svect): msg = " Config in deadlock {0}: Robot {1}".format\ (iconflict, indices[0]) for index in indices[1:]: msg += " & Robot {0}".format(index) log.debug(msg) inCollision = True if vnear is not None: log.debug(" vnear.index = {0}".format(vnear.index)) return inCollision # Check for actual collisions for i in xrange(len(activeIndices)): index1 = activeIndices[i] for j in xrange(i + 1, len(activeIndices)): index2 = activeIndices[j] diff = positions[i] - positions[j] dist = np.sqrt(np.dot(diff, diff)) if dist <= 2 * self.params.collisionRadius: log.debug(" Config in collision: Robot {0} & Robot {1}". format(index1, index2)) if vnear is not None: log.debug(" vnear.index = {0}".format(vnear.index)) inCollision = True if vnear is not None: if index1 in vnear.waitingIndices or index2 in vnear.waitingIndices: # If one of the colliding robot is a waiting # robot, the other robot should also wait. if index1 in vnear.waitingIndices: vnear.waitingIndices.append(index2) elif index2 in vnear.waitingIndices: vnear.waitingIndices.append(index1) else: for conflict in self.conflicts: # if conflict.type != ConflictType.SHARED: # continue # Consider both shared segments and junctions. if conflict.type == ConflictType.DEADLOCK: continue if (not (index1 in conflict.indices and index2 in conflict.indices)): continue s1_start, s1_end = conflict.intervals[index1] s2_start, s2_end = conflict.intervals[index2] s1 = config[index1] s2 = config[index2] rem1 = s1_end - s1 rem2 = s2_end - s2 # The robot that is behind the other shall wait. if rem1 > rem2: vnear.waitingIndices.append(index1) else: vnear.waitingIndices.append(index2) vnear.waitingIndices = sorted( list(set(vnear.waitingIndices))) return inCollision return inCollision
from misc import Offer, Search, Offerbase, Config import misc import linkedin, pracujpl import csv import subprocess import os import concurrent.futures #Create new offers list newoffers = [] newoffers_pracujpl = [] newoffers_linkedin = [] #Read config file config = Config('config.json') #Create separate threads for linkedin and pracuj.pl with concurrent.futures.ThreadPoolExecutor() as executor: linkedin = executor.submit(linkedin.linkedin_search, config) pracuj = executor.submit(pracujpl.pracujpl_search, config) newoffers_linkedin = linkedin.result() newoffers_pracujpl = pracuj.result() newoffers = newoffers_pracujpl + newoffers_linkedin #Update log db = Offerbase('offers.db') for task in config.searches: db.update_log(Search(task['jobtitle'], task['location'])) db.close()
def CheckAndPreventConflicts(self): """Check if the newly added config is near any conflict. If so, apply the conflict prevention policy. """ v = self.treeStart[-1] # the newly added vertex c = v.config waitingIndices = [] constrainedIndexSets = [] a = self.cd._a b = self.cd._b d = self.params.preventionDistance # When there is (seems to be) freedom to choose which robot to # wait, we need to be more careful since the choice we make # can affect the problem. The order in which we iterate # through the conflict also affects the problem. # decisionSets is a list of sublists. Each sublist contains # robot indices among which we can choose them to # wait. Suppose a sublist is [rindex1, rindex2]. There might # be some future conflict that forces, e.g., rindex1 to # wait. Then we remove the sublist. decisionSets = [] # movingIndices is a list of sublists. Each sublist contains robot # indices among which at least one robot MUST BE MOVING. (Otherwise, a # configuration will be stuck in a deadlock.) movingIndices = [] for conflict in self.cd.conflicts: if conflict.type == ConflictType.DEADLOCK: # indices store active indices that are related to this deadlock. if not set(conflict.indices).issubset( set(self.currentActiveIndices)): continue indices = sorted(conflict.indices) # indices = sorted(list(set(conflict.indices) & set(self.currentActiveIndices))) # if len(indices) < 2: # continue svect = Config([c[rindex] for rindex in indices]) # svect_inner = Config([conflict.intervals[rindex][0] - a # for rindex in indices]) svect_inner = Config( [conflict.intervals[rindex][0] for rindex in indices]) svect_outer = Config([s - d for s in svect_inner]) # svect_max = Config([conflict.intervals[rindex][1] + b # for rindex in indices]) svect_max = Config( [conflict.intervals[rindex][1] for rindex in indices]) if not (svect.Dominates(svect_outer) and svect_max.Dominates(svect)): # This config is not near this deadlock obstacle continue if svect_inner.Dominates(svect): # All robots that are related to this deadlock have not # entered the deadlock yet. intersect = list(set(indices) & set(waitingIndices)) if len(intersect) > 0: # At least one robot is waiting so that's fine. continue else: # In this case, we will decide later which one to wait. decisionSets.append(indices) else: # Some robots have already entered the deadlock. See first # which robots have already entered or not entered. diff = svect - svect_inner decisionSet = [] # this list keeps the indices of robots # outside the deadlock for (ds, index) in zip(diff, indices): if ds < 0: # This robot has not entered the deadlock. decisionSet.append(index) intersect = list(set(decisionSet) & set(waitingIndices)) if len(intersect) > 0: # At least one robot outside the deadlock is already # waiting. Do nothing here. continue else: if len(decisionSet) == 1: # # If we reach this case, the robot with index # # decisionSet[0] is forced to wait since it is THE # # ONLY ONE that has not entered the deadlock yet. We # # also need to make sure that at least one of the # # other robots MUST BE moving. # waitingIndices.append(decisionSet[0]) # if len(indices) > 2: # # We may need to examine this case more closely. # # IPython.embed(header="len(conflict.indices) > 2") # moving = list(set(indices) - set(decisionSet)) # movingIndices.append(moving) # else: # # index is the index of the robot that must be moving. # index = list(set(indices) - set(decisionSet))[0] # for decisionSet in decisionSets: # # Remove any appearance of index in decision sets. # try: # decisionSet.remove(index) # except: # pass # newDecisionSets = [] # for decisionSet in decisionSets: # if len(decisionSet) == 0: # # This should not be the case # raise ValueError # elif len(decisionSet) == 1: # # The decision has been made. # waitingIndices.append(decisionSet[0]) # else: # intersect = list(set(waitingIndices) & set(decisionSet)) # if len(intersect) > 0: # continue # else: # newDecisionSets.append(decisionSet) # decisionSets = newDecisionSets if decisionSet[0] not in waitingIndices: waitingIndices.append(decisionSet[0]) else: # In this case, we decide later decisionSets.append(decisionSet) else: # Elementary conflict (shared segment or junction) rindex1, rindex2 = conflict.indices if ((rindex1 not in self.currentActiveIndices) or (rindex2 not in self.currentActiveIndices)): # At least one robot is inactive: this conflict is not relevant continue s1 = c[rindex1] s2 = c[rindex2] s1_start, s1_end = conflict.intervals[rindex1] s2_start, s2_end = conflict.intervals[rindex2] if conflict.type == ConflictType.SHARED: # SHARED SEGMENT # if ((s1_start - a - d <= s1 <= s1_start) and # (s2_start - a - d <= s2 <= s2_start - a)): # # Both robots have not entered the shared segment # # yet. So we keep them as candidates. # decisionSet = [rindex1, rindex2] # decisionSets.append(decisionSet) # elif ((s1_start - a - d <= s1 <= s1_start - a) and # (s2_start - a <= s2 <= s2_start)): # # Both robots have not entered the shared segment # # yet. So we keep them as candidates. # decisionSet = [rindex1, rindex2] # decisionSets.append(decisionSet) if ((s1_start - d <= s1 <= s1_start) and (s2_start - d <= s2 <= s2_start)): # Both robots have not entered the shared segment # yet. So we keep them as candidates. decisionSet = [rindex1, rindex2] decisionSets.append(decisionSet) elif ((s1_start - d <= s1 <= s1_start + a) and (s2_start - d <= s2 <= s2_start + a)): if s1 >= s1_start: waitingIndices.append(rindex2) else: waitingIndices.append(rindex1) # elif ((s1_start - a - d <= s1 <= s1_end + a) and # (s2_start - a - d <= s2 <= s2_end + a)): elif ((s1_start - d <= s1 <= s1_end) and (s2_start - d <= s2 <= s2_end)): # This configuration is inside a velocity constrained # region. (Both robots are inside a shared segment so # they should move at the same speed.) indices = [rindex1, rindex2] # Now we need to check if robot1/robot2 is already part # of any other shared segment conflict. appeared = False for i in xrange(len(constrainedIndexSets)): if ((rindex1 in constrainedIndexSets[i]) or (rindex2 in constrainedIndexSets[i])): newIndices = list( set(constrainedIndexSets[i] + indices)) constrainedIndexSets[i] = newIndices appeared = True break if not appeared: constrainedIndexSets.append(indices) else: # JUNCTION # if ((s1_start - d <= s1 <= s1_end) and # (s2_start - d <= s2 <= s2_end)): # # Both robots almost reach the junction. # decisionSet = [rindex1, rindex2] # decisionSets.append(decisionSet) if ((s1_start - d <= s1 <= s1_start) and (s2_start - d <= s2 <= s2_start)): # Both robots are about to cross this junction. decisionSet = [rindex1, rindex2] decisionSets.append(decisionSet) elif ((s1_start - d <= s1 <= s1_end) and (s2_start - d <= s2 <= s2_end)): rem1 = s1_end - s1 rem2 = s2_end - s2 # The robot that is behind the other shall wait. if rem1 > rem2: waitingIndices.append(rindex1) else: waitingIndices.append(rindex2) # Now we have already iterated through all conflicts. # Manage the pending decisions. newDecisionSets = [] for decisionSet in decisionSets: intersect = list(set(waitingIndices) & set(decisionSet)) if len(intersect) > 0: continue else: newDecisionSets.append(decisionSet) decisionSets = newDecisionSets if len(decisionSets) > 0: appearance = dict() for decisionSet in decisionSets: for el in decisionSet: if el not in appearance: appearance[el] = 1 else: appearance[el] += 1 # Sort the dictionary `appearance` by its values (descending order). sorted_appearance = sorted(appearance.items(), key=itemgetter(1)) # Idea: indices that appear most often should be waiting so as to # minimize the waiting robots. for item in sorted_appearance: index, _ = item newDecisionSets = [] for decisionSet in decisionSets: try: decisionSet.remove(index) waitingIndices.append(index) except: pass if len(decisionSet) > 1: newDecisionSets.append(decisionSet) decisionSets = newDecisionSets if len(decisionSets) == 0: break if len(decisionSets) > 0: # All pending decisions should have already been made. raise ValueError for movingSet in movingIndices: if len(list(set(movingSet) - set(waitingIndices))) == 0: import IPython IPython.embed(header="error with moving indices") # Manage constrained index sets # Idea: if there are three robot in the shared segment but the one # behind is waiting, that robot should not affect the other two. If the # one waiting is not the one behind, the moving one will eventually # collide with it and be made waiting anyway. waitingSet = set(waitingIndices) newconstrainedset = [] for constrainedset in constrainedIndexSets: constrainedset = list(set(constrainedset) - waitingSet) if len(constrainedset) > 1: newconstrainedset.append(constrainedset) v.waitingIndices = sorted(list(set(waitingIndices))) v.constrainedIndexSets = newconstrainedset # constrainedIndexSets return
import trusted_kernel_manager from misc import assert_is, assert_equal, assert_in, assert_not_in, assert_raises, assert_regexp_matches, assert_is_instance, assert_is_not_none, assert_greater, assert_len, assert_uuid, capture_output, Config import random import os import sys import ast import zmq from IPython.zmq.session import Session from contextlib import contextmanager import time import re import config_default as conf sage = conf.sage configg = Config() d = configg.get_default_config("_default_config") from IPython.testing.decorators import skip def test_init(): tmkm = trusted_kernel_manager.TrustedMultiKernelManager() assert_len(tmkm._kernels.keys(), 0) assert_len(tmkm._comps.keys(), 0) assert_len(tmkm._clients.keys(), 0) assert_is(hasattr(tmkm, "context"), True) def test_init_parameters(): tmkm = trusted_kernel_manager.TrustedMultiKernelManager(default_computer_config = {"a": "b"}, kernel_timeout = 3.14) assert_equal(tmkm.default_computer_config, {"a": "b"}) assert_equal(tmkm.kernel_timeout, 3.14)
capture_output, Config, ) import random import os import sys import ast import zmq from IPython.zmq.session import Session from contextlib import contextmanager import time import re import config_default as conf sage = conf.sage configg = Config() d = configg.get_default_config("_default_config") from IPython.testing.decorators import skip def test_init(): tmkm = trusted_kernel_manager.TrustedMultiKernelManager() assert_len(tmkm._kernels.keys(), 0) assert_len(tmkm._comps.keys(), 0) assert_len(tmkm._clients.keys(), 0) assert_is(hasattr(tmkm, "context"), True) def test_init_parameters(): tmkm = trusted_kernel_manager.TrustedMultiKernelManager(default_computer_config={"a": "b"}, kernel_timeout=3.14)
def test_get_not_existing_problem_param_json(self): with self.unpack_named_src('problem.json') as conf_file: cfg = Config() self.assertIsNone(cfg.get_problem_param('qwerty')) with self.assertRaises(KeyError): cfg.get_problem_param('qwerty', False)
def test_get_bool_problem_param_json(self): with self.unpack_named_src('problem.json') as conf_file: cfg = Config() self.assertFalse(cfg.get_problem_param('use_doall')) self.assertFalse(cfg.get_problem_param('qwerty'))
def test_get_problem_param_json(self): with self.unpack_named_src('problem.json') as conf_file: cfg = Config() self.assertEqual(cfg.get_problem_param('title', False), '') self.assertEqual(cfg.get_problem_param('title'), '')
def test_get_main_solution_json(self): with self.unpack_named_src('problem.json') as conf_file: cfg = Config() self.assertEqual(cfg.get_main_solution(), 'solutions/floors_ks.cpp')