diff --git a/.idea/workspace.xml b/.idea/workspace.xml index b6a7aa0..2c23d57 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,10 +2,9 @@ - - + + - - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - + + - - - - - + + @@ -94,14 +54,11 @@ - - + + - - - - - + + @@ -119,7 +76,7 @@ @@ -135,6 +92,10 @@ @@ -181,8 +142,8 @@ - - + + - + - + - + - + - - - - + + + + + + + + - - - - @@ -384,7 +345,56 @@ @@ -392,14 +402,14 @@ - + - + - + @@ -427,7 +437,14 @@ - @@ -441,9 +458,6 @@ - - - @@ -451,9 +465,6 @@ - - - @@ -471,9 +482,6 @@ - - - @@ -481,9 +489,6 @@ - - - @@ -498,9 +503,6 @@ - - - @@ -508,9 +510,6 @@ - - - @@ -528,9 +527,6 @@ - - - @@ -538,9 +534,6 @@ - - - @@ -555,9 +548,6 @@ - - - @@ -575,9 +565,6 @@ - - - @@ -585,9 +572,6 @@ - - - @@ -602,9 +586,6 @@ - - - @@ -622,9 +603,6 @@ - - - @@ -632,9 +610,6 @@ - - - @@ -645,9 +620,6 @@ - - - @@ -665,9 +637,6 @@ - - - @@ -688,9 +657,6 @@ - - - @@ -708,9 +674,6 @@ - - - @@ -731,23 +694,24 @@ - + - - - - - + + - + - - - + + + + + + + @@ -758,52 +722,64 @@ - + - - - - - + + + + + + + + + - - - - - + + - + - - - - - + + - + - - + + - + + + + + + + + - - + + - + + + + + + + + \ No newline at end of file diff --git a/asia.py b/asia.py new file mode 100644 index 0000000..0c7ce8f --- /dev/null +++ b/asia.py @@ -0,0 +1,5 @@ +imie = input("Podaj imie: ") +if imie.upper() == "Asia".upper(): + print(imie+" zdrwiej!") +else: + print("Spierdalaj!") \ No newline at end of file diff --git a/genAl.py b/genAl.py new file mode 100644 index 0000000..30144af --- /dev/null +++ b/genAl.py @@ -0,0 +1,71 @@ +import random +#populacje +pBadana = [] #populacja badana +wyniki = [] + +#sprawdzanie dopasowania +def sprDop(x): + #print(x) + if x <1 or x >127: + del pBadana[pBadana.index(x)] + return 0 + return 2 * (pow(x,2) + 1) + +#generator ppulacji +def genP(x): + for i in range(0,x): + pBadana.append(random.randint(1,127)) + pBadana.sort() + +#mutacja +def mutant(x): + mutantGen = 1<< random.randint(0,7) #1 przesuwamy bitowo, losowa pomiedzy najstarszym a najmlodszym bitem + #print("Mutant gen: " + str(mutantGen)) + return x^mutantGen #exclusive or + +#krzyzowanie +def cross(x,y): + locus = 7 - random.randint(0,7) + #tworzenie maski bitowej d przneoseznia loculusa + bMask = 0 + for i in range(0,locus): + bMask = bMask | 1<< i + #wyluskanie elemntu do zmiany + tempX = x & bMask + tempY = y & bMask + negBMask = ~bMask + headX = x & negBMask + headY = y & negBMask + + sumX = headX | tempY + sumY = headY | tempX + pBadana[pBadana.index(x)] = sumX + pBadana[pBadana.index(y)] = sumY + +#sterwanie mutacjami i kombinacjami +def mutSteer(): + for nbs, p in enumerate(pBadana): + pBadana[nbs] = mutant(p) + for p in pBadana: + cross(p, pBadana[random.randint(0,len(pBadana)-1)]) + +#let's start +def engine(): + genP(5) + resultFlag = False + genNo = 0 + while not resultFlag: + print("Generacja: " + str(genNo)) + genP(10 - len(pBadana)) + for probka in pBadana: + tempW = sprDop(probka) + if tempW == 32260: #najwyzsze mozliwe dopasowanie + resultFlag = True + print(str(probka) + ": " + str(tempW)) + del pBadana[0:5] + mutSteer() #mutujemy tylko te które zostały w selekcji + print("-------------------------") + genNo +=1 + +engine() +print(sprDop(127)) \ No newline at end of file diff --git a/perceptron.py b/perceptron.py new file mode 100644 index 0000000..f80aae8 --- /dev/null +++ b/perceptron.py @@ -0,0 +1,84 @@ +class WspPR: + x=0 + y=0 + s=0 + def __init__(self,x,y,s): + self.x = x + self.y = y + self.s = s + +def predictins(obj, weights): + activat = 0 + activat += (obj.x * weights[1]) + (obj.y * weights[2]) + weights[0] + return 1 if activat >=0 else 0 + +def trening(obj, learningRate,epoch): + weigh = [0.0,0.0,0.0] + for epochNumb in range(epoch): + errorSummary = 0.0 + for objs in obj: + predictio = predictins(objs,weigh) + error = objs.s - predictio + errorSummary = error ** 2 + weigh[0] = weigh[0] + learningRate * error + #print(weigh[0] + learningRate * error) + weigh[1] = weigh[1] + learningRate * error * objs.x + weigh[2] = weigh[2] + learningRate * error * objs.y + #print(": ".join([str(epochNumb),str(learningRate),str(errorSummary)])) + #print(weigh) + return weigh + +weights = [1,2,-1] +wspNauczyciel = [] +wspTest = [] + +wspNauczyciel.append(WspPR(2,7,0)) +wspNauczyciel.append(WspPR(3,3,1)) +wspNauczyciel.append(WspPR(3,12,0)) +wspNauczyciel.append(WspPR(100,250,0)) +wspNauczyciel.append(WspPR(19,50,0)) +wspNauczyciel.append(WspPR(10, -10, 1)) +wspNauczyciel.append(WspPR(5, 0, 1)) +wspNauczyciel.append(WspPR(100, 100, 1)) +wspNauczyciel.append(WspPR(12, 20, 1)) +wspNauczyciel.append(WspPR(-1,3,0)) +wspNauczyciel.append(WspPR(-6,-10,0)) +wspNauczyciel.append(WspPR(-3,0,0)) +wspNauczyciel.append(WspPR(10,101,0)) +wspNauczyciel.append(WspPR(10,23,0)) + +wspTest.append(WspPR(-2,5,0)) +wspTest.append(WspPR(10,3,1)) +wspTest.append(WspPR(12,12,1)) +wspTest.append(WspPR(200,250,1)) +wspTest.append(WspPR(19,5,1)) +wspTest.append(WspPR(30, -10, 1)) +wspTest.append(WspPR(5, 4, 1)) +wspTest.append(WspPR(100,-100, 1)) +wspTest.append(WspPR(12, 50, 0)) +wspTest.append(WspPR(-1,-9,1)) +wspTest.append(WspPR(-6,10,0)) +wspTest.append(WspPR(-3,-10,1)) +wspTest.append(WspPR(1001,101,1)) +wspTest.append(WspPR(100,203,0)) + + +for objs in wspNauczyciel: + wynik = predictins(objs,weights) + if wynik == objs.s: + result = "PASS" + else: + result = "FAIL" + print(" ".join(["Wspolrzedne:",str(objs.x),str(objs.y),"oczekiwany",str(objs.s),"otrzymany",str(wynik),result])) + +weights = trening(wspNauczyciel,0.1,10) +print(weights) +print("--") +for objs in wspTest: + wynik = predictins(objs, weights) + if wynik == objs.s: + result = "PASS" + else: + result = "FAIL" + print(" ".join( + ["Wspolrzedne:", str(objs.x), str(objs.y), "oczekiwany", str(objs.s), "otrzymany", str(wynik), result])) \ No newline at end of file diff --git a/szukaj.py b/szukaj.py index 9c65867..3238bfc 100644 --- a/szukaj.py +++ b/szukaj.py @@ -1,12 +1,15 @@ import random +import math as m +import matplotlib.pyplot as plt #globals punkty = [] +wynik = [] #miasta class miasta: nrMiasta = 0 - wspX = 0 - wspY = 0 + wspX = 0.0 + wspY = 0.0 def __init__(self,nr,wsx,wsy): self.nrMiasta = nr @@ -21,6 +24,49 @@ def generatorMiast(n): for i in range (1,n+1): punkty.append(miasta(i,random.randint(1,100),random.randint(1,100))) -generatorMiast(3) +def tabCr(): + for o in punkty: + for p in punkty: + if o.nrMiasta == p.nrMiasta: + pass + else: + dist = m.sqrt(pow(m.fabs(p.wspY - o.wspY),2)+pow(m.fabs(p.wspX - p.wspX),2)) + print("Nr: " + str(o.nrMiasta) +" to Nr: "+ str(p.nrMiasta)+ ", DIST: "+str(dist)) +def plotCr(pkts): + plt.title("Punkty do odwiedzenia") + plt.xlabel("X") + plt.ylabel("Y") + X = [] + Y = [] + for o in pkts: + X.append(o.wspX) + Y.append(o.wspY) + plt.annotate(str(o.nrMiasta),(o.wspX,o.wspY)) + plt.plot(X,Y) + #plt.scatter(X,Y) + plt.grid() + plt.show() + +def engine(czyPierwszy): + if czyPierwszy: + wynik.append(punkty[0]) + del punkty[0] + distTmp = [] + for o in punkty: + distTmp.append(m.sqrt(m.pow((o.wspY - wynik[len(wynik) -1].wspY),2) + m.pow((o.wspX - wynik[len(wynik) -1].wspX),2))) + tmpMin = min(distTmp) + wynik.append(punkty[distTmp.index(tmpMin)]) + del punkty[distTmp.index(tmpMin)] + if punkty: + engine(False) + + +generatorMiast(15) for o in punkty: - o.allPrt() \ No newline at end of file + o.allPrt() +#tabCr() +engine(True) +for o in wynik: + o.allPrt() +plotCr(wynik) +