def tcp_test(num): count_failed = 0 count_success = 0 steps = [] counts = [] for i in range(num): print(i) controller = cppCellModel.controller_constructor(50, 50, 100, 350) counts.append(cppCellModel.HCellCount()) for i in range(35): #print("Before", cppCellModel.HCellCount(), cppCellModel.CCellCount()) cppCellModel.irradiate(controller, 2) ##print("After", cppCellModel.HCellCount(), cppCellModel.CCellCount()) cppCellModel.go(controller, 24) if cppCellModel.CCellCount() == 0: steps.append(i + 1) break count = cppCellModel.CCellCount() if count > 10: count_failed += 1 elif count == 0: count_success += 1 counts[-1] /= cppCellModel.HCellCount() cppCellModel.delete_controller(controller) print("Percentage of full recovs :", (100 * count_success) / num) print("Percentage of almost recovs :", (100 * (num - count_failed)) / num) print("Average dose in successes :", 2 * sum(steps) / len(steps)) print(sum(counts) / len(counts))
def adjust_reward(self, dose, ccell_killed, hcells_lost): if self.special_reward and self.inTerminalState(): if self.end_type == "L" or self.end_type == "T": return -1 else: if self.reward == 'dose': return -dose / 200 + 0.5 - ( self.init_hcell_count - cppCellModel.HCellCount()) / 3000 else: return 0.5 - ( self.init_hcell_count - cppCellModel.HCellCount() ) / 3000 #(cppCellModel.HCellCount() / self.init_hcell_count) - 0.5 - (2 * hcells_lost/2500) else: if self.reward == 'dose' or self.reward == 'oar': return -dose / 200 + (ccell_killed - 5 * hcells_lost) / 100000 elif self.reward == 'killed': return (ccell_killed - 5 * hcells_lost) / 100000
def act(self, action): dose = 1 + action / 2 if self.action_type == 'DQN' else action[ 0] * 4 + 1 rest = 24 if self.action_type == 'DQN' else int( round(action[1] * 60 + 12)) if self.dose_map is not None: tumor_radius = cppCellModel.tumor_radius(self.controller_capsule) pre_hcell = cppCellModel.HCellCount() pre_ccell = cppCellModel.CCellCount() self.total_dose += dose self.num_doses += 1 if dose > 0 else 0 cppCellModel.irradiate(self.controller_capsule, dose) self.radiation_h_killed += (pre_hcell - cppCellModel.HCellCount()) if self.dataset is not None: self.dataset[0].append( cppCellModel.controllerTick(self.controller_capsule) - 350) self.dataset[1].append((pre_ccell, cppCellModel.CCellCount())) self.dataset[2].append(dose) if self.dose_map is not None: self.add_radiation( dose, tumor_radius, cppCellModel.get_center_x(self.controller_capsule), cppCellModel.get_center_y(self.controller_capsule)) self.dose_maps.append( (cppCellModel.controllerTick(self.controller_capsule) - 350, np.copy(self.dose_map))) self.tumor_images.append( (cppCellModel.controllerTick(self.controller_capsule) - 350, cppCellModel.observeDensity(self.controller_capsule))) p_hcell = cppCellModel.HCellCount() p_ccell = cppCellModel.CCellCount() cppCellModel.go(self.controller_capsule, rest) post_hcell = cppCellModel.HCellCount() post_ccell = cppCellModel.CCellCount() reward = self.adjust_reward(dose, pre_ccell - post_ccell, pre_hcell - min(post_hcell, p_hcell)) if self.verbose: print("Radiation dose :", dose, "Gy ", "remaining :", post_ccell, "time =", rest, "reward=", reward) return reward
def inTerminalState(self): if cppCellModel.CCellCount() <= 0: if self.verbose: print("No more cancer") self.end_type = 'W' return True elif cppCellModel.HCellCount() < 10: if self.verbose: print("Cancer wins") self.end_type = "L" return True elif cppCellModel.controllerTick(self.controller_capsule) > 1550: if self.verbose: print("Time out!") self.end_type = "T" return True else: return False
def reset(self, mode): cppCellModel.delete_controller(self.controller_capsule) self.controller_capsule = cppCellModel.controller_constructor( 50, 50, 100, 350) self.init_hcell_count = cppCellModel.HCellCount() if mode == -1: self.verbose = False else: self.verbose = True self.total_dose = 0 self.num_doses = 0 self.radiation_h_killed = 0 if self.dose_map is not None: self.dose_maps.append( (cppCellModel.controllerTick(self.controller_capsule) - 350, np.copy(self.dose_map))) self.tumor_images.append( (cppCellModel.controllerTick(self.controller_capsule) - 350, cppCellModel.observeDensity(self.controller_capsule))) return self.observe()
def observe(self): if self.obs_type == 'scalars': return [ cppCellModel.controllerTick(self.controller_capsule) / 2000, cppCellModel.HCellCount() / 100000, cppCellModel.CCellCount() / 50000 ] else: if self.obs_type == 'densities': cells = (np.array(cppCellModel.observeDensity( self.controller_capsule), dtype=np.float32)) / 100.0 else: cells = (np.array( cppCellModel.observeSegmentation(self.controller_capsule), dtype=np.float32) + 1.0) / 2.0 # Obs from 0 to 1 if self.resize: cells = cv2.resize(cells, dsize=(25, 25), interpolation=cv2.INTER_CUBIC) return [cells]
def __init__(self, obs_type, resize, reward, action_type, special_reward): """Constructor of the environment Parameters: obs_type : Type of observations provided to the agent ('head' for segmentation or 'types' for weighted sums) resize : True if the observations should be resized to 25 * 25 arrays reward : Type of reward function used ('dose' to minimize the total dose, 'killed' to maximize damage to cancer cells while miniizing damage to healthy tissue and 'oar' to minimize damage to the Organ At Risk action_type : 'DQN' means that we have a discrete action domain and 'DDPG' means that it is continuous special_reward : True if the agent should receive a special reward at the end of the episode. """ self.controller_capsule = cppCellModel.controller_constructor( 50, 50, 100, 350) self.init_hcell_count = cppCellModel.HCellCount() self.obs_type = obs_type self.resize = resize self.reward = reward self.action_type = action_type self.special_reward = special_reward self.dose_map = None self.dataset = None
def surviving_fraction(self): return cppCellModel.HCellCount() / self.init_hcell_count