def test_reverseMDP(plot=False): greed = 0.9 phobia = 0.1 angStep = 1 downsample = int(numpy.floor(3 / angStep)) xPos, yPos = utils.hexFromDia(25, pitch=22.4) seed = 1 rg = RobotGrid(stepSize=angStep, epsilon=epsilon, seed=seed) for robotID, (x, y) in enumerate(zip(xPos, yPos)): rg.addRobot(robotID, str(robotID), [x, y, 0], hasApogee) rg.setCollisionBuffer(collisionBuffer) rg.initGrid() for rID in rg.robotDict: robot = rg.getRobot(rID) robot.setXYUniform() assert rg.getNCollisions() > 10 rg.decollideGrid() for robot in rg.robotDict.values(): robot.setDestinationAlphaBeta(0, 180) assert rg.getNCollisions() == 0 rg.pathGenMDP(greed, phobia) if plot: utils.plotPaths(rg, downsample=downsample, filename="reverseMDP.mp4")
def test_fatty(plot=False): xPos, yPos = utils.hexFromDia(11, pitch=22.4) print("n robots", len(xPos)) angStep = 0.1 greed = 0.8 phobia = 0.2 collisionBuffer = 4 downsample = int(numpy.floor(3 / angStep)) rg = RobotGrid(stepSize=angStep, epsilon=epsilon, seed=1) for robotID, (x, y) in enumerate(zip(xPos, yPos)): rg.addRobot(robotID, str(robotID), [x, y, 0], hasApogee) rg.setCollisionBuffer(collisionBuffer) rg.initGrid() for rID in rg.robotDict: robot = rg.getRobot(rID) robot.setXYUniform() robot.setDestinationAlphaBeta(90, 180) # assert rg.getNCollisions() > 10 rg.decollideGrid() rg.pathGenMDP(greed, phobia) # rg.smoothPaths(3) # rg.simplifyPaths() # rg.verifySmoothed() if plot: utils.plotPaths(rg, downsample=downsample, filename="fatty.mp4")
def test_forwardMDP(plot=False): xPos, yPos = utils.hexFromDia(25, pitch=22.4) seed = 1 rg = RobotGrid(stepSize=angStep, epsilon=epsilon, seed=seed) for robotID, (x, y) in enumerate(zip(xPos, yPos)): rg.addRobot(robotID, str(robotID), [x, y, 0], hasApogee) rg.setCollisionBuffer(collisionBuffer) rg.initGrid() for rID in rg.robotDict: robot = rg.getRobot(rID) robot.setXYUniform() assert rg.getNCollisions() > 10 rg.decollideGrid() print("N collisions 1", rg.getNCollisions()) for robot in rg.robotDict.values(): robot.setDestinationAlphaBeta(robot.alpha, robot.beta) robot.setAlphaBeta(0, 180) print("N collisions 2", rg.getNCollisions()) # assert rg.getNCollisions() == 0 rg.pathGenMDP(0.9, 0.1) if plot: utils.plotPaths(rg, filename="forwardMDP.mp4")
def test_initialConfigs(plot=False): xPos, yPos = utils.hexFromDia(21, pitch=22.4) angStep = 0.1 greed = 0.8 phobia = 0.2 downsample = int(numpy.floor(10 / angStep)) rg = RobotGrid(stepSize=angStep, epsilon=epsilon, seed=1) for robotID, (x, y) in enumerate(zip(xPos, yPos)): rg.addRobot(robotID, str(robotID), [x, y, 0], hasApogee) rg.setCollisionBuffer(collisionBuffer) rg.initGrid() for rID in rg.robotDict: robot = rg.getRobot(rID) robot.setXYUniform() assert rg.getNCollisions() > 10 if plot: utils.plotOne(-1, rg, figname="angStepO.png", isSequence=False) rg.decollideGrid() for robot in rg.robotDict.values(): robot.setDestinationAlphaBeta(0, 180) if plot: utils.plotOne(-1, rg, figname="angStepD.png", isSequence=False) rg.pathGenMDP(greed, phobia) if plot: utils.plotOne(-1, rg, figname="angStepE.png", isSequence=False) if plot: utils.plotPaths(rg, downsample=downsample, filename="init.mp4")
def doOne(seed, flip90=False): rg = RobotGrid(angStep, collisionBuffer, 2.2, seed) xPos, yPos = utils.hexFromDia(nDia, pitch=22.4) for ii, (xp, yp) in enumerate(zip(xPos, yPos)): if flip90: xrot = cos * xp + sin * yp yrot = sin * xp - cos * yp rg.addRobot(ii, xrot, yrot, hasApogee) else: rg.addRobot(ii, xp, yp, hasApogee) rg.initGrid() for ii in range(rg.nRobots): r = rg.getRobot(ii) r.setXYUniform() # r.setXYUniform can give nan values for alpha beta? # set all positioners randomly (they are initialized at 0,0) rg.decollide2() rg.pathGen() # calling decollide twice breaks things? robotsFolded = 0 for r in rg.allRobots: if r.alpha == 0 and r.beta == 180: robotsFolded += 1 # if rg.didFail: # filename = "seed_%i"%seed # if flip90: # filename += "_flipped" # else: # filename += "_notflipped" # utils.plotOne(1, rg, figname=filename+".png", isSequence=False, internalBuffer=collisionBuffer) return robotsFolded
def newGrid(seed=0): hasApogee = True rg = RobotGrid(angStep, collisionBuffer, epsilon, seed) for posID, (xp, yp) in posDict.items(): rg.addRobot(posID, xp, yp, hasApogee) rg.initGrid() for ii in range(rg.nRobots): r = rg.getRobot(ii) r.setXYUniform() rg.decollide2() return rg
def safeGrid(): hasApogee = True seed = 0 rg = RobotGrid(angStep, collisionBuffer, epsilon, seed) for posID, (xp, yp) in posDict.items(): rg.addRobot(posID, xp, yp, hasApogee) rg.initGrid() for ii in range(rg.nRobots): r = rg.getRobot(ii) r.setAlphaBeta(45, 90) rg.decollide2() return rg
def getGrid(angStep, cbuff, seed, nDia): rg = RobotGrid(stepSize=angStep, collisionBuffer=cbuff, epsilon=angStep * 2, seed=seed) xPos, yPos = xPos, yPos = utils.hexFromDia(nDia, pitch=22.4, rotAngle=90) for robotID, (x, y) in enumerate(zip(xPos, yPos)): rg.addRobot(robotID, x, y, hasApogee) rg.initGrid() for robot in rg.robotDict.values(): robot.setXYUniform() robot.setDestinationAlphaBeta(10, 170) rg.decollideGrid() # print("nCollisions in getGrid", rg.getNCollisions()) return rg
def test_unevenCBs(plot=False): hasApogee = True greed = 0.8 phobia = 0.2 xPos, yPos = utils.hexFromDia(17, pitch=22.4) seed = 1 # cb = 2.5 cs = 0.04 step = 0.1 # degrees per step in kaiju's rough path smoothPts = 10 # width of velocity smoothing window eps = step * 2.2 # downsample = int(numpy.floor(50 / step)) rg = RobotGrid(stepSize=step, epsilon=eps, seed=seed) for robotID, (x, y) in enumerate(zip(xPos, yPos)): rg.addRobot(robotID, str(robotID), [x, y, 0], hasApogee, collisionBuffer=numpy.random.uniform(1.5, 3.5)) rg.initGrid() for rID in rg.robotDict: robot = rg.getRobot(rID) robot.setXYUniform() assert rg.getNCollisions() > 10 rg.decollideGrid() for robot in rg.robotDict.values(): robot.setDestinationAlphaBeta(0, 180) assert rg.getNCollisions() == 0 tstart = time.time() rg.pathGenMDP(greed, phobia) print("pathgen took", time.time() - tstart) rg.smoothPaths(smoothPts) rg.simplifyPaths() rg.shrinkCollisionBuffer(cs) rg.verifySmoothed() assert rg.smoothCollisions == 0 print("n smooth collisions", rg.smoothCollisions) if plot: for r in rg.robotDict.values(): utils.plotTraj(r, "unevenCBs", dpi=250) utils.plotPaths(rg, downsample=3, filename="unevenCBs.mp4")
def test_setMDP(plot=False): greed = 0.8 phobia = 0.2 xPos, yPos = utils.hexFromDia(45, pitch=22.4) print("using ", len(xPos), "robots") # collisionBuffer = 3 angStep = 0.5 collisionBuffer = 3 downsample = int(numpy.floor(3 / angStep)) for seed in range(100): rg = RobotGrid(stepSize=angStep, epsilon=epsilon, seed=seed) for robotID, (x, y) in enumerate(zip(xPos, yPos)): rg.addRobot(robotID, str(robotID), [x, y, 0], hasApogee) rg.setCollisionBuffer(collisionBuffer) rg.initGrid() for rID in rg.robotDict: robot = rg.getRobot(rID) robot.setXYUniform() assert rg.getNCollisions() > 10 rg.decollideGrid() for robot in rg.robotDict.values(): robot.setDestinationAlphaBeta(90, 180) assert rg.getNCollisions() == 0 rg.pathGenMDP(greed, phobia) deadlockedRobots = [] for r in rg.robotDict.values(): # if not r.onTargetVec[-1]: if r.score() > 0: deadlockedRobots.append(r.id) if len(deadlockedRobots): print("seed", seed, "failed with these", deadlockedRobots, "in ", rg.nSteps) break else: print("seed", seed, "didn't fail", rg.nSteps, " taken to solve") if plot: utils.plotPaths(rg, downsample=downsample, filename="reverseSetMDP.mp4")
def test_tofile(plot=False): xPos, yPos = utils.hexFromDia(37, pitch=22.4, rotAngle=90) print("n robots", len(xPos)) angStep = 1 greed = 0.8 phobia = 0.2 downsample = int(numpy.floor(3 / angStep)) rg = RobotGrid(stepSize=angStep, epsilon=epsilon, seed=1) for robotID, (x, y) in enumerate(zip(xPos, yPos)): rg.addRobot(robotID, str(robotID), [x, y, 0], hasApogee) rg.setCollisionBuffer(collisionBuffer) rg.initGrid() for rID in rg.robotDict: robot = rg.getRobot(rID) robot.setXYUniform() assert rg.getNCollisions() > 10 rg.decollideGrid() for robot in rg.robotDict.values(): robot.setDestinationAlphaBeta(10, 170) rg.pathGenMDP(greed, phobia) # rg.smoothPaths(3) # rg.simplifyPaths() # rg.verifySmoothed() # t1 = time.time() # rg.summaryJSON("json.txt") # print("json took", (time.time()-t1)) # t1 = time.time() # rg.summaryPickle("rg.pkl") # print("pickle took", (time.time()-t1)) # t1 = time.time() # g = json.load(open("json.txt", "r")) # print("json load took", (time.time()-t1)) # t1 = time.time() # g = pickle.load(open("rg.pkl", "rb")) # print("pickle load took", (time.time()-t1)) if plot: utils.plotOne(-1, rg, figname="tofile.png", isSequence=False)
def getValidAssignments(seed): """seed is the random seed with which to initialze the RobotGrid return dictionary keyed by positioner id with the coordinates of the metrology fiber. These represent valid (non-collided) xy Fiber positions for each robot """ rg = RobotGrid(seed=seed) for ii, (xp, yp) in enumerate(zip(xCoords, yCoords)): rg.addRobot(robotID=ii, xPos=xp, yPos=yp) rg.initGrid() # give all robots an initial (radom) target configuration for robot in rg.robotDict.values(): # assigns a robot a target picked uniformly in xy # from its patrol annulus robot.setXYUniform() # decollide any colliding robots so that we have a completely # non-colliding target configuration rg.decollideGrid() targetPos = {} for robot in rg.robotDict.values(): targetPos[robot.id] = robot.metFiberPos[:-1] # xy coord, drop the z return targetPos
if __name__ == "__main__": minPoints = 30 cos = numpy.cos(numpy.radians(90)) sin = numpy.sin(numpy.radians(90)) seed = 5000 while True: seed += 1 print("trying seed", seed) rg = RobotGrid(angStep, collisionBuffer, epsilon, seed) xPos, yPos = utils.hexFromDia(nDia, pitch=pitch) for ii, (xp, yp) in enumerate(zip(xPos, yPos)): xrot = cos * xp + sin * yp yrot = sin * xp - cos * yp # print("%.8f, %.8f"%(xrot,yrot)) rg.addRobot(ii, xrot, yrot, hasApogee) rg.initGrid() for ii in range(rg.nRobots): r = rg.getRobot(ii) r.setXYUniform() # set all positioners randomly (they are initialized at 0,0) rg.decollide2() rg.pathGen() rg.smoothPaths(smoothPts) # must be odd rg.simplifyPaths() rg.setCollisionBuffer(collisionBuffer - collisionShrink) rg.verifySmoothed() # find max beta path points # only plot if we've found minimum # amount of points
def generatePath(seed=0, plot=False, movie=False): nDia = 3 pitch = 22.4 hasApogee = True rg = RobotGrid(angStep, collisionBuffer, epsilon, seed) # epfl grid is sideways # xPos, yPos = utils.hexFromDia(nDia, pitch=pitch) # for ii, (xp, yp), posID in enumerate(zip(xPos, yPos, posList)): # rg.addRobot(posID, xp, yp, hasApogee) # rg.initGrid() for posID, (xp, yp) in posDict.items(): # print("loading pos %i [%.2f, %.2f]"%(posID, xp, yp)) rg.addRobot(posID, xp, yp, hasApogee) rg.initGrid() for ii in range(rg.nRobots): r = rg.getRobot(ii) # print("loading pos %i [%.2f, %.2f]"%(r.id, r.xPos, r.yPos)) # print("robotID", r.id) r.setXYUniform() # set all positioners randomly (they are initialized at 0,0) rg.decollide2() rg.pathGen() if rg.didFail: print("path gen failed") raise (RuntimeError, "path gen failed") rg.smoothPaths(smoothPts) rg.simplifyPaths() rg.setCollisionBuffer(collisionBuffer - collisionShrink) rg.verifySmoothed() if rg.smoothCollisions: print("smoothing failed with ", rg.smoothCollisions) raise (RuntimeError, "smoothing failed") if movie: plotMovie(rg, filename="movie_%i" % seed) # find the positioner with the most interpolated steps forwardPath = {} reversePath = {} for robotID, r in zip(posDict.keys(), rg.allRobots): assert robotID == r.id if plot: plotTraj(r, "seed_%i_" % seed, dpi=250) # bp = numpy.array(r.betaPath) # sbp = numpy.array(r.interpSmoothBetaPath) ibp = numpy.array(r.simplifiedBetaPath) # ap = numpy.array(r.alphaPath) # sap = numpy.array(r.interpSmoothAlphaPath) iap = numpy.array(r.simplifiedAlphaPath) # generate kaiju trajectory (for robot 23) # time = angStep * stepNum / speed alphaTimesR = iap[:, 0] * angStep / RobotMaxSpeed alphaDegR = iap[:, 1] betaTimesR = ibp[:, 0] * angStep / RobotMaxSpeed betaDegR = ibp[:, 1] armPathR = {} # reverse path armPathR["alpha"] = [(pos, time) for pos, time in zip(alphaDegR, alphaTimesR)] armPathR["beta"] = [(pos, time) for pos, time in zip(betaDegR, betaTimesR)] reversePath[robotID] = armPathR # build forward path alphaTimesF = numpy.abs(alphaTimesR - alphaTimesR[-1])[::-1] alphaDegF = alphaDegR[::-1] betaTimesF = numpy.abs(betaTimesR - betaTimesR[-1])[::-1] betaDegF = betaDegR[::-1] armPathF = {} # reverse path armPathF["alpha"] = [(pos, time) for pos, time in zip(alphaDegF, alphaTimesF)] armPathF["beta"] = [(pos, time) for pos, time in zip(betaDegF, betaTimesF)] forwardPath[robotID] = armPathF return forwardPath, reversePath
def generatePath(seed=0, plot=False): nDia = 3 pitch = 22.4 hasApogee = True rg = RobotGrid(angStep, collisionBuffer, epsilon, seed) xPos, yPos = utils.hexFromDia(nDia, pitch=pitch) for ii, (xp, yp) in enumerate(zip(xPos, yPos)): rg.addRobot(ii, xp, yp, hasApogee) rg.initGrid() for ii in range(rg.nRobots): r = rg.getRobot(ii) r.setXYUniform() # set all positioners randomly (they are initialized at 0,0) rg.decollide2() rg.pathGen() if rg.didFail: print("path gen failed") raise (RuntimeError, "path gen failed") rg.smoothPaths(smoothPts) rg.simplifyPaths() rg.setCollisionBuffer(collisionBuffer - collisionShrink) rg.verifySmoothed() if rg.smoothCollisions: print("smoothing failed with ", rg.smoothCollisions) raise (RuntimeError, "smoothing failed") # find the positioner with the most interpolated steps useRobot = None maxSteps = 0 for i, r in enumerate(rg.allRobots): m = len(r.simplifiedBetaPath) # beta path is usually more complicated if m > maxSteps: maxSteps = m useRobot = r if plot: plotTraj(useRobot, "seed_%i_" % seed, dpi=250) # bp = numpy.array(useRobot.betaPath) # sbp = numpy.array(useRobot.interpSmoothBetaPath) ibp = numpy.array(useRobot.simplifiedBetaPath) # ap = numpy.array(useRobot.alphaPath) # sap = numpy.array(useRobot.interpSmoothAlphaPath) iap = numpy.array(useRobot.simplifiedAlphaPath) # generate kaiju trajectory (for robot 23) # time = angStep * stepNum / speed alphaTimesR = iap[:, 0] * angStep / RobotMaxSpeed alphaDegR = iap[:, 1] betaTimesR = ibp[:, 0] * angStep / RobotMaxSpeed betaDegR = ibp[:, 1] armPathR = {} # reverse path armPathR["alpha"] = [(pos, time) for pos, time in zip(alphaDegR, alphaTimesR)] armPathR["beta"] = [(pos, time) for pos, time in zip(betaDegR, betaTimesR)] reversePath = {robotID: armPathR} # build forward path alphaTimesF = numpy.abs(alphaTimesR - alphaTimesR[-1])[::-1] alphaDegF = alphaDegR[::-1] betaTimesF = numpy.abs(betaTimesR - betaTimesR[-1])[::-1] betaDegF = betaDegR[::-1] armPathF = {} # reverse path armPathF["alpha"] = [(pos, time) for pos, time in zip(alphaDegF, alphaTimesF)] armPathF["beta"] = [(pos, time) for pos, time in zip(betaDegF, betaTimesF)] forwardPath = {robotID: armPathF} return forwardPath, reversePath, maxSteps
epsilon = angStep * 2.2 seed1 = 0 seed2 = 1 hasApogee = True figOffset = 0 # maxPathSteps = int(700.0/angStep) posDict = OrderedDict() posDict[24] = [0, -22.4] posDict[17] = [0, 0] posDict[21] = [0, 22.4] posDict[19] = [19.39896904, -11.20000000] posDict[20] = [19.39896904, 11.20000000] posDict[16] = [-19.39896904, -11.20000000] posDict[25] = [-19.39896904, 11.20000000] rg = RobotGrid(angStep, collisionBuffer, epsilon, seed1) for posID, (xp, yp) in posDict.items(): rg.addRobot(posID, xp, yp, hasApogee) # xPos, yPos = utils.hexFromDia(nDia, pitch=pitch) # for ii, (xp,yp) in enumerate(zip(xPos,yPos)): # rg.addRobot(ii, xp, yp, hasApogee) rg.initGrid() for ii in range(rg.nRobots): r = rg.getRobot(ii) r.setXYUniform() # set all positioners randomly (they are initialized at 0,0) rg.decollide2() rg.pathGen() plotMovie(rg)
async def main(): # Set logging level to DEBUG #log.set_level(20) # Initialise the FPS instance. fps = FPS(layout="grid7.txt") await fps.initialise() hasApogee = True seed = 0 # ignored we're setting ppositions manually rg = RobotGrid(angStep, collisionBuffer, epsilon, seed) # epfl grid is sideways # xPos, yPos = utils.hexFromDia(nDia, pitch=pitch) # for ii, (xp, yp), posID in enumerate(zip(xPos, yPos, posList)): # rg.addRobot(posID, xp, yp, hasApogee) # rg.initGrid() for posID, (xp, yp) in posDict.items(): # print("loading pos %i [%.2f, %.2f]"%(posID, xp, yp)) rg.addRobot(posID, xp, yp, hasApogee) rg.initGrid() #print("fps.positioners", fps.positioners) for ii in range(rg.nRobots): r = rg.getRobot(ii) # print("loading pos %i [%.2f, %.2f]"%(r.id, r.xPos, r.yPos)) # print("robotID", r.id) await fps.positioners[r.id].update_position() alpha, beta = fps.positioners[r.id].position print("IIII", r.id, alpha, beta) r.setAlphaBeta(alpha, beta) # set all positioners randomly (they are initialized at 0,0) # rg.decollide2() rg.pathGen() if rg.didFail: print("path gen failed") raise (RuntimeError, "path gen failed") rg.smoothPaths(smoothPts) rg.simplifyPaths() rg.setCollisionBuffer(collisionBuffer - collisionShrink) rg.verifySmoothed() if rg.smoothCollisions: print("smoothing failed with ", rg.smoothCollisions) raise (RuntimeError, "smoothing failed") # find the positioner with the most interpolated steps reversePath = {} for robotID, r in zip(posDict.keys(), rg.allRobots): assert robotID == r.id # bp = numpy.array(r.betaPath) # sbp = numpy.array(r.interpSmoothBetaPath) ibp = numpy.array(r.simplifiedBetaPath) # ap = numpy.array(r.alphaPath) # sap = numpy.array(r.interpSmoothAlphaPath) iap = numpy.array(r.simplifiedAlphaPath) # generate kaiju trajectory (for robot 23) # time = angStep * stepNum / speed alphaTimesR = iap[:, 0] * angStep / RobotMaxSpeed alphaDegR = iap[:, 1] betaTimesR = ibp[:, 0] * angStep / RobotMaxSpeed betaDegR = ibp[:, 1] armPathR = {} # reverse path armPathR["alpha"] = [(pos, time) for pos, time in zip(alphaDegR, alphaTimesR)] armPathR["beta"] = [(pos, time) for pos, time in zip(betaDegR, betaTimesR)] reversePath[robotID] = armPathR await fps.send_trajectory(reversePath) await fps.shutdown()