def __init__(self): super(parentWindow, self).__init__() self.setupUi(self) self.__sensorCommuncate = Communcate() self.__sensorCommuncate.set_modbus_config(0x01, 'rtu', 0x03) self.__dataHolder = DataHolder("modbus sensor") self.__db_helper = DataBaseHelper() self.graph = DrawGraph() self.__timer = QTimer(self) self.__timer.timeout.connect(self.single) self.Open_pushButton.clicked.connect(self.port_open) self.Close_pushButton.clicked.connect(self.port_close) self.Signal_pushButton.clicked.connect(self.single) self.Circle_pushButton.clicked.connect(self.circle) self.Stop_pushButton.clicked.connect(self.stop_read)
def main(): parser = argparse.ArgumentParser(description='Genetic programming.') parser.add_argument('-question', help='Question number', type=int, required=True) parser.add_argument('-n', type=int) parser.add_argument('-m', type=int) parser.add_argument('-x', type=str) parser.add_argument('-expr', type=str) parser.add_argument('-data', type=str) parser.add_argument('-lambda', type=int) parser.add_argument('-time_budget', type=int) args = parser.parse_args() question = args.question if question == 1: x = [float(i) for i in args.x.split(' ')] expr = spd.loads(args.expr) tree_exp = TreeExpression().from_s_expression(expr) print(tree_exp.evaluate_expression(x)) elif question == 2: data_holder = DataHolder(args.n, args.m) data_holder.load_data(args.data) expr = spd.loads(args.expr) tree_exp = TreeExpression().from_s_expression(expr) print(data_holder.evaluate_expression(tree_exp)) elif question == 3: lmbd = int(get_argument_value('-lambda', sys.argv[1:])) data_holder = DataHolder(args.n, args.m) data_holder.load_data(args.data) ga = GeneticAlgorithm(lmbd=lmbd, n=args.n, m=args.m, k=5, chi=0.3, max_height=5, time_budget=args.time_budget, data=data_holder) # print(ga.run_ga().fitness) print(ga.run_ga().to_s_expression())
class parentWindow(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self): super(parentWindow, self).__init__() self.setupUi(self) self.__sensorCommuncate = Communcate() self.__sensorCommuncate.set_modbus_config(0x01, 'rtu', 0x03) self.__dataHolder = DataHolder("modbus sensor") self.__db_helper = DataBaseHelper() self.graph = DrawGraph() self.__timer = QTimer(self) self.__timer.timeout.connect(self.single) self.Open_pushButton.clicked.connect(self.port_open) self.Close_pushButton.clicked.connect(self.port_close) self.Signal_pushButton.clicked.connect(self.single) self.Circle_pushButton.clicked.connect(self.circle) self.Stop_pushButton.clicked.connect(self.stop_read) def port_open(self): if self.__sensorCommuncate.open_port(): self.Show_label.setText("Open port success!") else: self.Show_label.setText("Open port failed!") def port_close(self): self.__sensorCommuncate.close_port() self.Show_label.setText("Close port success") def single(self): data = self.__sensorCommuncate.request_data() self.__dataHolder.set_data(data) self.__db_helper.insert_data("humiture", self.__dataHolder) self.__show_data() def circle(self): self.__timer.start(15000) def stop_read(self): self.__sensorCommuncate.pause() self.__timer.stop() self.__clear_data() def __clear_data(self): self.illumation_lineEdit.clear() self.Temp_lineEdit.clear() self.Humidity_lineEdit.clear() self.Airspped_lineEdit.clear() def __show_data(self): self.illumation_lineEdit.setText(str(self.__dataHolder.illuminance())) self.Temp_lineEdit.setText(str(self.__dataHolder.temperature())) self.Humidity_lineEdit.setText(str(self.__dataHolder.humidity())) self.Airspped_lineEdit.setText(str(self.__dataHolder.windspeed()))
import numpy as np from data_holder import DataHolder holder = DataHolder(11, 350) holder.load_data('data.txt') np_x = np.array(holder.x) np_y = np.array(holder.y) X = np_x # transpose so input vectors are along the rows X = np.c_[X, np.ones(X.shape[0])] # add bias term beta_hat = np.linalg.lstsq(X, np_y)[0] y_hat = np.dot(X, beta_hat) sqerr = (np_y - y_hat) ** 2 print(sqerr.sum() / 350)
# ind = TreeExpression().random_init(5) # ind2 = TreeExpression().random_init(5) # offspring = ga.crossover(ind, ind2) # print(data.evaluate_expression(offspring)) # ga.generate_population() # for i in range(10): # print(ga.population[i].fitness) # print(ga.best_individual().fitness) # for i in range(10): # print(ga.selection().fitness) # start_time = time.time() import matplotlib.pyplot as plt data = DataHolder(1, 300) data.load_data('data/data.txt') ga = GeneticAlgorithm(lmbd=1000, n=2, m=3, k=2, chi=0.2, max_height=5, time_budget=1, data=data) start_time = time.time() ind = ga.run_ga() print('Elapsed time: {}'.format(time.time() - start_time)) # val = [ind.evaluate_expression(x) for x in data.x]
def main(): from data_holder import DataHolder, DataClass from synthetic_samples import CircleUniformVarianceDataGenerator, CheckersDataGenerator from synthetic_data.sphereSyntheticGenerator import SphereSyntheticGenerator max_depth = 6 repeats = 10 datas = [] with Timer('Generating random data'): for _ in range(repeats): #sampleDataGenerator = CheckersDataGenerator(squares=3, noise=0.6) #sampleDataGenerator = CircleUniformVarianceDataGenerator(radiusThreshold=0.3, dimensions=2, noise=0.1) #data = DataHolder(sampleDataGenerator.generate(50)) #sampleDataGenerator = CircleUniformVarianceDataGenerator(radiusThreshold=1.6, dimensions=30, noise=0.05) #sampleDataGenerator = CircleUniformVarianceDataGenerator(radiusThreshold=2.4, dimensions=30, noise=0.1,noiseDimensionModulator=(1, 5)) sampleDataGenerator = SphereSyntheticGenerator( ratio=1.2, dimensions=50, noise=0.005, noiseDimensionModulator=(1, 10)) sampleData = sampleDataGenerator.generate(10000) print('sampledata ratio: {}'.format(sampleData['class'].sum() / sampleData['class'].count())) sampleData = discretizeDf(sampleData, classColumn='class') data = DataHolder(sampleData, classColumn='class') #data = DataHolder( discretizeDf(sampleData), classColumn='class' ) datas.append(data) for cutSelector in [ # BestCutSelector(ChiSquaredCalculator), # BestCutSelector(GiniCutterCalculator), RandomProportional(ChiSquaredCalculator), TopN(ChiSquaredCalculator, 3), ]: print('==========={}[{}]============'.format( cutSelector.__class__.__name__, cutSelector._cutScoreCalculator.__name__)) combinedMetrics = MetricsAggregator() for data in datas: with Timer('Fitting tree'): tree = Tree(max_depth=max_depth, cut_selector=cutSelector) tree.fit(*data.train.asTuples()) with Timer('Predicting'): preds, metrics = tree.predict(*data.test.asTuples()) print(metrics) combinedMetrics += metrics # with Timer('Visualizing'): # HeatmapVisualizer.plot( # tree=tree, # df=data.train.asSingleDf(), # xLimits=(-0.5,0.5), # yLimits=(-0.5,0.5), # ) print('=== AVG:=== {}'.format(combinedMetrics)) print('done!')