#Cilj je narest probram k bo ustvaril n tock in zracunal koliko je najvecje stavilo k presecisc ene premice vsemi daljicami.
from random import randint
import math
import time
seconds = time.time()
print("Zacetek programa: ", seconds)
###############################
#Metoda je klass kjer je napisan algoritem za izracunavo c
class Metoda :
#stevilo_pik _ default
n=5
#sirina_okvirja _ default
sirina=100
############################
# postavimo vse parametre
polje= [] #polje tock
tocke=[] #arraj za permutacijo daljic
daljice=[] #trenutna permutacija na kateri racunam
ar_daljice=[] #vse mozne permutacije daljic
############################
#postavi random tocko v polje z sirino width
def random_tocka(self,width, krog=False):
if krog==True:
return self.random_tocka_v_krogu(width)
i = randint(0,width)
j = randint(0,width)
return i,j
############################
#postavi random tocko v krog z polmerom width/2
def random_tocka_v_krogu(self,width):
i = randint(0,width)
j = randint(0,width)
w = width / 2
ps = math.sqrt(j*j + (w-i)*(w-i))
while (ps > w):
j = randint(0,width)
ps = math.sqrt(j*j + (w-i)*(w-i))
return i,j
############################
# to tocko zapisemo v array
def dodaj_tocko(self,i,j):
self.polje.append([i,j])
def pobrisi_polje(self):
self.polje = []
#print(self.polje)
def izpisi_polje (self):
#print(self.polje)
pass
############################
# Z pomocjo zgornih funkcij zapise v arraj 2n tock
def postavi_polje(self,stevilo_pik= 5, sirina_okvirja=100,krog=False): ## tu dodaj parametre
self.pobrisi_polje()
stevilo_pik *= 2
for _ in range(stevilo_pik) :
#self.izpisi_polje()
i,j = self.random_tocka(sirina_okvirja,krog)
while [i,j] in Metoda.polje :
i,j = self.random_tocka(sirina_okvirja,krog)
self.dodaj_tocko(i,j)
#self.izpisi_polje()
############################
#Matriko naredi za presek premice in daljice
#vzemi 2 točke na premici in eno na daljici
#vzemi 2 tocke na premico in eno na dalciji (tadrugo)
#rabita ime nasprotni prednak
def seka(self,l,d):
#print (l)
x = l[0]
y = l[1]
z1 = d[0]
z2 = d[1]
#print(self.polje[x][0])
#print(self.polje[x][1])
#print()
#print()
D1=matrix(SR, 3, [self.polje[x][0],self.polje[x][1],1,self.polje[y][0],self.polje[y][1],1,self.polje[z1][0],self.polje[z1][1],1])
D2=matrix(SR, 3, [self.polje[x][0],self.polje[x][1],1,self.polje[y][0],self.polje[y][1],1,self.polje[z2][0],self.polje[z2][1],1])
#print(D1)
#print(D1.det())
#print(D2)
#print(D2.det())
if D2.det() * D1.det() < 0 :
return true
else:
return false
def ustvari_daljice_v2 (self,n):
self.daljice = [Set([i,j]) for i in range(2*n) for j in range(i+1, 2*n)]
def program (self,n=5):
p = MixedIntegerLinearProgram(maximization = False)
x=p.new_variable(binary=True)
p.set_objective(p["t"])
for i in range(2*n):
p.add_constraint(sum(x[Set([i,j])] for j in range(2*n) if i != j) == 1)
for l in self.daljice :
p.add_constraint(sum(x[d] for d in self.daljice if self.seka(l,d)) <= p["t"] )
# ({3,4}, set([5,6]))
#print (p.get_values(x)) # vrne vrednost x a)
a = p.solve()
#print (a) # vrne vrednost t ja
return a
Zacetek programa: 1641741235.670977
seconds = time.time()
nn=3
for i in range(2,nn):
for j in range(3*nn-2*i):
a = Metoda ()
a.postavi_polje(i)
a.ustvari_daljice_v2(i)
a.seka(a.daljice[0],a.daljice[1])
b=a.program(i)
seconds1 = time.time()
print (i)
#print (b/sqrt(i))
#print (seconds1-seconds)
2 2 2 2 2
import time
seconds = time.time()
st=2
print(st)
nn=20
for i in range(st,nn):
ar=[]
cas=0
for j in range(3):
seconds = time.time()
a = Metoda ()
a.postavi_polje(i)
a.ustvari_daljice_v2(i)
a.seka(a.daljice[0],a.daljice[1])
b=a.program(i)
ar.append(b)
seconds1 = time.time()
cas += seconds1-seconds
mm= max(ar)
print (str(i), "-daljic",str( "presecisc "),str( mm )," C: ", str(float(mm/sqrt(i))), "cas: ", cas/ (3))
2 2 -daljic presecisc 1.0 C: 0.7071067811865476 cas: 0.009653806686401367 3 -daljic presecisc 1.0 C: 0.5773502691896257 cas: 0.09989817937215169 4 -daljic presecisc 1.0 C: 0.5 cas: 0.3965009053548177 5 -daljic presecisc 1.0 C: 0.447213595499958 cas: 0.8445250193277994 6 -daljic presecisc 1.0 C: 0.40824829046386296 cas: 2.0516225496927896 7 -daljic presecisc 1.0000000000000002 C: 0.3779644730092273 cas: 3.6779468059539795 8 -daljic presecisc 2.0000000000000004 C: 0.7071067811865477 cas: 6.533250570297241 9 -daljic presecisc 2.0 C: 0.6666666666666666 cas: 10.39283021291097 10 -daljic presecisc 2.0 C: 0.632455532033676 cas: 17.5063575108846 11 -daljic presecisc 2.0 C: 0.6030226891555273 cas: 26.421429951985676 12 -daljic presecisc 2.0 C: 0.5773502691896257 cas: 38.47291326522827 13 -daljic presecisc 2.0 C: 0.5547001962252291 cas: 54.642125844955444 14 -daljic presecisc 2.0 C: 0.5345224838248487 cas: 75.73027062416077
import json
nn=30
with open("poganjanje-%d.json" % nn) as f:
for i, mm, cas in json.load(f):
print (str(i), "-daljic",str( "presecisc "),str( mm )," C: ", str(float(mm/sqrt(i))), "cas: ", cas/ (3))
2 -daljic presecisc 1.0 C: 0.7071067811865476 cas: 0.5147643884023031 3 -daljic presecisc 1.0 C: 0.5773502691896257 cas: 0.06637001037597656 4 -daljic presecisc 1.0 C: 0.5 cas: 0.18192736307779947 5 -daljic presecisc 1.0 C: 0.447213595499958 cas: 0.41359424591064453 6 -daljic presecisc 1.0 C: 0.40824829046386296 cas: 0.9518889586130778 7 -daljic presecisc 2.0 C: 0.7559289460184544 cas: 1.466227928797404 8 -daljic presecisc 2.0 C: 0.7071067811865476 cas: 2.807890017827352 9 -daljic presecisc 2.0 C: 0.6666666666666666 cas: 4.203701098759969 10 -daljic presecisc 2.0 C: 0.632455532033676 cas: 6.503405650456746 11 -daljic presecisc 2.0 C: 0.6030226891555273 cas: 10.20706303914388 12 -daljic presecisc 2.0 C: 0.5773502691896257 cas: 15.100358645121256 13 -daljic presecisc 2.0 C: 0.5547001962252291 cas: 21.550816774368286 14 -daljic presecisc 2.0 C: 0.5345224838248487 cas: 33.613250970840454 15 -daljic presecisc 2.0 C: 0.5163977794943223 cas: 60.449558893839516 16 -daljic presecisc 2.0 C: 0.5 cas: 78.31560269991557 17 -daljic presecisc 2.0 C: 0.48507125007266594 cas: 83.93041729927063 18 -daljic presecisc 3.0 C: 0.7071067811865476 cas: 6550.640018622081 19 -daljic presecisc 3.0 C: 0.6882472016116853 cas: 12268.013555606207 20 -daljic presecisc 3.0 C: 0.670820393249937 cas: 10521.755459070206