def t(num): model = restestmodel.ResNet(restestmodel.Bottleneck, [3, 4, 23, 3]) # model = CnnModel.CNN() # model.load_state_dict(torch.load('CnnParameters.pt')) model.load_state_dict(torch.load('Res.pt')) model.eval() path = 'G:/TianYuze/DataSets' # path = 'C:/Users/Antlab/Desktop/ppp' a = Auxiliary.mtest(path, num) a = torch.from_numpy(a) a = a.view(1, 1, 16, 16) a = a.float() b = model(Variable(a)) b = b.data b = b.view(49, 1) b = b.numpy() b = 20 * np.log10(b) # print(b) fig = plt.figure(dpi=64, figsize=(16, 9)) plt.xlabel('GHz', fontsize=32) plt.ylabel("dB", fontsize=32) plt.tick_params(axis='both', which='major', labelsize=18) plt.ylim(-26, 1) plt.xlim(8, 13) x = np.linspace(8, 13, 49) plt.plot(x, b, color='red', linewidth=3) c = Auxiliary.shows11(path, num) plt.show()
def similar_enough(self, album, result): artists = tag.parse_tag("artist", album.loaded_tracks) titles = tag.parse_tag("album", album.loaded_tracks) tracks = tag.parse_tag("title", album.loaded_tracks) years = tag.parse_tag("date", album.loaded_tracks) discogs_album = self.d.release(result.id) artists_match = any([ aux.edit_comp(artist1.name, artist2) for artist1 in discogs_album.artists for artist2 in artists ]) titles_match = any( [aux.edit_comp(discogs_album.title, title) for title in titles]) if artists_match and titles_match: if len(tracks) == len(discogs_album.tracklist): album.imposed_track_order = [i for i in range(len(tracks))] return True else: tracks_match = True album.imposed_track_order = [] for i in range(len(tracks)): index = i tracks_match = aux.edit_comp( tracks[i], discogs_album.tracklist[i].title) if not tracks_match: for j in range(len(discogs_album.tracklist)): tracks_match = aux.edit_comp( tracks[i], discogs_album.tracklist[j].title) if tracks_match: index = j break if not tracks_match: return False album.imposed_track_order.append(index) return True return False
def steepest_ascent_hill_climbing_n_times(nurses_number, n_times): """ Solve the Steepest-Ascent Hill Climbing problem to n random states :parameter nurses_number: Number of nurses that will be used to solve the problem :type nurses_number: Integer with the number of nurses :parameter n_times: Number of random states that will be evaluated :type n_times: Int :return current_state: The last state that was analyzed :rtype current_state: String :return current_violations: The number of violations of the last state evaluated :rtype current_violations Int Important variables ------------------- best_state_generated : Will save the best state generated best_violation_found : Will save the best violation found random_states_generated : Will save a list with the random states generated to don't be used a state equals to a other state used state : Save a random state generated current_state : Save the current state current_violations : Save the current violations """ print('Executing...') best_state_generated = '' best_violations_found = 0 random_states_generated = list() off_list = True # Will execute a loop for the number of random states passed for i in range(0, n_times): # Will generate the random states while off_list: state = Au.random_generator(nurses_number) if state not in random_states_generated: random_states_generated.append(state) off_list = False # Will save the values returned of steepest_ascent_hill_climbing_solution current_state, current_violations = steepest_ascent_hill_climbing_solution(state, nurses_number) if best_state_generated == '' or best_violations_found > current_violations: best_state_generated = current_state best_violations_found = current_violations off_list = True # Here, we are show the best state found print('\n===========================================') Au.print_output('Best state found to '+str(n_times)+' random states run', best_state_generated, best_violations_found, nurses_number) print('===========================================\n')
def __init__(self, path): self.path = path self.tracks = aux.list_tracks(os.listdir(self.path)) self.loaded_tracks = tag.load_tracks(self.path, self.tracks) self.tags = tag.get_tags(self.loaded_tracks) self.images = aux.list_images(os.listdir(self.path)) self.ext = aux.list_ext(self.tracks) self.imposed_track_order = [] self.encoded = tag.parse_tag("encodedby", self.loaded_tracks) == 'MetaCleanup' self.counts = Counts()
def detect_best_window(self, win_size=8., win_shift=0.2, plot_debug=False, ax=False, savefig=False): """ This function detects the best window of the file to be analyzed. The core mechanism is in the best_window_algorithm function. For plot debug, call this function in "main" with argument plot_debug=True :param win_size: float. Size in seconds of the best window in seconds. :param win_shift: float. Size in seconds between windows. Default is 0.2 seconds. :param plot_debug: boolean. use True to plot filter parameters (and thresholds) for detecting best window :param ax: axes of the debugging plots. :return: two floats. The first float marks the start of the best window and the second the defined window-size. """ filename = self._wavfile p_idx, t_idx = self.detect_peak_and_trough_indices() peak_time, peak_ampl, trough_time, trough_ampl = self._time[p_idx], self._eod[p_idx],\ self._time[t_idx], self._eod[t_idx] # peaks and troughs here refer to those found in each eod-cycle. For each cycle there should be one peak and # one trough if the detect_peak_indices function worked fine. my_times = peak_time[peak_time <= peak_time[-1] - win_size] # Upper window-boundaries solution my_times = np.arange(0.0, peak_time[-1] - win_size, win_shift) cvs = np.empty(len(my_times)) no_of_peaks = np.empty(len(my_times)) mean_ampl = np.empty(len(my_times)) for i, curr_t in enumerate(my_times): # This for-loop goes through each eod-cycle. Isn't this too much? It considerably makes the code slow. window_idx = (peak_time >= curr_t) & (peak_time <= curr_t + win_size) # the last line makes a window from curr_t and adds 8. seconds to it. Lower window-boundaries solution. p2t_ampl = peak_ampl[window_idx] - trough_ampl[window_idx] cvs[i] = np.std(p2t_ampl, ddof=1) / np.mean(p2t_ampl) mean_ampl[i] = np.mean(p2t_ampl) no_of_peaks[i] = len(p2t_ampl) if plot_debug: ax = aux.draw_bwin_analysis_plot(filename, self._time, self._eod, no_of_peaks, cvs, mean_ampl) # ToDo: Need to find a way to plot ax5!! It's not as easy as it seems... # ToDo: WOULD BE GREAT TO DO ALL THE PLOTTING IN EXTRA FUNCTION IN AUXILIARY!!! bwin_bool_array = self.best_window_algorithm(no_of_peaks, mean_ampl, cvs, plot_debug=plot_debug, axs=ax, win_shift=win_shift) bwin_bool_inx = np.where(bwin_bool_array)[0][0] # Gets the index of the best window out of all windows. entire_time_idx = self._eod_peak_idx[bwin_bool_inx] bwin = my_times[bwin_bool_inx] self.roi = (entire_time_idx, entire_time_idx + int(self._sample_rate/2.)) # plotting the best window in ax[5] if plot_debug and len(ax) > 0: aux.draw_bwin_in_plot(ax, filename, self._time, self._eod, bwin, win_size, p_idx, t_idx, savefig=savefig) return bwin, win_size
def run(self): for album_dir in self.albums: album = Album(os.path.join(self.music_folder, album_dir)) if album.encoded: continue discogs_utils = dis.DiscogsUtils(self.discogs_object) print('Searching Discogs for {0}'.format(album_dir)) release_no = discogs_utils.get(album) if not release_no: print('Could not find {0} on Discogs.'.format(album_dir)) continue release = discogs_utils.d.release(release_no) release_name = ', '.join( [artist.name for artist in release.artists]) + ' - ' + release.title release_name = aux.format_path_name(release_name) self.dir_replace(album, release_name) print('Re-tagging {0}'.format(release_name)) tag_dict = discogs_utils.TAG_DICT_STATIC(release_no) for i in range(len(album.tracks)): dynamic = discogs_utils.TAG_DICT_DYNAMIC( release_no, album.imposed_track_order[i]) tag_dict.update(dynamic) track_name = tag_dict["tracknumber"] + ' - ' + tag_dict[ "title"] + album.ext[i] track_name = aux.format_path_name(track_name) for tag in tag_dict: album.loaded_tracks[i][tag] = tag_dict[tag] self.file_replace(album, track_name, i) album.loaded_tracks[i].save( os.path.join(album.path, track_name)) album.counts.track_count += 1 print('Tagging of track {0} successful!'.format(str(i + 1))) time.sleep(2.0) album.counts.album_count += 1 print('Adding art\n') self.add_art(album, release) self.update_counts(self.counts, album.counts) time.sleep(5.0) print('Cleanup of your {0} folder complete!\n'.format( os.path.basename(self.music_folder))) print('Tagging fixed for {0} track(s) and {1} album(s).'.format( str(self.counts.track_count), str(self.counts.album_count))) print('Directory names for {0} album(s) changed.'.format( str(self.counts.directory_count))) print('Cover art added to {0} album(s).'.format( str(self.counts.art_count))) return
def _parse_rows(self, rows): """ Return all columns of table row in proper format to be used later. Data from playoffs included if flag is set. """ result = [] for row in rows: tds = row.findAll('td') if len(tds) < 21: continue if tds[0].contents == [] and not self.playoffs: break aux.columns_values(tds) result.append(tds) return result
def blitRecorder(Screen, Recorder) : turnStr = str(Recorder.getTurn()) scoreStr = Auxiliary.insertComma(Recorder.getScore()) goalStr = Auxiliary.insertComma(Recorder.getGoal()) goalPercentage = Recorder.getGoalPercentage() # 获取目标完成百分比数值, [0, 100) goalPercentageStr = ('%0.1f' % goalPercentage) + '%' rectWidth = (DisplayMapping.get('RectEnd')[0] - DisplayMapping.get('Rect')[0]) * goalPercentage / 100 rectHeight = DisplayMapping.get('RectEnd')[1] - DisplayMapping.get('Rect')[1] Screen = blitStringAtRight(Screen, 'Turn', turnStr) # 绘制回合数 Screen = blitStringAtRight(Screen, 'Score', scoreStr) # 绘制总分 Screen = blitStringAtRight(Screen, 'Goal', goalStr) # 绘制目标 pygame.draw.rect(Screen, RectColour, [DisplayMapping.get('Rect'), (rectWidth, rectHeight)], 0) # 绘制目标百分比矩形 Screen = blitStringAtRight(Screen, 'Percentage', goalPercentageStr) # 绘制目标完成百分比 return Screen
def processInput(p, m, i, e, SCREEN_W=30, SCREEN_H=22): collisions = Auxiliary.getCollisions(p, m, i) while True: ret = Getch.getch() if ret == "p": Menu.openMenu(p, SCREEN_W, SCREEN_H) # NOTE: DO NOT DELETE -> Collision detected if length of keys is greater than 0 if Auxiliary.isCollided(collisions, 0, ret): continue # Update User Skill Tree itemPickUp(p, collisions, ret) # Collision between enemies # TODO: Make it so you lose health when right next to enemy and not colliding into enemy if Auxiliary.isCollided(collisions, 3, ret): p.attributes["Health"] -= e[0].attack continue return ret
def add_kinetic_auxiliary_protocol(self, aux_index, duration, label, plate_index, protocol_index, order): new_protocol = Auxiliary(aux_index, duration, label, plate_index, order) kinetic_protocol = self.plateList[plate_index].get_protocol( protocol_index) kinetic_protocol.add_protocol(new_protocol)
def parse(self, fname): aux = Auxiliary.Auxiliary() with open(fname, 'r') as f: nums = f.read() nums = aux.fullReplace(nums, ";;", ";") nums = aux.fullReplace(nums, "о/р ", "о/р") print(nums)
def plot(self, plot_type, average): """ Main point of entry. Set off scraper, process and plot data. """ # Dict with appropiate functions for data transforming defined # at bottom of module. (self.x_min, teams_raw, team_set, get_clean, to_plot) = OPTIONS[plot_type] cumulative = aux.Data([], []) for team, raw_data in teams_raw(self.scraper): raw_set = team_set(raw_data) data = get_clean(self, raw_set) to_plot(self, team, data) if average: aux.aggragate_cumulative(cumulative, data) if average: aux.average_data(cumulative, len(self.scraper.teams)) to_plot(self, 'League Average', cumulative)
def initWord(game_list): while True: hit = 0 inp = Auxiliary.quit(input("Choose your difficulty:\n[EASY] [MEDIUM] [HARD]\n").lower()) if inp == "easy" or inp == "medium" or inp == "hard": if inp == "medium" or inp == "hard": hit = (1 if inp == "medium" else 2) break print("Invalid Input") continue split_list = splitListEvenly(sorted(game_list, key=len))[hit] return [split_list[random.randint(0, len(split_list) - 1)], hit]
def moveBears(self) : # print 'moveBears starts...' count = 0 bearPositions = self.map.searchItems(self.map.getItems().keys(), ['Bear']) while len(bearPositions) > 0 : # 仍有未处理过的熊 randomWeight = {} for position in bearPositions : randomWeight[position] = 1 position = Auxiliary.randomSelect(randomWeight) if self.moveBear(position) : count += 1 bearPositions.remove(position) # print 'moveBears OK' return count
def plot_eodform(self, ax, filtr): fu_freq = self.fund_freq low_pass = fu_freq*filtr if self.type_detector() == 'pulse': low_pass = 3000. f_eod = aux.butter_lowpass_filter(self.w_eod, low_pass, self._sample_rate) pt, tt, pe, te = self.w_pt plot_inxs = (self.w_time >= tt[1]) & (self.w_time <= tt[3]) ax.plot(1000. * (self.w_time[plot_inxs] - self.w_time[0]), f_eod[plot_inxs], color='green', lw=2) ax.set_ylabel('Amplitude [au]') # "au" stands for arbitrary unit ax.set_xlabel('Time [ms]') pass
def _plot_outcome_conceding(self, team, data): """ Sets the specific params for of win/loss outcome when team concedes x runs. """ y_axis = self._fit_y_axis(data) record = aux.outcome_record(data, self.histogram) y_label = 'Wins/losses when conceding x runs' if self.histogram else\ 'Total runs sorted by runs conceded per game' tag_label = 'outcome_conceding_histogram' if self.histogram else\ 'outcome_conceding_sorted' self._plot_team(data, record, aux.Labels(team, 'Runs conceded', y_label, tag_label), y_axis)
def add_auxiliary_protocol(self, aux_index, duration, label, plate_index, order): """Function to add an Auxiliary protocol to the list of protocols to execute for a particular PlateConfiguration. The auxiliary protocol type is defined by the aux_index input. Arguments: aux_index {integer} -- possible values 1, 2, 3 or 4 representing which auxiliary port will be triggerd on. duration {integer} -- duration for how long to trigger the selected auxiliary port for. label {string} -- protocol label, should be set to 'Auxiliary' plate_index {integer} -- index of the PlateConfiguration object in self.plateList in which to select wells order {integer} -- the index of the protocol in the existing list of protocols of the plate configuration object. If this is the first protocol that is being added to the PlateConfiguration object then the order is equal to zero. If it is the second, then the order is equal to 1 and so on. """ new_protocol = Auxiliary(aux_index, duration, label, plate_index, order) self.plateList[plate_index].add_protocol(new_protocol)
def calculate_weights(vectors,batch=True): """ Main method for calculating weights of features Vector is feature vector with correct classification appended """ NUMFEATURES = len(vectors[0])-1 weights = np.array([[random.uniform(-INITRANGE,INITRANGE) for i in range(LAYERS[j])] for j in range(NUMLAYERS)]) stochasticIndex = 0 while True: prevWeights = weights if batch: weights = batchDescentStep(vectors,weights) else: weights = stochasticDescentStep(vectors[stochasticIndex],weights) stochasticIndex = (stochasticIndex+1)%len(vectors) if Auxiliary.converged(weights,prevWeights): return weights
def calculate_weights(vectors, batch=True): """ Main method for calculating weights of features Vector is feature vector with correct classification appended """ NUMFEATURES = len(vectors[0]) - 1 weights = np.array([0 for i in range(NUMFEATURES)]) stochasticIndex = 0 while True: prevWeights = weights if batch: weights = batchDescentStep(vectors, weights) else: weights = stochasticDescentStep(vectors[stochasticIndex], weights) stochasticIndex = (stochasticIndex + 1) % len(vectors) if Auxiliary.converged(weights, prevWeights): return weights
def moveBear(self, Position) : # print 'moveBear starts...' if self.map.checkValidity(Position) == INSIDE : # 在地图内 item = self.map.getItem(Position) if item and item.getName() == 'Bear' : # 物品是熊 positions = self.map.searchItems(self.map.getNeighbours(Position)) if len(positions) == 0 : # 相邻位置均不空闲 return False randomWeight = {} for position in positions : randomWeight[position] = 1 destination = Auxiliary.randomSelect(randomWeight) if self.map.moveItem(Position, destination) : # 移动成功 return True else : # 移动失败 return False else : return False # 物品不为熊 else : return False # 在地图外
def train(self,X,Y,batch=True): """ Main method for calculating weights of features Vector is feature vector with correct classification appended """ self.d = len(X[0]) n = len(X) self.w = np.zeros(d) step = 0 while True: w_prev = np.copy(self.w) if batch: self.batchDescentStep(X,Y) else: self.stochasticDescentStep(X[step],Y[step]) step = (step+1) % n if Auxiliary.converged(self.w,w_prev): return
def TAG_DICT_DYNAMIC(self, id_, no): while True: try: if self.d.release(id_).tracklist[no].artists != []: artist_tag = [ artist.name for artist in self.d.release(id_).tracklist[no].artists ] else: artist_tag = [ artist.name for artist in self.d.release(id_).artists ] return { "artist": artist_tag, "title": self.d.release(id_).tracklist[no].title, "tracknumber": aux.format_track_number(no + 1) } except discogs_client.exceptions.HTTPError: print('Discogs needs cool-down time. Be patient!') time.sleep(2.0)
def engine(w): guesses = 0 max_guesses = 7 initialized = Game.initWord(w) game_word = initialized[0] difficulty = initialized[1] game = Game.initGame(game_word) while True: print(''.join(game)) inp = Auxiliary.processInput() if Game.update_game(game_word, inp, game) == 0: print("Incorrect Guess\n" + HANGMANPICS(guesses)) guesses += 1 if ''.join(game).replace(" ", "") == game_word: print("You guessed " + game_word + " correctly!") return [2, difficulty] if guesses == max_guesses: print("GAME OVER! You did not guess the word " + game_word) return [1, difficulty] return [0, 0]
def DSOD(): image_size, num_classes = CONFIG['image_size'], CONFIG['num_classes'] input = layer.Input(image_size) x, output = BackBone(input) x1, x2, x3, x4, x5, x6 = Auxiliary(x, output) x1_conf, x1_loc, x1_priorbox = predict_block(x1, 'x1', 3, 30.0, [2]) x2_conf, x2_loc, x2_priorbox = predict_block(x2, 'x2', 6, 60.0, [2, 3], 114.0) x3_conf, x3_loc, x3_priorbox = predict_block(x3, 'x3', 6, 114.0, [2, 3], 168.0) x4_conf, x4_loc, x4_priorbox = predict_block(x4, 'x4', 6, 168.0, [2, 3], 222.0) x5_conf, x5_loc, x5_priorbox = predict_block(x5, 'x5', 6, 222.0, [2, 3], 276.0) x6_conf, x6_loc, x6_priorbox = predict_block(x6, 'x6', 6, 276.0, [2, 3], 330.0) conf = layer.Concatenate(axis=1, name='conf')( [x1_conf, x2_conf, x3_conf, x4_conf, x5_conf, x6_conf]) loc = layer.Concatenate( axis=1, name='loc')([x1_loc, x2_loc, x3_loc, x4_loc, x5_loc, x6_loc]) priorbox = layer.Concatenate(axis=1, name='priorbox')([ x1_priorbox, x2_priorbox, x3_priorbox, x4_priorbox, x5_priorbox, x6_priorbox ]) num_boxes = K.int_shape(loc)[-1] // 4 conf = layer.Reshape((num_boxes, num_classes), name='conf_reshape')(conf) conf = layer.Activation(activation='softmax', name='conf_softmax')(conf) loc = layer.Reshape((num_boxes, 4), name='loc_reshape')(loc) prediction = layer.Concatenate(axis=2, name='prediction')([loc, conf, priorbox]) model = keras.Model(input=input, output=prediction) return model
def detect_peak_and_trough_indices(self, peak_threshold=None, norm_window=.1): """This function finds the indices of peaks and troughs of each EOD-cycle in the recording :param peak_threshold: This is the threshold to be used for the peakdet function (Translated Matlab-Code...). :param norm_window: :return: two arrays. The first contains the peak indices and the second contains the trough indices. """ if self._eod_peak_idx is None or self._eod_trough_idx is None: w = np.ones(self._sample_rate*norm_window) w[:] /= len(w) eod2 = np.sqrt(np.correlate(self._eod**2., w, mode='same') - np.correlate(self._eod, w, mode='same')**2.) eod2 = self._eod / eod2 if peak_threshold is None: peak_threshold = np.percentile(np.abs(eod2), 99.9)-np.percentile(np.abs(eod2), 70) # The Threshold is 1.5 times the standard deviation of the eod _, self._eod_peak_idx, _, self._eod_trough_idx = aux.peakdet(eod2, peak_threshold) # refine by matching troughs and peaks everything = list(self.peak_trough_iterator()) _, _, self._eod_peak_idx, _, _, self._eod_trough_idx = map(lambda x: np.asarray(x), zip(*everything)) return self._eod_peak_idx, self._eod_trough_idx
def filtr_data(self, filter_fac=5.5): fund_freq = self.fund_freq() filtered_data = aux.butter_lowpass_filter(self._eod, fund_freq * filter_fac, self._sample_rate) return filtered_data
def main(audio_file, channel=0, output_folder='.' + os.path.sep + 'analysis_output', verbose=None): # get config dictionary cfg = ct.get_config_dict() if verbose is not None: cfg['verboseLevel'][0] = verbose channel = channel with audioread.audio_open(audio_file) as af: tracen = af.channels if channel >= tracen: print 'number of traces in file is', tracen quit() ft = FT.FishTracker(audio_file.split(os.path.sep)[-1], af.samplerate) index = 0 data = ft.get_data() for buffer in af: fulldata = np.fromstring(buffer, dtype='<i2').reshape(-1, af.channels) n = fulldata.shape[0] if index + n > len(data): if index == 0: print "panic!!!! I need a larger buffer!" # ft.processdata(data[:index] / 2.0 ** 15) index = 0 if n > 0: data[index:index + n] = fulldata[:n, channel] index += n else: break # long file analysis good_file = ft.exclude_short_files(data, index) if good_file == False: print "file too short !!!" exit() # best window algorithm mod_file = aux.conv_to_single_ch_audio(audio_file) Fish = FR.FishRecording(mod_file) bwin, win_width = Fish.detect_best_window() print '\nbest window is between: %.2f' % bwin, '& %.2f' % (bwin + win_width), 'seconds.\n' os.remove(mod_file) # fish_type algorithm fish_type = Fish.type_detector() print('current fish is a ' + fish_type + '-fish') # data process: creation of fish-lists containing frequencies, power of fundamentals and harmonics of all fish if index > 0: power_fres1, freqs_fres1, psd_type, fish_type,\ fishlist = ft.processdata(data[:index] / 2.0 ** 15, fish_type, bwin, win_width, config_dict=cfg) # Pulse analysis pulse_data = [] pulse_freq = [] if psd_type == 'pulse' or fish_type == 'pulse': print '' print 'try to create MEAN PULSE-EOD' print '' pulse_data, pulse_freq = ft.pulse_sorting(bwin, win_width, data[:index] / 2.0 ** 15) # create EOD plots out_folder = aux.create_outp_folder(audio_file, output_folder) ft.bw_psd_and_eod_plot(power_fres1, freqs_fres1, bwin, win_width, data[:index] / 2.0 ** 15, psd_type, fish_type, fishlist, pulse_data, pulse_freq, out_folder) # saves fundamentals of all wave fish !!! st.save_fundamentals(fishlist, out_folder) print('\nAnalysis completed! .npy arrays located in %s\n' %out_folder)
def __init__(self, wavfile): self._wavfile = wavfile self._time, self._eod, self._sample_rate = aux.load_trace(wavfile) self._eod_peak_idx = None self._eod_trough_idx = None self.roi = None
"FILEPATH".format(in_dir)) presenting_meids = vision_envelope_codebook.query( "type == 'pres'").modelable_entity_id.values logging.info("the presenting vision loss envelope meids are " + str( presenting_meids)) # use get draws to pull envelope meids for presenting moderate/severe/blindness envelopes = {} for meid in presenting_meids: sev = vision_envelope_codebook.query( "modelable_entity_id == @meid").sev.values[0].upper() logging.info(""" Loading dismod draws for presenting {} vision loss envelope, meid {}""".format(sev, meid)) envelopes[sev] = aux.get_vision_draws(meid=meid, measure_id=5, location_id=location_id) if cfg.testing: envelopes[sev].to_csv("FILEPATH".format( intermediate_out_dir, location_id, sev), index=False) # append the 3 envelopes together total_vision_loss_envelope = envelopes[ 'LOW_MOD'].append([envelopes['LOW_SEV'], envelopes['BLIND']]) # sum the prevalence of each envelope together to get total prevalence total_vision_loss_envelope = total_vision_loss_envelope.groupby( ['location_id', 'age_group_id', 'year_id', 'sex_id']) total_vision_loss_envelope = total_vision_loss_envelope.sum().reset_index() query = "age_group_id == 4 and sex_id == 2 and year_id == {}".format(2019)
# Starting time loop # ============================================================================= plt.ion() plt.figure(1, figsize=(11, 8.5)) style.use('ggplot') for t in range(1, npt + 1): # Generating analytical solution Ca = AN.difuana(M, L, Dx, xn, xo, T0 + t * dT) # Estimating solution C1 in t CL = AN.difuana(M, L, Dx, X0, xo, T0 + (t - 1) * dT) CR = AN.difuana(M, L, Dx, XL, xo, T0 + (t - 1) * dT) spa = AUX.FVev_sp(C, Dx, dx, CL, CR) C1 = C + dT * spa # Implicit part of the solution # Imposing boundary conditions C[0] = C[0] + AN.difuana(M, L, Dx, X0, xo, T0 + t * dT) * Sx * 2 C[len(xn) - 1] = C[len(xn) - 1] + AN.difuana(M, L, Dx, XL, xo, T0 + t * dT)\ * Sx * 2 # Solving linear system C1i = spsolve(K, C) # Crank-Nicholson ponderation C1t = (1 - theta) * C1 + theta * C1i
plt.xlabel(r'Distance $(m)$') plt.ylabel(r'Concentration $ \frac{kg}{m} $') plt.pause(3) plt.close(1) # ============================================================================= # Solving first timestep with forward Euler # ============================================================================= # Generating analytical solution Ca = AN.difuana(M, L, Dx, xn, xo, T0 + dT) # Estimating solution C1 in t CL1 = Ca[0] CR1 = Ca[len(xn) - 1] spa = AUX.FVev_sp(C, Dx, dx, CL1, CR1) C1 = C + dT * spa # ============================================================================= # Starting time loop # ============================================================================= plt.ion() plt.figure(1, figsize=(11, 8.5)) style.use('ggplot') for t in range(2, npt + 1): # Generating analytical solution Ca = AN.difuana(M, L, Dx, xn, xo, T0 + t * dT)
def main(audio_file, channel=0, output_folder='.' + os.path.sep + 'analysis_output', verbose=None, beat_plot= False): # get config dictionary cfg = ct.get_config_dict() if verbose is not None: cfg['verboseLevel'][0] = verbose channel = channel with audioread.audio_open(audio_file) as af: tracen = af.channels if channel >= tracen: print('number of traces in file is', tracen) quit() ft = FT.FishTracker(audio_file.split(os.path.sep)[-1], af.samplerate) index = 0 data = ft.get_data() for buffer in af: fulldata = np.fromstring(buffer, dtype='<i2').reshape(-1, af.channels) n = fulldata.shape[0] if index + n > len(data): if index == 0: print("panic!!!! I need a larger buffer!") # ft.processdata(data[:index] / 2.0 ** 15) index = 0 if n > 0: data[index:index + n] = fulldata[:n, channel] index += n else: break # long file analysis good_file = ft.exclude_short_files(data, index) if good_file == False: print("file too short !!!") exit() # best window algorithm mod_file = aux.conv_to_single_ch_audio(audio_file) Fish = FR.FishRecording(mod_file) bwin, win_width = Fish.detect_best_window() print ('\nbest window is between: %.2f' % bwin, '& %.2f' % (bwin + win_width), 'seconds.\n') os.remove(mod_file) # fish_type algorithm fish_type, r_value = Fish.type_detector() print('current fish is a ' + fish_type + '-fish') # data process: creation of fish-lists containing frequencies, power of fundamentals and harmonics of all fish if index > 0: power_fres1, freqs_fres1, psd_type, fish_type,\ fishlist, mean_proportions = ft.processdata(data[:index] / 2.0 ** 15, fish_type, bwin, win_width, config_dict=cfg) ##################################################### # collect data for mat meth figure: # bw_data = data[(bwin * af.samplerate):(bwin * af.samplerate + win_width * af.samplerate)] # np.save('pulse_trace_data.npy', bw_data) if beat_plot: # designed for parama_data/20140519_Rioanita/EN099.wav beat_data = data[(1.2 * af.samplerate): (1.9 * af.samplerate)] / 2.0 ** 15 beat_time = np.arange(len(beat_data)) * 1.0 / af.samplerate aux.beat_plot(beat_data, beat_time) embed() quit() ##################################################### # Pulse analysis pulse_data = [] pulse_freq = [] if psd_type == 'pulse' or fish_type == 'pulse': print('') print('try to create MEAN PULSE-EOD') print('') pulse_data, pulse_freq = ft.pulse_sorting(bwin, win_width, data[:index] / 2.0 ** 15, fish_type, psd_type) wave_data = [] if fish_type == 'wave' and len(fishlist) == 1: print('') print('try to create MEAN WAVE-EOD') print('') wave_data = ft.wave_sorting(bwin, win_width, data[:index] / 2.0 ** 15) # create EOD plots out_folder = aux.create_outp_folder(audio_file, output_folder) ft.bw_psd_and_eod_plot(power_fres1, freqs_fres1, bwin, win_width, data[:index] / 2.0 ** 15, psd_type, fish_type, fishlist, pulse_data, pulse_freq, out_folder, mean_proportions, r_value, wave_data) # saves fundamentals of all wave fish !!! st.save_fundamentals(fishlist, out_folder) print('\nAnalysis completed! .npy arrays located in %s\n' %out_folder)
def AB2D(Nx, Ny, dt): # ============================================================================== # Declaration of physical variables for the problem. A positive value in X means # going to the right and a positive value in Y means going up # ============================================================================== X0 = 0. # Initial x point (m) XL = 5. # Final y point (m) Y0 = 0. # Initial y point (m) YL = 5. # Final y point (m) Dx = 0.08 # Diff coeff x (m2/s) Dy = 0.08 # Diff coeff y (m2/s) u = 0.7 # Horizontal velocity (m/s) v = 0.3 # Vertical velocity (m/s) M = 2. # Mass injected (g) xC0 = 0.0 # x injection coordinate yC0 = 0.0 # y injection coordinate t0 = 1. # Initial time (s) A = (XL - X0) * (YL - Y0) # Domain area (m2) # ============================================================================= # Numerical parameters for the program. # ============================================================================= T = 5 # Total sim. time (s) # dt = 0.001 # timestep size (s) # Nx = 21 # Nodes in x direction # Ny = 21 # Nodes in y direction # Calculation of initial parameters nT = int(np.ceil((T - t0) / dt)) # Number of timesteps dx = (XL - X0) / (Nx - 1) # dx (m) dy = (YL - Y0) / (Ny - 1) # dy (m) # Generating spatial mesh x = np.linspace(X0, XL, Nx) # 1D array style y = np.linspace(Y0, YL, Ny) # 1D array style X, Y = np.meshgrid(x, y) # Meshgrid for plotting del (x, y) # Deleting useless arrays # Generating error vectors (for svaing and comparing results) errt = np.zeros(int(nT)) # Error evolution in time # ============================================================================== # Calculation of nondimensional parameters of the ADE (Sx, Sy, CFLx and CFLy) # ============================================================================== Sx = Dx * dt / (dx**2) Sy = Dy * dt / (dy**2) CFLx = u * dt / dx CFLy = v * dt / dy SM.show_sc(Sx, Sy, CFLx, CFLy) # ============================================================================== # Imposing initial condition as an early analytical solution of the ADE equation # It is C(x0, y0, t0). # ============================================================================== C0 = AN.difuana(M, A, Dx, Dy, u, v, xC0, yC0, X, Y, t0) # Estimating Cmax for plotting purposes Cmax = np.max(C0) # ============================================================================== # First time step (must be done in a forward Euler time scheme since there are # no other points in time). The first step has a size dt, hence the total time # is (1 * dt) # ============================================================================== # Calculating analytical solution for the first timestep Ca = AN.difuana(M, A, Dx, Dy, u, v, xC0, yC0, X, Y, t0 + dt) C1e = np.zeros((Ny, Nx)) sp0 = AUX.ev_sp(C0, Dx, Dy, dx, dy, u, v, Nx, Ny) C1e = dt * sp0 + C0 # Imposing boundary conditions (domain is a 2D array) C1e[0, :] = Ca[0, :] # Bottom boundary C1e[Ny - 1, :] = Ca[Ny - 1, :] # Top boundary C1e[:, 0] = Ca[:, 0] # Left boundary C1e[:, Nx - 1] = Ca[:, Nx - 1] # Right boundary # Calculating first time step error errt[0] = np.linalg.norm(C1e - Ca) # Second array declaration C2e = np.zeros((Ny, Nx)) err = np.zeros((Ny, Nx)) # starting time loop for I in range(2, nT + 1): # Calculating analytical solution for the time step Ca = AN.difuana(M, A, Dx, Dy, u, v, xC0, yC0, X, Y, t0 + I * dt) # Spatial function evaluated in t sp0 = AUX.ev_sp(C0, Dx, Dy, dx, dy, u, v, Nx, Ny) # Spatial function evaluated in t - 1 sp1 = AUX.ev_sp(C1e, Dx, Dy, dx, dy, u, v, Nx, Ny) # Implementing 2nd order Adams-Bashforth C2e = C1e + (dt / 2) * (3 * sp0 - sp1) # Imposing Boundary conditions C2e[0, :] = Ca[0, :] C2e[Ny - 1, :] = Ca[Ny - 1, :] C2e[:, 0] = Ca[:, 0] C2e[:, Nx - 1] = Ca[:, Nx - 1] # Calculating error for the timestep given err = np.abs(C2e - Ca) errt[I - 1] = np.linalg.norm(C2e - Ca) # Updating concentration fields C0 = C1e C1e = C2e C2e = np.zeros((Ny, Nx)) return errt
def FT2D(Nx, Ny, dt, theta): # ============================================================================== # Declaration of physical variables for the problem. A positive value in X means # going to the right and a positive value in Y means going up # ============================================================================== X0 = 0. # Initial x point (m) XL = 5. # Final y point (m) Y0 = 0. # Initial y point (m) YL = 5. # Final y point (m) Dx = 0.08 # Diff coeff x (m2/s) Dy = 0.08 # Diff coeff y (m2/s) u = 0.7 # Horizontal velocity (m/s) v = 0.3 # Vertical velocity (m/s) M = 2. # Mass injected (g) xC0 = 0.0 # x injection coordinate yC0 = 0.0 # y injection coordinate t0 = 1. # Initial time (s) A = (XL - X0) * (YL - Y0) # Domain area (m2) # ============================================================================= # Numerical parameters for the program. # theta = 1 fully implicit # theta = 0 fully explicit # ============================================================================= T = 5 # Total sim. time (s) # dt = 0.01 # timestep size (s) # Nx = 21 # Nodes in x direction # Ny = 21 # Nodes in y direction # theta = 0.5 # Crank-Nicholson ponderation # Calculation of initial parameters nT = int(np.ceil((T - t0) / dt)) # Number of timesteps dx = (XL - X0) / (Nx - 1) # dx (m) dy = (YL - Y0) / (Ny - 1) # dy (m) # Generating spatial mesh x = np.linspace(X0, XL, Nx) # 1D array style y = np.linspace(Y0, YL, Ny) # 1D array style X, Y = np.meshgrid(x, y) # Meshgrid for plotting del (x, y) # Deleting useless arrays # Generating error vectors (for svaing and comparing results) errt = np.zeros(int(nT)) # Error evolution in time # ============================================================================== # Calculation of nondimensional parameters of the ADE (Sx, Sy, CFLx and CFLy) # ============================================================================== Sx = Dx * dt / (dx**2) Sy = Dy * dt / (dy**2) CFLx = u * dt / dx CFLy = v * dt / dy SM.show_sc(Sx, Sy, CFLx, CFLy) # Matrix assembly for diffusive implicit part LHS = AUX.Mat_ass(Sx, Sy, Nx, Ny) # ============================================================================== # Imposing initial condition as an early analytical solution of the ADE equation # It is C(x0, y0, t0). # ============================================================================== C0 = AN.difuana(M, A, Dx, Dy, u, v, xC0, yC0, X, Y, t0) # Estimating Cmax for plotting purposes Cmax = np.max(C0) # ============================================================================== # First time step. Forward Euler for advective part, and Crank-Nicholson for # diffusive term # ============================================================================== # First fractional step part for timestep 1 - advective term # Calculating analytical solution for first timestep Ca = AN.difuana(M, A, Dx, Dy, u, v, xC0, yC0, X, Y, t0 + dt) # Evaluating space derivative just for advections sp0 = AUX.ev_sp(C0, 0, 0, dx, dy, u, v, Nx, Ny) # Making first advection step C1s = C0 + dt * sp0 # Imposing boundary conditions (domain is a 2D array) C1s[0, :] = Ca[0, :] # Bottom boundary C1s[Ny - 1, :] = Ca[Ny - 1, :] # Top boundary C1s[:, 0] = Ca[:, 0] # Left boundary C1s[:, Nx - 1] = Ca[:, Nx - 1] # Right boundary # Second fractional step part for timestep 1 - diffusive term # Explicit part - evaluating diffusion just in space sp0 = AUX.ev_sp(C1s, Dx, Dy, dx, dy, 0, 0, Nx, Ny) C1e = C1s + dt * sp0 # Implicit part C1i = spsolve(LHS, np.reshape(C1s, (Nx * Ny, 1))) C1 = theta * C1i.reshape(Nx, Ny) + (1 - theta) * C1e # Error checking errt[0] = np.linalg.norm(C1 - Ca) # Second array declaration C2s = np.zeros((Ny, Nx)) err = np.zeros((Ny, Nx)) # Starting time loop for I in range(2, nT + 1): # Calculating analytical solution for the time step Ca = AN.difuana(M, A, Dx, Dy, u, v, xC0, yC0, X, Y, t0 + I * dt) # Calculating C* - only advection with Adams Bashforth Order 2 # Spatial function evaluated in t - 1 sp0 = AUX.ev_sp(C0, 0, 0, dx, dy, u, v, Nx, Ny) # Spatial function evaluated in t sp1 = AUX.ev_sp(C1, 0, 0, dx, dy, u, v, Nx, Ny) # Implementing 2nd order Adams-Bashforth C2s = C1 + (dt / 2) * (3 * sp0 - sp1) # Imposing Boundary conditions C2s[0, :] = Ca[0, :] C2s[Ny - 1, :] = Ca[Ny - 1, :] C2s[:, 0] = Ca[:, 0] C2s[:, Nx - 1] = Ca[:, Nx - 1] # Calculating C2 with the values of C* - explicit FE C2sp = AUX.ev_sp(C2s, Dx, Dy, dx, dy, 0, 0, Nx, Ny) C2e = C2s + dt * C2sp # Calculating C2 with C* - implicit BE C2i = spsolve(LHS, np.reshape(C2s, (Nx * Ny, 1))) # Making Crank-Nicholson ponderation C2 = theta * C2i.reshape(Nx, Ny) + (1 - theta) * C2e # Calculating error for the timestep given err = np.abs(C2 - Ca) errt[I - 1] = np.linalg.norm(C2e - Ca) # Updating concentration fields C0 = C1 C1 = C2 C2 = np.zeros((Ny, Nx)) return errt
def steepest_ascent_hill_climbing_solution(current_state, nurses_number): """ Solve the Steepest-Ascent Hill Climbing problem This is the function that will solve the Steepest-Ascent Hill Climbing problem. It receives two parameters that will be used to solve the problem. Both has to be defined when called :parameter current_state: State that will be analyzed for a solution :type current_state: String composed of 0's and 1's :parameter nurses_number: Number of nurses that will be used to solve the problem :type nurses_number: Integer with the number of nurses :return current_state: The last state that was analyzed :rtype current_state: String :return current_violations: The number of violations of the last state evaluated :rtype current_violations Int Important variables ------------------- current_violations : Will save the violation value to the current state current_state : Will save the current state bit_position : Used to control which bit will be changed to generate the neighbors state last_violation : Will save the value to the violation of the last state analyzed best_violation_found : Will save the best violation found last_state : Will save the last state analyzed best_state_found : Will save the best state that was found state_loop : Used to control the neighbors generation """ # Here is evaluated the current state and verified if is the gol state current_violations = Au.check_objective(current_state, nurses_number) if current_violations == 0: Au.print_output('Objective state found in first state evaluated', current_state, current_violations, nurses_number) return current_state, current_violations # If it isn't the gol state, we will start the loop and generate the other states to find the best solution possible bit_position = 0 # Here is shown the state that is being analyzed Au.print_output('Root state', current_state, current_violations, nurses_number) # Variables that will be used to save analyzed states to be used after to comparisons or similar last_violation = current_violations best_violation_found = current_violations last_state = current_state best_state_found = current_state while True: state_loop = 0 # Search for the best state to follow inside all the possible states generated while state_loop < 21*nurses_number: # Will get the possible states and verify if it is the best found at this loop current_state, bit_position = Au.state_generator(last_state, bit_position, nurses_number) current_violations = Au.check_objective(current_state, nurses_number) # If the best state than the saved, so the saved is updated if current_violations < best_violation_found: best_violation_found = current_violations best_state_found = current_state state_loop += 1 bit_position = 0 # The current state and the current_violations are updated current_state = best_state_found current_violations = best_violation_found # If the current violation great equals the last best violation, so the best possible was found if current_violations >= last_violation: Au.print_output('Best solution found', current_state, current_violations, nurses_number) return current_state, current_violations # If current violations equals 0, so the gol state was found if current_violations == 0: Au.print_output('Objective state found', current_state, current_violations, nurses_number) return current_state, current_violations # Otherwise, we will show the state analyzed and will repeat the loop for a better state Au.print_output('State generated', current_state, current_violations, nurses_number) # Here we're updating the last violation and the last_state to be used latter last_violation = current_violations last_state = current_state
plt.subplot(1, 1, 1) plt.plot(x, C) plt.title('Initial condition') plt.xlabel(r'Distance $(m)$') plt.ylabel(r'Concentration $ \frac{kg}{m} $') plt.pause(3) plt.close(1) # ============================================================================== # Entering the time loop # ============================================================================== # First time step Ca = AN.difuana(M, L, Dx, x, xo, T0 + dT) spa = AUX.FDev_sp(C, Dx, dx) C1 = C + dT * spa C1[0] = Ca[0] C1[N - 1] = Ca[N - 1] # Starting plot plt.ion() plt.figure(1, figsize=(11, 8.5)) style.use('ggplot') plt.subplot(1, 1, 1) plt.plot(x, C) plt.title('Initial condition 2') plt.xlabel(r'Distance $(m)$')
for directory in [out_dir, intermediate_out_dir, final_out_dir]: if not os.path.exists(directory): os.makedirs(directory) #----GET ENVELOPE LEVEL PREVALENCE AND SUM THE ENVELOPES TOGETHER TO GET TOTAL-- # Create a numpy array of presenting envelope modelable entity ids vision_envelope_codebook = pd.read_csv(FILEPATH) presenting_meids = vision_envelope_codebook.query("type == 'pres'").modelable_entity_id.values print("the presenting vision loss envelope meids are " + str(presenting_meids)) # use get draws to pull envelope meids for presenting moderate/severe/blindness envelopes = {} for meid in presenting_meids: sev = vision_envelope_codebook.query("modelable_entity_id == @meid").sev.values[0].upper() print("Loading dismod draws for presenting {s} vision loss envelope, meid {m}".format(s=sev, m=meid)) envelopes[sev] = aux.get_vision_draws(meid=meid, measure_id=5, location_id=location_id) if cfg.testing: envelopes[sev].to_csv(intermediate_out_dir + \ "/{l}_{s}_envelope_get_draws_pull.csv".format(l=location_id, s=sev), index=False) # append the 3 envelopes together total_vision_loss_envelope = envelopes['LOW_MOD'].append([envelopes['LOW_SEV'], envelopes['BLIND']]) # sum the prevalence of each envelope together to get total prevalence total_vision_loss_envelope = total_vision_loss_envelope.groupby(['location_id', 'age_group_id', 'year_id', 'sex_id']) total_vision_loss_envelope = total_vision_loss_envelope.sum().reset_index() # quick sanity check to make sure the envelope summing occurred correctly query = "age_group_id == 4 and sex_id == 2 and year_id == 2017" mod = envelopes['LOW_MOD'].query(query).draw_100.values[0] sev = envelopes['LOW_SEV'].query(query).draw_100.values[0]
def itemPickUp(p, c, r): if Auxiliary.isCollided(c, 1, r): # GOLD p.gold += random.randint(5, 15) if Auxiliary.isCollided(c, 2, r): #SWORD p.attributes['Strength'] += 15
def all_converged(centers,lastCenters): """Checks if centers have converged to a stable equilibrium""" for i in range(len(centers)): if not Auxiliary.has_converged(centers[i],lastCenters[i]): return False return True
def generateItem(self, RandomWeight) : return Class_Item(Auxiliary.randomSelect(RandomWeight))
import Auxiliary import adabound import restestmodel dtype = torch.cuda.FloatTensor # dtype = torch.FloatTensor model = CnnModel.CNN().cuda() # model = restestmodel.ResNet(restestmodel.Bottleneck, [3, 4, 23, 3]) # model = CnnModel.CNN() # model.load_state_dict(torch.load('CnnParameters.pt')) # model.load_state_dict(torch.load('Res.pt')) path = 'C:/Users/iamtyz/Desktop/1234' batch = 50 # DataSets = list(range(1, 6001)) DataSets = list(range(1, 7803)) DataSetsOrders = Auxiliary.Shuffle(DataSets, batch) criterion = nn.MSELoss(reduce=True, size_average=False) # optimizer = adabound.AdaBound(model.parameters(), lr=1e-3, final_lr=1e-5) optimizer = optim.Adam(model.parameters()) # optimizer = optim.SGD(model.parameters(), lr=2e-5, momentum=0.9) # epoch = 0 loop = 0 for epoch in range(1000): # while 1: time = dt.datetime.now().isoformat() order = DataSetsOrders[loop:loop + batch] if loop == int(len(DataSetsOrders) / batch): loop = 0 loop += batch epoch += 1 # order = random.sample(DataSetsOrders, batch)
def FVM1D(Sx, N): # ============================================================================== # Variable declaration # ============================================================================== X0 = 0. # Initial x coordinate XL = 5. # Final x coordinate M = 10 # Mass injected xo = 2 # Injection coordinate Dx = 0.3 # Diffusion coefficient T0 = 1. # Initial time Tf = 5. # Final time # ============================================================================== # Numerical parameters # theta = 0 --> Fully explicit # theta = 1 --> Fully implicit # ============================================================================== # Sx = 0.3 theta = 0.5 # C-N ponderation factor # N = 41 # Volumes in the domain L = XL - X0 # Domain length dx = L / N # Calculating spacing xn = np.zeros(N) # Node coordinates vector # Filling vector of node coordinates for ic in range(0, N): xn[ic] = ic * dx + dx / 2 # Calculating dT and number of timesteps dT = Sx * (dx**2) / (Dx) # Calculating timestep size npt = int(np.ceil((Tf - T0) / (dT))) # Number of timesteps # Generating vector that stores error in time ert = np.zeros(int(npt) + 1) # ============================================================================== # Generating matrix for implicit solver # ============================================================================== K = SP.lil_matrix((N, N)) K[0, 0] = 1 + 3 * Sx K[0, 1] = -Sx K[N - 1, N - 1] = 1 + 3 * Sx K[N - 1, N - 2] = -Sx for i in range(1, N - 1): K[i, i] = 1 + 2 * Sx K[i, i + 1] = -Sx K[i, i - 1] = -Sx # ============================================================================== # Generating initial condition # ============================================================================== C = AN.difuana(M, L, Dx, xn, xo, T0) C1 = np.zeros(N) Cmax = np.max(C) # # Plotting initial condition # plt.ion() # plt.figure(1, figsize=(11, 8.5)) # style.use('ggplot') # # plt.subplot(1, 1, 1) # plt.plot(xn, C) # plt.title('Initial condition') # plt.xlabel(r'Distance $(m)$') # plt.ylabel(r'Concentration $ \frac{kg}{m} $') # ============================================================================= # Starting time loop # ============================================================================= # plt.ion() # plt.figure(1, figsize=(11, 8.5)) # style.use('ggplot') for t in range(1, npt + 1): # Generating analytical solution Ca = AN.difuana(M, L, Dx, xn, xo, T0 + t * dT) # Estimating solution C1 in t CL = AN.difuana(M, L, Dx, X0, xo, T0 + (t - 1) * dT) CR = AN.difuana(M, L, Dx, XL, xo, T0 + (t - 1) * dT) spa = AUX.FVev_sp(C, Dx, dx, CL, CR) C1 = C + dT * spa # Implicit part of the solution # Imposing boundary conditions C[0] = C[0] + AN.difuana(M, L, Dx, X0, xo, T0 + t * dT) * Sx * 2 C[len(xn) - 1] = C[len(xn) - 1] + AN.difuana(M, L, Dx, XL, xo, T0 + t * dT)\ * Sx * 2 # Solving linear system C1i = spsolve(K, C) # Crank-Nicholson ponderation C1t = (1 - theta) * C1 + theta * C1i # Estimating error err = np.abs(C1t - Ca) ert[t] = np.linalg.norm(err) # # Plotting numerical solution and comparison with analytical # plt.clf() # # plt.subplot(2, 2, 1) # plt.plot(xn, C1t, 'b') # plt.xlim([X0, XL]) # plt.ylim([0, Cmax]) # plt.ylabel(r'Concentration $ \frac{kg}{m} $') # plt.title('Numerical solution') # # plt.subplot(2, 2, 2) # plt.plot(xn, Ca) # plt.xlim([X0, XL]) # plt.ylim([0, Cmax]) # plt.title('Analytical solution') # # plt.subplot(2, 2, 3) # plt.semilogy(xn, err) # plt.xlim([X0, XL]) # plt.ylim([1e-8, 1e2]) # plt.ylabel('Absolute error') # plt.title('Error') # # plt.subplot(2, 2, 4) # plt.semilogy(np.linspace(T0, Tf, npt), ert) # plt.xlim([T0 - 0.2, Tf + 0.2]) # plt.ylim([1e-8, 1e2]) # plt.title('Error evolution') # # plt.draw() # plt.pause(0.2) # Preparing for next timestep C = C1t return ert
def main(): """ This function is responsible for start the menu of the program to execute options that the user will choose to solve the problem of nurses allocation :return: void """ option = 1 while option != 0: print('Choose one option') print('1 - Simulated Annealing') print('2 - Genetic Algorithm') print('3 - Documentation') print('4 - About') print('5 - Exit') option = int(input()) if option == 1: temperature = 350 nurses_number = 10 initial_state = None option_temperature_value = int( input( 'Do you want to define a value to temperature? (Default: 350)\n' '1 - Yes\t\t2 - No\n')) if option_temperature_value == 1: temperature = int( input('Enter with the value of the temperature:\n')) option_nurses_number = int( input( 'Do you want to define a number of nurses? (Default: 10)\n' '1 - Yes\t\t2 - No\n')) if option_nurses_number == 1: nurses_number = int(input('Enter number of nurses:\n')) option_state = int( input('Do you want define the initial state?\n' '1 - Yes\t\t2 - No\n')) if option_state == 1: iterator = 0 print( 'Alert: An state consists of a string of bits (0 or 1).\n' 'Insert the bits of each position all together, without spaces \n' 'or any other character.The individual contains ', 21 * nurses_number, ' bits.\n') initial_state = input('Enter with the initial state :\n') while not Au.check_chromosome_composition( chromosome=initial_state, nurses_number=nurses_number): print('Invalid Input') initial_state = input('Enter with the individual number ' + str(iterator + 1) + ':\n') Sa.simulated_annealing_solution(current_state=initial_state, nurses_number=nurses_number, temperature=temperature) elif option == 2: population_size = 40 amount_of_generations = 1000 mutation_probability = .05 elitism_percentage = .01 nurses_number = 10 population = list() option_population_size = int( input('Do you want define population size? (Default: 40)\n' '1 - Yes\t\t2 - No\n')) if option_population_size == 1: population_size = int(input('Enter population size:\n')) option_amount_of_generations = int( input( 'Do you want to define a amount of generations? (Default: 1000)\n' '1 - Yes\t\t2 - No\n')) if option_amount_of_generations == 1: amount_of_generations = int( input('Enter amount of generations:\n')) option_mutation_probability = int( input( 'Do you want to define a mutation probability? (Default: 5%)\n' '1 - Yes\t\t2 - No\n')) if option_mutation_probability == 1: mutation_probability = float( input('Enter mutation probability (Ex: 0.05 to 5%):\n')) option_elitism_percentage = int( input( 'Do you want to define a elitism percentage? (Default: 1%)\n' '1 - Yes\t\t2 - No\n')) if option_elitism_percentage == 1: elitism_percentage = float( input('Enter elitism percentage (Ex: 0.25 to 25%):\n')) option_nurses_number = int( input( 'Do you want to define a number of nurses? (Default: 10)\n' '1 - Yes\t\t2 - No\n')) if option_nurses_number == 1: nurses_number = int(input('Enter number of nurses:\n')) option_population = int( input('Do you want define each individual?\n' '1 - Yes\t\t2 - No\n')) if option_population == 1: iterator = 0 print( 'Alert: Each individual consists of a string of bits (0 or 1).\n' 'Insert the bits of each chromosome all together, without spaces \n' 'or any other character.The individual contains ', 21 * nurses_number, ' bits.\n') while iterator < population_size: individual = input('Enter with the individual number ' + str(iterator + 1) + ':\n') if Au.check_chromosome_composition( chromosome=individual, nurses_number=nurses_number): population.append(individual) iterator += 1 else: print('Invalid Input') Ga.generic_algorithm_solution( nurses_number=nurses_number, population=population, population_size=population_size, amount_of_generations=amount_of_generations, mutation_probability=mutation_probability, elitism_percentage=elitism_percentage) elif option == 3: print('Main Documentation') help(main) print('-------------------------------------------------------') print('Simulated Annealing Documentation') help(Sa.simulated_annealing_solution) print('-------------------------------------------------------') print('Genetic Algorithm Documentation') help(Ga.generic_algorithm_solution) print('-------------------------------------------------------') print('Auxiliary Documentation') help(Au.neighbors_generator) print('-------') help(Au.random_generator) print('-------') help(Au.fitness_function) print('-------') help(Au.selection) print('-------') help(Au.crossover) print('-------') help(Au.chromosome_mutation) print('-------') help(Au.check_chromosome_composition) print('-------') help(Au.calculate_weights) print('-------') help(Au.print_output_simulated_annealing) print('-------') help(Au.print_output_genetic_algorithm) print('-------------------------------------------------------') elif option == 4: print( '\n\nThis program was developed as a work proposal for the \n' 'Artificial Intelligence discipline of the Computer Science course \n' 'at the Federal University of Ceará, Quixadá campus.\n' 'The authors of the program are the following students: \n' 'Wallesson Cavalcante and Wesley Pedro. We intend to present \n' 'solutions to the problem of nurse allocation in their respective \n' 'shifts using the Simulated Annealing and Genetic Algorithm \n' 'with some restrictions or extra add-ons.\n\n') elif option == 5: option = 0 else: print('Invalid option')
plt.xlabel(r'Distance $(m)$') plt.ylabel(r'Concentration $ \frac{kg}{m} $') plt.pause(3) plt.close(1) # ============================================================================== # Entering the time loop # ============================================================================== for t in range(1, npt + 1): # Generating analytical solution Ca = AN.difuana(M, L, Dx, x, xo, T0 + t * dT) # Explicit internal part C1 = C + dT * AUX.FDev_sp(C, Dx, dx) # Imposing boundary conditions C1[0] = Ca[0] C1[N - 1] = Ca[N - 1] # Estimating error err = np.abs(C1 - Ca) ert[t] = np.linalg.norm(err) # Plotting numerical solution and comparison with analytical plt.clf() plt.subplot(2, 2, 1) plt.plot(x, C1, 'b') plt.xlim([X0, XL])
def simulated_annealing_solution(current_state: str = None, nurses_number: int = 10, temperature: int = 350): """ Function used to solve the problem of nurse shifts using Simulated Annealing :param current_state: Initial state to use in the problem :param nurses_number: Number of nurses :param temperature: Initial temperature to solve the problem :return: void """ # Initializing current state if it wasn't defined by the user if current_state is None: current_state = Au.random_generator(nurses_number=nurses_number) # Repeat until temperature is set to zero while temperature > 0: # Used to determinate if the state was chose to the next epoch chose = False # Violation count to the current state violation_count_current_state = Au.fitness_function(individual=current_state, nurses_number=nurses_number) # Verify if the objective state was found if violation_count_current_state == 0: Au.print_output_simulated_annealing(state=current_state, violations=0, title='Best state found', nurses_number=nurses_number, chose=chose, temperature=temperature) return # Get the next neighbor state randomly next_state = Au.neighbors_generator(state=current_state, nurses_number=nurses_number) # Violation count to the neighbor state violation_count_next_state = Au.fitness_function(individual=next_state, nurses_number=nurses_number) # Calculate the variation of violation between the current state and the generated state violation_count_variation = violation_count_current_state - violation_count_next_state # Conditions to choose the next state if violation_count_variation > 0: current_state = next_state violation_count_current_state = violation_count_next_state chose = True # Using the calculation below, the generated states always had a greater number of constraints violated. # elif (e**(violation_count_variation/temperature)) > uniform(0, 1): # current_state = next_state else: n = randint(0, 100) if n < math.exp(violation_count_variation/temperature): current_state = next_state violation_count_current_state = violation_count_next_state chose = True Au.print_output_simulated_annealing(state=current_state, violations=violation_count_current_state, nurses_number=nurses_number, chose=chose, temperature=temperature) temperature -= 1 Au.print_output_simulated_annealing(state=current_state, violations=violation_count_current_state, title='Last state found', nurses_number=nurses_number, chose=chose, temperature=temperature)