def read_problem(file): global numSites global numDemands global sites try: if (file[-3:].lower() == "dat"): sites = readDataFiles.readDat(file) elif (file[-3:].lower() == "tsp"): sites = readDataFiles.readTSP(file) except IOError: print('Error reading file') raise numSites = sites.shape[0] numDemands = numSites plot.plotData(sites) print('%d locations' % numSites) print('Finished Reading File!')
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from plot import plotData from data import loadData import GradientDescent as gd import numpy as np data = loadData('../data/ex1data1.txt') (m, features) = data.shape X = data[:, 0] y = data[:, 1] # Plotting the data plotData(X, y) plt.show() # Gradient Descent print("Running Gradient Descent ...") X = np.append(np.ones((m, 1)), X, 1) # add a column of ones to X theta = np.zeros((2, 1)) # initialize fitting parameters # some gradient descent settings iterations = 1500 alpha = 0.01 # compute and display initial cost (expected: 32.0727338775) cost = gd.computeCost(X, y, theta) print("initial cost: ", cost)
from request import getData from plot import plotData #Poznan, Polanka getData(6531) #Cordoba getData(8461) #Liverpool getData(3187) #Nijmegen getData(6477) #Toulouse getData(3835) #Hanover getData(6205) #Gent getData(8904) plotData()
def geneticAlgorithm(pop, iter_max, pcross, corss_type, pmut, mut_type, save_plot=False, file_name="figure"): count = 0 print(f"Starting generation {count}! Initial population is {pop}") print(f"Function parameters are:\n\titer_max = {iter_max}" # f"\n\tpsel = {psel}" f"\n\tpcross = {pcross}" f"\n\tcorss_type = {corss_type}" f"\n\tpmut = {pmut}" f"\n\tmut_type = {mut_type}") # Generate initial population population = generateNotSoRandomPopulation(pop) # check hard constraints hard_constraint_check = checkHardConstraints(population) population = population[np.argwhere( hard_constraint_check == True).flatten()] print( f"Count of initial population going to the next generation: {np.count_nonzero(hard_constraint_check)}" ) # Fitness calculation for every chromosome. penalties = penaltyFunction(population) fitness = fitnessFunction(population, penalties) meanPenaltiesList = [penalties.mean()] elitePenaltiesList = [penalties.min()] print( f"Average penalty = {meanPenaltiesList[-1]:.3f}," f" Min penalty = {penalties.min():.3f}, Max penalty = {penalties.max():.3f}" ) print( f"Min fitness = {fitness.min():.3f}, Max fitness = {fitness.max():.3f}" ) while True: # Generate next generation count += 1 print(f"---------- Moving to generation {count} ----------") print("Selecting...") population = selectWorthyChromosomes(population, fitness) print("Crossing over...") population = crossover(population, corss_type, pcross) print("Mutating ...") population = mutate(population, mut_type, pmut, .33) hard_constraint_check = checkHardConstraints(population) population = population[np.argwhere( hard_constraint_check == True).flatten()] print( f"Count of population that survived: {np.count_nonzero(hard_constraint_check)}" ) penalties = penaltyFunction(population) fitness = fitnessFunction(population, penalties) meanPenaltiesList.append(penalties.mean()) elitePenaltiesList.append(penalties.min()) print( f"Average penalty = {meanPenaltiesList[-1]:.3f}," f" Min penalty = {penalties.min():.3f}, Max penalty = {penalties.max():.3f}" ) print( f"Min fitness = {fitness.min():.3f}, Max fitness = {fitness.max():.3f}" ) if terminationCriteria(count, iter_max, meanPenaltiesList, np.count_nonzero(hard_constraint_check)): # Finished is true! plotData( count, meanPenaltiesList, elitePenaltiesList, f"Results with pop={pop} for pcorss={pcross}," f" corss_type={corss_type}, pmut={pmut}, mut_type={mut_type}", save_plot, file_name) break return meanPenaltiesList, elitePenaltiesList
sim.setVoltage(24.0) #V sim.setLoadTorque(0.0) #Nm sim.setLoadInertia(0.0) #kg*m^2 sim.position_controller_inputs['setpoint'] = 0.0 sim.PositionController.Kp = 20.0 sim.PositionController.Ki = 0.0 sim.VelocityController.Kp = 2.0 sim.VelocityController.Ki = 1.0 sim.CurrentController.PID_id.Kp = 2.0 sim.CurrentController.PID_iq.Kp = 2.0 sim.CurrentController.PID_id.Ki = 1.0 sim.CurrentController.PID_iq.Ki = 1.0 TIME = 1.0 #sec AMPLITUDE = np.pi / 4.0 #rad FREQUNCY = 5.0 #Hz def call_back(t, dt): sim.position_controller_inputs['setpoint'] = np.sin(2 * np.pi * t * FREQUNCY) * AMPLITUDE #sim.position_controller_inputs['setpoint'] = signal.square(2 * np.pi * t * 1.0) * AMPLITUDE result = sim.simpleAnalysis(TIME, call_back) plot.plotData(result)
import matplotlib.pyplot as plt import plot ## =============== Part 1: Loading and Visualizing Data ================ # We start the exercise by first loading and visualizing the dataset. # The following code will load the dataset into your environment and plot the data. print('Loading and Visualizing Data ...') # Load from ex6data1,You will have X, y in your environment: data = sco.loadmat('ex6data1.mat') X,y = data["X"],data['y'] print(np.shape(X),np.shape(y)) # Plot training data f1 = plot.plotData(X,y) f1.show() input('Program paused. Press enter to continue.') ## ==================== Part 2: Training Linear SVM ==================== # The following code will train a linear SVM on the dataset and plot the decision boundary learned. # Load from ex6data1. You will have X, y in your environment: data1 = sco.loadmat('ex6data1.mat') # fprintf('\nTraining Linear SVM ...\n') # # % You should try to change the C value below and see how the decision # % boundary varies (e.g., try C = 1000) # C = 1; # model = svmTrain(X, y, C, @linearKernel, 1e-3, 20);