def run(self, csccp, run_options): if run_options["verbose"]: print "Starting Dynamic Programming" if csccp.trivial(): raise dat.TrivialCSCCP("This CSCCP problem is trivial.") # 1- Variable init * * * * * * * * * * * * * * * * * * * * * * * * l = max([len(x) for x in csccp.W]) n = csccp.n R = csccp.R D = run_options["dec"] wt_max = int(csccp.wmax * 10**D + n * 0.5 / 10**D) wt_min = int(csccp.wmin * 10**D - n * 0.5 / 10**D) W = array([([int(w * 10**D) for w in wside] + [0] * l)[:l] for wside in csccp.W], dtype=int32) P = array([(pside + [0] * l)[:l] for pside in csccp.P], dtype=float32) K = array(csccp.K, dtype=int32) C = zeros((wt_max + 1, R), self.float_t) # [0 .. n-1] x [0 .. wt_max] x [0 .. R-1] L = zeros((n, wt_max + 1, R, 2), self.int_t) try: # 2- Core computations * * * * * * * * * * * * * * * * * * * * SIGINT, TIMEOUT = self.kernelOpt.run(n, R, wt_max, K, W, P, L, C, run_options) if SIGINT: raise KeyboardInterrupt if TIMEOUT > 0: raise tools.Timer.TimeoutException(TIMEOUT) # 3- Result filtering starting * * * * * * * * * * * * * * * * * valid_candidates = [] len_filtered_results = 0 for w in range(wt_min, wt_max + 1): for r in range(0, R): probability = C[w][r] if probability > 0: len_filtered_results += 1 real_weight, comp = self.compute_real_weight( csccp, w, r, W, L) if real_weight >= csccp.wmin and real_weight <= csccp.wmax: valid_candidates.append( (probability * csccp.scaffold_probability_rel2config, real_weight + csccp.scaffold_weight_rel2config, comp)) valid_candidates = sorted(valid_candidates, key=lambda x: x[0], reverse=True) valid_candidates = valid_candidates[0:R] return dat.CSCCP.Result( array(valid_candidates, dtype=dtype([('p', float), ('w', float), ('comp', list)]))) except KeyboardInterrupt: print "* WARNING: the program was interrupted by user (CTRL+C). The program will now terminate *" exit()
def run(self, csccp, run_options): if csccp.trivial(): raise dat.TrivialCSCCP("This CSCCP problem is trivial") if run_options['verbose']: print "Starting Iterative Dynamic Programming C-optimized L-compressed" # 1- Variable init * * * * * * * * * * * * * * * * * * * * * * * * * * * l = max([len(x) for x in csccp.W]) n = csccp.n multiplier = 10 R = csccp.R RR = multiplier * R wt_max = int(csccp.wmax + 0.5 * n) wt_min = int(csccp.wmin - 0.5 * n) K = array(csccp.K, dtype=int32) W = array([([int(w) for w in wside] + [0] * l)[:l] for wside in csccp.W], dtype=int32) P = array([(pside + [0] * l)[:l] for pside in csccp.P], dtype=float32) try: # 2- Core computations loop * * * * * * * * * * * * * * * * * * * * * count = 0 reason = "none" valid_candidates = [] K_encoder = dp_solvers.Encoder1() R_encoder = dp_solvers.Encoder1() K_encoder.setup(K, 32) while True: count += 1 # 2.1- Variable init for the loop C = zeros((wt_max + 1, RR), float32) # [0 .. n-1] x [0 .. wt_max] x [0 .. R-1] L = zeros((wt_max + 1, RR, 2), uint32) R_encoder.setup([RR - 1 for x in range(0, n)], 32) # 2.2- Core computations SIGINT, TIMEOUT = self.kernelOptCompressed.run( n, RR, wt_max, K, W, P, L, C, K_encoder, R_encoder, run_options) if SIGINT: raise KeyboardInterrupt if TIMEOUT > 0: raise tools.Timer.TimeoutException(TIMEOUT) # 2.3- Result filtering len_filtered_results = 0 for w in range(wt_min, wt_max + 1): for r in range(0, RR): probability = C[w][r] if probability: len_filtered_results += 1 real_weight, comp = self.compute_real_weight( csccp, w, r, W, L, K_encoder, R_encoder) if real_weight >= csccp.wmin and real_weight <= csccp.wmax: valid_candidates.append( (probability * csccp.scaffold_probability_rel2config, real_weight + csccp.scaffold_weight_rel2config, comp)) # 2.4- Taking decision to iterate or not if len_filtered_results and len_filtered_results < RR: reason = "len(filtered_results)=%d < RR" % ( len_filtered_results) break if len(valid_candidates) >= R: reason = "found R valid candidates" break if RR >= csccp.number_of_compounds: break RR = min(RR * multiplier, csccp.number_of_compounds) # -- END WHILE if run_options["verbose"]: print "Finished with %d iterations: RR=%d<=%d. Reason: %s" % ( count, RR, csccp.number_of_compounds, reason) valid_candidates = sorted(valid_candidates, key=lambda x: x[0], reverse=True) valid_candidates = valid_candidates[0:R] return dat.CSCCP.Result( array(valid_candidates, dtype=dtype([('p', float), ('w', float), ('comp', list)]))) except KeyboardInterrupt: print "* WARNING: the program was interrupted by user (CTRL+C). The program will now terminate *" exit()
def run(self, csccp, options={}): ''' Args: CSCCP csccp : the input problem options : {verbose,debug,maxruntime} Returns: CSCCP.Result: the results Raises: TimeoutException ''' if csccp.trivial(): raise dat.TrivialCSCCP("This CSCCP problem is trivial") if options["verbose"]: print "Starting python CPU Brute force solver..." res = [] minimum = 1 minIdx = 0 count = 0 timer = Timer() timer.start() # 2- Solve CSCCP and handle interruption and timeout exceptions try: for comp in csccp: #stop the program if it takes too long to run if timer.elapsedTime() > options["maxruntime"]: raise Timer.TimeoutException(timer.elapsedTime()) weights = [0] + [ csccp.W[s][comp[s]] for s in range(0, csccp.n) ] weight = sum(weights) if options["debug"]: print "Debug:", weight + csccp.scaffold_weight_rel2config, "(weight computed)" # we only keep coumpounds that have the right weight if weight >= csccp.wmin and weight <= csccp.wmax: # then we compute the probability probabilities = [1] + [ csccp.P[s][comp[s]] for s in range(0, csccp.n) ] probabilities = array(probabilities, dtype=self.float_t) probability = multiply.reduce(probabilities) #probability=reduce(mul,probabilities) if not len(res): minimum = probability minIdx = 0 res.append([probability, weight, comp]) elif len(res) < csccp.R: if probability < minimum: minIdx = len(res) minimum = probability #if probability>=minimum: we just add it to the list without changing res.append([probability, weight, comp]) else: #if probability<minimum: we do nothing if probability >= minimum: res[minIdx] = [probability, weight, comp] minIdx = argmin([x[0] for x in res]) minimum = res[minIdx][0] except KeyboardInterrupt: print "* WARNING: the program was interrupted by user (CTRL+C). Results might be inaccurate! *" # 3- Processing results res = sorted(res, key=lambda x: x[0], reverse=True) res = array([(p * csccp.scaffold_probability_rel2config, w + csccp.scaffold_weight_rel2config, comp) for p, w, comp in res], dtype=dtype([('p', float), ('w', float), ('comp', list)])) if options["verbose"]: print "Done." return dat.CSCCP.Result(res)