def pc_and_tape(self): # Execute vision code cap = cv.VideoCapture(0) # Initialize detectors pc = PC() while True: ret, frame = cap.read() last = 0 c_names, c_values = pc.find_cubes_with_contours(frame) t_names, t_values = pc.find_cubes_with_contours(frame) self.send(t_names, t_values) # Keep old coordinates when cube is lost if c_values[0] == 0: self.send(c_names, [last]) else: last = c_values[0] self.send(c_names, c_values) if cv2.WaitKey(1) == 27: break cap.release() cv2.destroyAllWindows()
def main(opt, metrics_callback=None, plotting_callback=None, verbose=False): time0 = time.time() opt.model = "jci_pc" # load data train_data = DataManagerFile(opt.data_path, opt.i_dataset, opt.train_samples, opt.test_samples, train=True, normalize=opt.normalize_data, random_seed=opt.random_seed, intervention=True, regimes_to_ignore=opt.regimes_to_ignore) gt_dag = train_data.adjacency.detach().cpu().numpy() train_data_pd = pd.DataFrame(train_data.dataset.detach().cpu().numpy()) regimes_pd = pd.DataFrame(train_data.regimes) obj = PC() targets = None if opt.knowledge == "known": known = True targets = train_data.gt_interv[:,1:].T elif opt.knowledge == "unknown": known = False else: raise ValueError("The value of knowledge is incorrect.") dag = obj._run_pc(train_data_pd, regimes=regimes_pd, alpha=opt.alpha, indep_test=opt.indep_test, known=known, targets=targets) n_nodes = train_data_pd.shape[1] dag = dag[:n_nodes, :n_nodes] # Compute SHD-CPDAG and SID metrics shd = float(cdt.metrics.SHD(target=gt_dag, pred=dag, double_for_anticausal=False)) sid = float(cdt.metrics.SID(target=gt_dag, pred=dag)) sid_cpdag = cdt.metrics.SID_CPDAG(target=gt_dag, pred=dag) shd_cpdag = float(cdt.metrics.SHD_CPDAG(target=gt_dag, pred=dag)) fn, fp, rev = edge_errors(dag, gt_dag) timing = time.time() - time0 if verbose: print(f"SID: {sid}, SHD:{shd}, SHD_CPDAG:{shd_cpdag}") print(f"SID lower: {sid_cpdag[0]}, SID upper:{sid_cpdag[1]}") print(f"fn: {fn}, fp:{fp}, rev:{rev}") #save if not os.path.exists(opt.exp_path): os.makedirs(opt.exp_path) if metrics_callback is not None: metrics_callback(stage="jci_pc", step=0, metrics={"dag": dag, "sid": sid, "shd": shd, "alpha": alpha, "shd_cpdag": shd_cpdag, "fn": fn, "fp": fp, "rev": rev}, throttle=False) dump(opt, opt.exp_path, 'opt') dump(timing, opt.exp_path, 'timing', True) results = f"shd: {shd},\nsid lower: {sid_cpdag[0]},\nsid upper: {sid_cpdag[1]},\nfn: {fn},\nfp: {fp},\nrev: {rev}" dump(results, opt.exp_path, 'results', True) np.save(os.path.join(opt.exp_path, "DAG"), dag) plot_adjacency(gt_dag, dag, opt.exp_path)
def __init__(self, memoryFile): self.nCycles = 0 # Used to hold number of clock cycles spent executing instructions # Fetch self.PCMux = Mux() self.ProgramC = PC(long(0xbfc00200)) self.InstMem = InstructionMemory(memoryFile) self.IFadder = Add() self.JMux = Mux() self.IFaddconst = Constant(4) self.IF_ID_Wall = Wall() self.fetchgroup = {} # Decode self.register = RegisterFile() self.signext = SignExtender() self.control = ControlElement() self.jmpCalc = JumpCalc() self.ID_EX_Wall = Wall() # Execute self.EXadder = Add() self.shiftL = LeftShifter() self.ALogicUnit = ALU() self.ALUSrcMux = Mux() self.RegDstMux = Mux() self.ALUctrl = AluControl() self.EX_MEM_Wall= Wall() # Memory self.storage = DataMemory(memoryFile) self.brnch = Branch() self.MEM_WB_Wall= Wall() # Write Back self.WBmux = Mux() self.ProgramC self.elements1 = [self.InstMem, self.IFaddconst, self.IFadder, self.PCMux, self.JMux] self.elements2 = [self.control, self.register, self.signext, self.jmpCalc] self.elements3 = [self.shiftL, self.ALUSrcMux, self.RegDstMux, self.ALUctrl, self.ALogicUnit, self.EXadder] self.elements4 = [self.brnch, self.storage] self.elementsboot = [self.IFaddconst, self.IFadder, self.InstMem, self.IF_ID_Wall, self.register, self.signext, self.control, self.jmpCalc, self.ID_EX_Wall, self.RegDstMux, self.shiftL, self.EXadder, self.ALUSrcMux, self.ALUctrl, self.ALogicUnit, self.EX_MEM_Wall, self.brnch, self.storage, self.MEM_WB_Wall, self.WBmux, self.PCMux, self.JMux] self.walls = [self.MEM_WB_Wall, self.EX_MEM_Wall, self.ID_EX_Wall, self.IF_ID_Wall] self._connectCPUElements()
def __init__(self): pskills = [ copy.deepcopy(self.skill_database[0]), copy.deepcopy(self.skill_database[1]), copy.deepcopy(self.skill_database[2]), copy.deepcopy(self.skill_database[3]) ] pc_fighter = { 'hp': 50, 'atk': 15, 'df': 10, 'spd': 15, 'skills': pskills } self.__pc = PC('Player', fighter=pc_fighter)
def __init__(self, memoryFile): self.nCycles = 0 # Used to hold number of clock cycles spent executing instructions self.adder = Add() self.branchAdder = Add() self.alu = Alu() self.aluControl = AluControl() self.branchNE = BranchNotEqual() self.branch = Branch() self.constant = Constant(4) self.control = Control() self.dataMemory = DataMemory(memoryFile) self.instructionMemory = InstructionMemory(memoryFile) self.regMux = Mux() self.aluMux = Mux() self.dmMux = Mux() self.branchMux = Mux() self.jmpMux = Mux() self.registerFile = RegisterFile() self.shiftLeft2 = ShiftLeft2() self.signExtend = SignExtend() self.jump = Jump() self.leftShift2 = ShiftLeft2() self.leftShift2Jump = ShiftLeft2Jump() self.IFID = IFID() self.IDEX = IDEX() self.EXMEM = EXMEM() self.MEMWB = MEMWB() self.pc = PC(0xbfc00200) # hard coded "boot" address self.IFIDelements = [ self.constant, self.branchMux, self.jmpMux, self.instructionMemory, self.adder ] self.IDEXelements = [ self.control, self.registerFile, self.signExtend, self.leftShift2Jump, self.jump ] self.EXMEMelements = [ self.regMux, self.aluControl, self.aluMux, self.alu, self.shiftLeft2, self.branchAdder, self.branchNE, self.branch ] self.MEMWBelements = [ self.dataMemory ] self.WBelements = [ self.dmMux ] self.elements = [ self.IFIDelements, self.IDEXelements, self.EXMEMelements, self.WBelements] self.pipes = [ self.IFID, self.IDEX, self.EXMEM, self.MEMWB] self._connectCPUElements()
def __init__(self, memoryFile): self.nCycles = 0 # Used to hold number of clock cycles spent executing instructions self.dataMemory = DataMemory(memoryFile) self.instructionMemory = InstructionMemory(memoryFile) self.registerFile = RegisterFile() self.constant3 = Constant(3) self.constant4 = Constant(4) self.randomControl = RandomControl() self.mux = Mux() self.adder = Add() self.pc = PC(0xbfc00000) # hard coded "boot" address self.elements = [ self.constant3, self.constant4, self.randomControl, self.adder, self.mux ] self._connectCPUElements()
def __init__(self, memoryFile): self.nCycles = 0 # Used to hold number of clock cycles spent executing instructions self.datamemory = DataMemory(memoryFile) self.instructionmemory = InstructionMemory(memoryFile) self.registerfile = RegisterFile() self.constant4 = Constant(4) self.alu = Alu() self.controlunit = ControlUnit() self.shift2 = Shift2() self.shift16 = Shift16() self.signextend = SignExtend() self.alterand = Alterand() self.altershift = Altershift() self.mux_writereg = Mux() # 6 multiplexors self.mux_regoutput = Mux() self.mux_jump = Mux() self.mux_branch = Mux() self.mux_datamem = Mux() self.mux_shift16 = Mux() self.adderpc = Add() # 2 adders self.addershift = Add() self.pc = PC(0xbfc00000) # hard coded "boot" address self.elements = [ self.constant4, self.adderpc, self.instructionmemory, self.controlunit, self.altershift, self.mux_writereg, self.registerfile, self.shift16, self.signextend, self.shift2, self.addershift, self.mux_regoutput, self.alu, self.alterand, self.mux_branch, self.mux_jump, self.datamemory, self.mux_datamem, self.mux_shift16, self.registerfile ] self._connectCPUElements()
def __init__(self, memoryFile): self.nCycles = 0 # Used to hold number of clock cycles spent executing instructions self.dataMemory = DataMemory(memoryFile) self.instructionMemory = InstructionMemory(memoryFile) self.registerFile = RegisterFile() self.alu = ALU() self.mainControl = MainControl() self.splitter = Splitter() self.signExtender = SignExtender() self.andGate = AndGate() self.breaker = Breaker() self.constant4 = Constant(4) # self.randomControl = RandomControl() self.pcMux1 = Mux() self.pcMux2 = Mux() self.regMux = Mux() self.aluMux = Mux() self.resultMux = Mux() self.luiMux = Mux() self.adder = Add() self.branchAdder = Add() self.jumpAddress = JMPAddress() self.shiftBranch = LeftShiftTwo() self.shiftJump = LeftShiftTwo() self.pc = PC(hex(0xbfc00000)) # hard coded "boot" address self.elements = [self.constant4, self.adder, self.instructionMemory, self.breaker, self.splitter, self.shiftJump, self.mainControl, self.regMux, self.signExtender, self.luiMux, self.registerFile, self.jumpAddress, self.shiftBranch, self.branchAdder, self.aluMux, self.alu, self.dataMemory, self.andGate, self.pcMux1, self.pcMux2, self.resultMux, self.registerFile, self.pc] self._connectCPUElements()
def iconsistency(ConMatrix, m=-1, n=-1): stack = [] # initialize stack to simulate backtracking next(nodeCount) # check for path consistency to be used for first step if globs['pcheuristic'] == 0: # simple path consistency if not PC(ConMatrix, m, n): return None elif globs['pcheuristic'] == 2: # exact weighted path consistency if not PCew(ConMatrix, m, n): return None elif globs['pcheuristic'] == 1: # van Beek weighted path consistency if not PCw(ConMatrix, m, n): return None # as long as the consistency problem is not decided, process it while 1: # check for processing to be used if globs['process'] == 1: # dynamic processing res = dynamicUnassignedVars(ConMatrix) if not res: return ConMatrix # solution found dummy, (i, j) = res # grab unassigned variable elif globs['process'] == 0: # static processing # check for splitting to be used if globs['split'] == 0: # splitting based on set of base relations for dummy, (i, j) in stL: if bsplit[ConMatrix[i][j] - 1][0] > 1: break else: return ConMatrix # solution found elif globs['split'] == 1: # splitting based on horn set for dummy, (i, j) in stL: if hsplit[ConMatrix[i][j] - 1][0] > 1: break else: return ConMatrix # solution found # check for splitting to be used if globs['split'] == 0: # splitting based on set of base relations values = bsplit[ConMatrix[i][j] - 1][1][:] elif globs['split'] == 1: # splitting based on horn set values = hsplit[ConMatrix[i][j] - 1][1][:] # check for value decision heuristic to be used if globs['valheuristic'] == 0: # non heuristic valuesw = values valuesw.reverse() elif globs['valheuristic'] == 1: # least constraining value heuristic valuesw = [(-ew[a - 1], a) for a in values] valuesw.sort(reverse=True) valuesw = [a[1] for a in valuesw] # as long as a consistent variable-value pair in not found, search for it while 1: next(nodeCount) # increment visited nodes counter # check if current variable has any variables left, if not backtrack to a previous variable assignment if not valuesw: # check if any previous variable assignments are left in the stack while stack: ConMatrix, (i, j), valuesw, dummy = stack.pop() # check if newly grabbed variable has any variables left, if not backtrack to a previous variable assignment if valuesw: break else: return None value = valuesw.pop() # grab first value from variable c = tuple([ic[:] for ic in ConMatrix]) if valuesw else ( ) # keep copy of the constraint matrix in case an inconsistency happens # assignment takes place ConMatrix[i][j] = value ConMatrix[j][i] = inv[value - 1] # check for path consistency to be used if globs['pcheuristic'] == 0: # simple path consistency if PC(ConMatrix, i, j): break elif globs['pcheuristic'] == 2: # exact weighted path consistency if PCew(ConMatrix, i, j): break elif globs[ 'pcheuristic'] == 1: # van Beek weighted path consistency if PCw(ConMatrix, i, j): break ConMatrix = c # revert contraint mantrix to previous state stack.append((c, (i, j), valuesw[:], dummy)) # save current state (function call) in a stack raise RuntimeError, "Can't happen"
# Copyright (c) 2012 Bailey Mihajlich # Licensed under the GNU GPL v.2 # See license.txt for licence information from constants import * from images import * from pc import PC # global pygame thingies window = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT)) newgame = True clock = pygame.time.Clock() # global images images = loadAllImages() # global PC hero = PC("Cole", CENTERCENTER) # global sprite groups heroGroup = pygame.sprite.Group(hero) solidGroup = pygame.sprite.Group(hero) attackGroup = pygame.sprite.Group() itemGroup = pygame.sprite.Group() solidQ = pygame.sprite.Group() backgroundQ = pygame.sprite.Group() attackQ = pygame.sprite.Group() itemQ = pygame.sprite.Group()
import jsonloader as loader from pc import PC import parser from initiative import Initiative requested_pc = input( "Which player character do you want to use? ").strip().lower() pc = PC(loader.get_pc(requested_pc)) print("Got statistics for " + pc.name) print() print(pc) initiative = Initiative() initiative.add_entity(pc, 1) while (True): parser.get_input(initiative) print()