def fuzzy_side(n: int, p: list, w: list, c: list, m: int, best: float, worst: float, avg: float, pop: list, fc: FuzzyController): fs = set([ks.fitness(pop[i], p, w, c, m, n) for i in range(100)]) pop_size = 100 '''Determinarea T1, T2, T3''' t1 = len(fs) / pop_size t2 = (best - avg) / best sorted_pop = sorted(pop, key=lambda x: ks.fitness(x, p, w, c, m, n), reverse=True) best_sol = sorted_pop[0] worst_sol = sorted_pop[-1] t3 = hamming_distance(best_sol, worst_sol, n) / n rc, pc, rm, pm = fc.apply_rules(t1, t2, t3) if rc == "low": op_c = "2-point" elif rc == "medium": op_c = random.choice(["k-point", "uniform"]) else: op_c = random.choice(["segregation", "inversion"]) if rm == "low": op_m = random.choice(["interchanging", "revers"]) elif rc == "medium": op_m = "binary_m" else: op_m = "parity" return op_c, pc, op_m, pm
def fga(n: int, p: list, w: list, c: list, m: int): pop = initialize_pop(n, p, w, c, m) best, worst, avg = evaluate_pop(pop, n, p, w, c, m) g = 0 last_improvement = g max_generations = 10**6 # numarul maxim de generatii t = time.time() fc = FuzzyController(n) while (g - last_improvement < 100) and (g < max_generations) and (time.time() - t <= 250): op_c, pc, op_m, pm = fuzzy_side(n, p, w, c, m, best, worst, avg, pop, fc) # print(op_c, " ", op_m) crossover_count = int((100 * pc) // 2) children = crossover(n, m, pop, crossover_count, p, w, c, g, op_c) mutants = mutation(children, n, pm, op_m) pop = nex_generation(mutants, pop, p, w, c, m, n, g).copy() new_best, worst, avg = evaluate_pop(pop, n, p, w, c, m) if new_best > best: last_improvement = g best = new_best g += 1 return best
def main(args): global capture global hmax, hmin global stats, startTime highgui.cvNamedWindow('Camera', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Red Hue', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Green Hue', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Value', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Red Laser', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Green Laser', highgui.CV_WINDOW_AUTOSIZE) highgui.cvMoveWindow('Camera', 0, 10) highgui.cvMoveWindow('Value', 10, 420) highgui.cvMoveWindow('Red Laser', 360, 10) highgui.cvMoveWindow('Green Laser', 360, 360) highgui.cvMoveWindow('Red Hue',700, 10 ) highgui.cvMoveWindow('Green Hue',700, 420) highgui.cvCreateTrackbar("Brightness Trackbar","Camera",0,255, change_brightness); highgui.cvCreateTrackbar("vmin Trackbar","Value",vmin,255, change_vmin); highgui.cvCreateTrackbar("vmax Trackbar","Value",vmax,255, change_vmax); highgui.cvCreateTrackbar("red hmin Trackbar","Red Hue",red_hmin,180, change_red_hmin); highgui.cvCreateTrackbar("red hmax Trackbar","Red Hue",red_hmax,180, change_red_hmax); highgui.cvCreateTrackbar("green hmin Trackbar","Green Hue",green_hmin,180, change_green_hmin); highgui.cvCreateTrackbar("green hmax Trackbar","Green Hue",green_hmax,180, change_green_hmax); print "grabbing camera" capture = highgui.cvCreateCameraCapture(0) print "found camera" highgui.cvSetCaptureProperty(capture,highgui.CV_CAP_PROP_FRAME_WIDTH, iwidth) highgui.cvSetCaptureProperty(capture,highgui.CV_CAP_PROP_FRAME_HEIGHT, iheight) frame = highgui.cvQueryFrame(capture) frameSize = cv.cvGetSize(frame) hsv = cv.cvCreateImage(frameSize,8,3) mask = cv.cvCreateImage(frameSize,8,1) red_hue = cv.cvCreateImage(frameSize,8,1) green_hue = cv.cvCreateImage(frameSize,8,1) saturation = cv.cvCreateImage(frameSize,8,1) value = cv.cvCreateImage(frameSize,8,1) red_laser = cv.cvCreateImage(frameSize,8,1) green_laser = cv.cvCreateImage(frameSize,8,1) turret = FuzzyController(frameSize.width,frameSize.height,True) while 1: frame = highgui.cvQueryFrame(capture) cv.cvCvtColor(frame, hsv, cv.CV_BGR2HSV) cv.cvSplit(hsv,red_hue,saturation,value,None) cv.cvSplit(hsv,green_hue,saturation,value,None) cv.cvInRangeS(red_hue, cv.cvScalar(red_hmin), cv.cvScalar(red_hmax), red_hue) cv.cvInRangeS(green_hue, cv.cvScalar(green_hmin), cv.cvScalar(green_hmax), green_hue) cv.cvInRangeS(value, cv.cvScalar(vmin), cv.cvScalar(vmax), value) cv.cvAnd(red_hue, value, red_laser) cv.cvAnd(green_hue, value, green_laser) green_cenX,green_cenY = averageWhitePoints(green_laser) draw_target(frame, green_cenX, green_cenY, "GREEN") red_cenX, red_cenY = averageWhitePoints(red_laser) draw_target(frame, red_cenX, red_cenY, "RED") if(green_cenX >= 0 and green_cenY >= 0):# and move_count <= 0): turret.update(green_cenX,green_cenY) highgui.cvShowImage('Camera',frame) highgui.cvShowImage('Red Hue', red_hue) highgui.cvShowImage('Green Hue', green_hue) highgui.cvShowImage('Value',value) highgui.cvShowImage('Red Laser',red_laser) highgui.cvShowImage('Green Laser',green_laser) if stats: printRunningStats((green_cenX, green_cenY), (red_cenX, red_cenY)) k = highgui.cvWaitKey(10) if k == '\x1b' or k == 'q': sys.exit() if k == 'p': if stats: printTotalStats() stats = False else: startTime = time() stats = True
''' Created on Jan 1, 2002 @author: douglas ''' __author__ = 'gustavo' from FuzzyController import FuzzyController from PyQt4 import QtCore, QtGui import PyQt4 fuzzyControler = FuzzyController("container_crane.fcl") #print(fuzzyControler) input = { "s_comunicatividad":5, "t_receptividad":7, "e_empatia":4 } input= { 's_receptividad': 4.8672389428309346, 's_comunicatividad': 2.6292891970049093, 't_comunicatividad': 8.0503738351059724, 't_receptividad': 9.1297355325614813, 'e_empatia': 4.6655176048198079 } print fuzzyControler.calculate(input)
def main(args): global capture global hmax, hmin highgui.cvNamedWindow('Camera', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Hue', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Satuation', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Value', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Laser', highgui.CV_WINDOW_AUTOSIZE) highgui.cvMoveWindow('Camera', 0, 10) highgui.cvMoveWindow('Hue', 0, 350) highgui.cvMoveWindow('Satuation', 360, 10) highgui.cvMoveWindow('Value', 360, 350) highgui.cvMoveWindow('Laser', 700, 40) highgui.cvCreateTrackbar("Brightness Trackbar","Camera",0,255, change_brightness); highgui.cvCreateTrackbar("hmin Trackbar","Hue",hmin,180, change_hmin); highgui.cvCreateTrackbar("hmax Trackbar","Hue",hmax,180, change_hmax); highgui.cvCreateTrackbar("smin Trackbar","Satuation",smin,255, change_smin); highgui.cvCreateTrackbar("smax Trackbar","Satuation",smax,255, change_smax); highgui.cvCreateTrackbar("vmin Trackbar","Value",vmin,255, change_vmin); highgui.cvCreateTrackbar("vmax Trackbar","Value",vmax,255, change_vmax); print "grabbing camera" capture = highgui.cvCreateCameraCapture(0) print "found camera" highgui.cvSetCaptureProperty(capture,highgui.CV_CAP_PROP_FRAME_WIDTH, 320) highgui.cvSetCaptureProperty(capture,highgui.CV_CAP_PROP_FRAME_HEIGHT, 240) frame = highgui.cvQueryFrame(capture) frameSize = cv.cvGetSize(frame) hsv = cv.cvCreateImage(frameSize,8,3) mask = cv.cvCreateImage(frameSize,8,1) hue = cv.cvCreateImage(frameSize,8,1) satuation = cv.cvCreateImage(frameSize,8,1) value = cv.cvCreateImage(frameSize,8,1) laser = cv.cvCreateImage(frameSize,8,1) turret = FuzzyController(frameSize.width,frameSize.height,True) move_count = 0 while 1: frame = highgui.cvQueryFrame(capture) cv.cvCvtColor(frame, hsv, cv.CV_BGR2HSV) #cv.cvInRangeS(hsv,hsv_min,hsv_max,mask) cv.cvSplit(hsv,hue,satuation,value,None) cv.cvInRangeS(hue,cv.cvScalar(hmin),cv.cvScalar(hmax),hue) cv.cvInRangeS(satuation,cv.cvScalar(smin),cv.cvScalar(smax),satuation) cv.cvInRangeS(value,cv.cvScalar(vmin),cv.cvScalar(vmax),value) #cv.cvInRangeS(hue,0,180,hue) cv.cvAnd(hue, value, laser) #cv.cvAnd(laser, value, laser) cenX,cenY = averageWhitePoints(laser) #print cenX,cenY draw_target(frame,cenX,cenY) if(cenX != 0 and cenY != 0):# and move_count <= 0): turret.update(cenX,cenY,False) """ turret.reset() move_count = 3 if(cenX < 100): turret.left(20) elif(cenX > 200): turret.right(20) if(cenY < 80): turret.up(40) elif(cenY > 170): print "DOWN please.." turret.down(40) print cenY """ #move_count -= 1 #draw_target(frame,200,1) highgui.cvShowImage('Camera',frame) highgui.cvShowImage('Hue',hue) highgui.cvShowImage('Satuation',satuation) highgui.cvShowImage('Value',value) highgui.cvShowImage('Laser',laser) k = highgui.cvWaitKey(10) if k == 'q': sys.exit()