def _calculate_score(self): for result in self.results: adverb_values = [float(x['value']) for x in result['terms'] if x['type'] == 'adverb'] substantive_values = [float(x['value']) for x in result['terms'] if x['type'] == 'substantive'] adjective_values = [float(x['value']) for x in result['terms'] if x['type'] == 'adjective'] score = util.mult(adverb_values) * util.mult(substantive_values) * util.mult(adjective_values) result['score'] = score
def edgeFrame(self,edge): p=util.mult(self.perpendicularTo(edge),self.frameWidth) v0=edge.Vertexes[0].Point v1=edge.Vertexes[1].Point pts = [v0+p,v1+p,v1-p,v0-p] face = util.faceFromVectors(pts) return self.extrudeInCenter(face,self.frameThickness) \
def part2(): field_rules, your_ticket, nearby_tickets = parse_input(read_input()) valid_tickets = list( filter(lambda t: ticket_is_valid(t, field_rules), nearby_tickets)) fields = { field_name: Field(field_name) for field_name in field_rules.keys() } positions = [Position(i) for i in range(len(fields))] for position in positions: possible_fieldnames_str = set.intersection( *(find_possible_fields(t[position.index], field_rules) for t in valid_tickets)) position.possible_fields = [ fields[field_name] for field_name in possible_fieldnames_str ] for field_name in fields.values(): field_name.possible_positions = list( filter(lambda p: field_name in p.possible_fields, positions)) location_per_fieldname = find_field_positions(positions, list(fields.values())) return mult(your_ticket[location_per_fieldname[f]] for f in location_per_fieldname.keys() if f.startswith('departure'))
def legHolder(self,toCut=False): points = [] c1 = Base.Vector(0,0,0) c2 = c1 + Base.Vector(self.legHolderLength,0,0) c3 = c2 + Base.Vector(0,self.horseWidth,0) c4 = c1 + Base.Vector(0,self.horseWidth,0) attachMargin = (self.legHolderLength- self.attachWidth)/2 t11 = c1 + Base.Vector(attachMargin,0,0) t12 = t11+ Base.Vector(self.attachWidth) t21 = c4 + Base.Vector(attachMargin,0,0) t22 = t21+ Base.Vector(self.attachWidth) #block between legs b0 = util.mult((c3 - c2),0.5,c2) b11 = b0 + Base.Vector(0,-self.legSafeWidth) b21 = b0 + Base.Vector(0,self.legSafeWidth) b12 = b11 + Base.Vector(self.legSafeHeight/2,self.legSafeCurve1) b22 = b21 + Base.Vector(self.legSafeHeight/2,-self.legSafeCurve1) b13 = b11 + Base.Vector(self.legSafeHeight) b23 = b21 + Base.Vector(self.legSafeHeight) bt = b0 + Base.Vector(self.legSafeCurve2 + self.legSafeHeight) spPoints = [b11,b12,b13,bt,b23,b22,b21] points.append(b21) points.append(c3) points.append(c4) points.append(c1) points.append(c2) points.append(b11) p = util.partFromVectors(points,Base.Vector(0,0,self.thickness)) lines = util.linesFromPoints(points) sp = Part.BSplineCurve() sp.interpolate(spPoints,False) lines.append(sp.toShape()) w = Part.Wire(lines) f = Part.Face(w) p = f.extrude(Base.Vector(0,0,self.thickness)) a1 = self.attachPart(t11,t12,Base.Vector(0,-1,0),toCut=toCut) a2 = self.attachPart(t21,t22,Base.Vector(0,1,0),toCut=toCut) part = p.fuse(a1).fuse(a2) part.translate(Base.Vector(0,self.sideThickness,0)) return part
def generate_deformations(img, theta_step=1, deformations=30): H, W = np.shape(img)[:2] center = np.float32(H / 2.0), np.float32(W / 2.0) rotation_matrices = [ cv2.getRotationMatrix2D((center[1], center[0]), theta, 1.0) for theta in range(0, 361) ] for theta in range(0, 360, theta_step): Rt = rotation_matrices[theta] N = deformations r_phi = np.random.randint(0, 360, N) r_lambda1 = np.random.uniform(0.6, 1.5, N) r_lambda2 = np.random.uniform(0.6, 1.5, N) r_noise_ratio = np.random.uniform(0, 0.1, N) for noise_ratio, lambda1, lambda2, phi in zip(r_noise_ratio, r_lambda1, r_lambda2, r_phi): Rp = rotation_matrices[phi] Rp1 = rotation_matrices[360 - phi] Rl = np.matrix([[lambda1, 0, 0], [0, lambda2, 0]]) Rz = mult(Rp, mult(Rl, Rp1)) R = mult(Rt, Rz) R_inv = cv2.invertAffineTransform(R) warped = cv2.warpAffine(img, R, dsize=(H, W), borderMode=cv2.BORDER_REFLECT101) # add gaussian noise noise = np.uint8(np.random.normal(0, 25, (W, H))) blurred = warped # cv2.GaussianBlur(warped, (7, 7), 25) noised = cv2.addWeighted(blurred, 1 - noise_ratio, noise, noise_ratio, 0) yield R_inv, noised
def _calculate_score(self): for result in self.results: adverb_values = [ float(x['value']) for x in result['terms'] if x['type'] == 'adverb' ] substantive_values = [ float(x['value']) for x in result['terms'] if x['type'] == 'substantive' ] adjective_values = [ float(x['value']) for x in result['terms'] if x['type'] == 'adjective' ] score = util.mult(adverb_values) * util.mult( substantive_values) * util.mult(adjective_values) result['score'] = score
def setup(data, test_data=None, processes=1, maxtasks=10000): Evaluator.close() max_train_size = data.size() max_test_size = test_data.size() if test_data is not None else (0, 0) max_size = max_train_size if util.mult(max_train_size) > util.mult( max_test_size) else max_test_size Evaluator._inputs = mp.sharedctypes.RawArray(ctypes.c_double, util.mult(max_size)) Evaluator._targets = mp.sharedctypes.RawArray(ctypes.c_double, max_size[0]) Evaluator._test_inputs = mp.sharedctypes.RawArray(ctypes.c_double, util.mult(max_test_size)) \ if test_data is not None else None Evaluator._test_targets = mp.sharedctypes.RawArray(ctypes.c_double, max_test_size[0]) \ if test_data is not None else None Evaluator.multiprocessing = processes > 1 Evaluator._pool = concurrent.futures.ProcessPoolExecutor(max_workers=processes) \ if Evaluator.multiprocessing else None Evaluator._maxtasks = maxtasks Evaluator._tasks = 0 Evaluator._processes = processes
def attachPart(self,p1,p2,direction,size=attachSize,holeWidth=thickness, toCut=False): p12 = util.mult(direction,size-self.attachCurveDepth,p1) p22 = util.mult(direction,size-self.attachCurveDepth,p2) c = util.mult((p2 - p1),0.5,p1) + util.mult(direction,size) a = Part.Arc(p12,c,p22) lines = util.linesFromPoints([p22,p2, p1,p12]) lines += [a.toShape()] w = Part.Wire(lines) p = Part.Face(w).extrude(Base.Vector(0,0,self.thickness)) distHoleStart = util.mult(direction,self.attachHoleMarginStart) distHoleEnd = util.mult(direction,size-self.attachHoleMarginEnd) pi1 = util.mult((p2 - p1),1.0/3,p1) + distHoleStart pi2 = util.mult((p2 - p1),2.0/3,p1) + distHoleStart pi3 = util.mult((p2 - p1),1.0/3,p1) + distHoleEnd pi4 = util.mult((p2 - p1),2.0/3,p1) + distHoleEnd hole = util.partFromVectors([pi1,pi3,pi4,pi2], Base.Vector(0,0,self.thickness)) if not toCut: attach = p.cut(hole) else: attach = p return attach
def _set_data(data, test_data): size = data.size() flat_len = util.mult(size) assert flat_len <= len(Evaluator._inputs) and size[0] <= len( Evaluator._targets) try: Evaluator._inputs[:flat_len] = data.inputs.ravel()[:flat_len] Evaluator._targets[:size[0]] = data.targets[:size[0]] except TypeError: raise TypeError('Data must be numeric') test_size = test_data.size() if test_data is not None else None if test_size is not None: test_flat_len = util.mult(test_size) assert test_flat_len <= len( Evaluator._test_inputs) and test_size[0] <= len( Evaluator._test_targets) Evaluator._test_inputs[:test_flat_len] = test_data.inputs.ravel( )[:test_flat_len] Evaluator._test_targets[:test_size[ 0]] = test_data.targets[:test_size[0]] return size, test_size
def find_sum_combo_with_z3(numbers, n, target=2020): solver = z3.Solver() symvars = [z3.Int(f'sv_{i}') for i in range(n)] solver.add(z3.Sum(symvars) == target) solver.add(z3.Distinct(symvars)) for sv in symvars: solver.add(z3.Or([sv == number for number in numbers])) if str(solver.check()) == 'sat': print(f'Model = {solver.model()}') return mult(solver.model()[sv].as_long() for sv in symvars) else: print('No solution') return -1
def back(self,toCut=False): points = [] c1 = Base.Vector(0,0,0) c2 = c1 + Base.Vector(self.backLength,0,0) c3 = c2 + Base.Vector(0,self.horseWidth,0) c4 = c1 + Base.Vector(0,self.horseWidth,0) #arc in the top av1 = util.mult((c3 - c2),0.5,c2) + Base.Vector(self.backCurveDepth) c3c = c3 + Base.Vector(0,self.sideThickness,0) c2c = c2 + Base.Vector(0,-self.sideThickness,0) points.append(c3c) points.append(c3 + Base.Vector(-self.tenonLength,self.sideThickness,0)) points.append(c3 + Base.Vector(-self.tenonLength,0,0)) points.append(c4) points.append(c1) points.append(c2 + Base.Vector(-self.tenonLength,0,0)) points.append(c2 + Base.Vector(-self.tenonLength,-self.sideThickness,0)) points.append(c2c) lines = util.linesFromPoints(points) lines.append(Part.Arc(c2c,av1,c3c).toShape()) w = Part.Wire(lines) f = Part.Face(w) p = f.extrude(Base.Vector(0,0,self.thickness)) offset = Base.Vector((self.backLength - self.tenonLength - self.attachWidth)/2) att1v1 = offset + c1 att1v2 = offset + c1+Base.Vector(self.attachWidth) att2v1 = offset + c4 att2v2 = offset + c4+Base.Vector(self.attachWidth) a1 = self.attachPart(att1v1,att1v2,Base.Vector(0,-1,0),toCut=toCut) a2 = self.attachPart(att2v1,att2v2,Base.Vector(0,1,0),toCut=toCut) part = p.fuse(a1) part = part.fuse(a2) part.translate(Base.Vector(0,self.sideThickness,0)) return part
def enumPossibleMoves(self): self.moveLookUp={} self.currentIndex=self.getCurrentIndex() #hack, to be removed self.previousZ=self.z[self.currentIndex] pz = self.crpSampler(self.alpha,self.m,self.currentIndex) zi = copy.deepcopy(self.z) lambd=[] for j in range(len(pz)-1): zi[self.currentIndex]=j if self.kplus==self.numberVariables: numberClasses=len(pz) else: numberClasses=len(pz)-1 nplus,nminus = computeEdge(self.childDensity.dag,zi,numberClasses) pGz = priorG(self.beta,nplus,nminus) cePrior = self.classEdgePrior(self.beta,self.kplus,self.classOrder) ep = bernoulli(self.childDensity.dag,cePrior,zi) lambd.append(pGz*ep) if self.kplus<self.numberVariables: for k in range(len(self.classOrder)+1): newClassOrder=copy.deepcopy(self.classOrder) zi[self.currentIndex]=self.kplus newClassOrder.insert(k,self.kplus) nplus,nminus = computeEdge(self.childDensity.dag,zi,len(pz)) pGz2 = priorG(self.beta,nplus,nminus) ceprior2 = self.classEdgePrior(self.beta,self.kplus+1,newClassOrder) ep2 = bernoulli(self.childDensity.dag,ceprior2,zi) lambd.append(pGz2*ep2) if k>0: pz.append(pz[j+1]) probVector =mult(pz, lambd) out=[] for i in range(len(probVector)): out.append(i) self.moveLookUp[i]=probVector[i] return out
def part2(): return mult( get_n_combinations(partition) for partition in partition_input(read_all_voltages_sorted()))
def find_sum_combo(numbers, n, target=2020): for perm in itertools.combinations(numbers, n): if sum(perm) == target: return mult(perm)
def normalO(self,size=1): params=self.face.Surface.parameter(self.refPoint()) normal=self.face.normalAt(params[0],params[1]) return util.mult(normal,size)
def part2(): return mult( find_n_tree_in_path(read_input_as_lines(), x_d, y_d) for x_d, y_d in ((1, 1), (3, 1), (5, 1), (7, 1), (1, 2)))
def part1(): tile_data = parse_input_as_tiles(read_input()) match_borders(tile_data) return mult(t.tile_id for t in find_corner_tiles(tile_data))
def tenonPoints(self,p1,p2,direction,depth=sideThickness): translationVector = util.mult(direction,depth) p11 = p1 + translationVector p21 = p2 + translationVector return [p1,p11,p21,p2]