{ "cells": [ { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [], "source": [ "class Cilj:\n", " def __init__(self, d, r, p): # kaj imamo podano za vsak cilj: d = smer, r = cas sprostitve, p = zacetna pozicija\n", " self.d = d\n", " self.r = r\n", " self.p = p\n", " \n", " def pozicija(self, t, v): # pozicija cilja ob casu t, v = hitrost\n", " return self.p + (t - self.r) * v * self.d\n", " \n", " def __hash__(self):\n", " return hash((self.d, self.r, self.p))\n", " \n", " def __eq__(self, other): # preverimo enakost dveh ciljev\n", " return (self.d, self.r, self.p) == (other.d, other.r, other.p)\n", " \n", " def __repr__(self):\n", " return f\"Cilj({self.d}, {self.r}, {self.p})\"" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [], "source": [ "class Ureditev: # načini, kako razporedimo cilje\n", " def __init__(self, seznam, kljuc): # seznam = vsi cilji, kljuc = funkcija, ki posortira\n", " self.seznam = sorted(seznam, key=kljuc)\n", " self.indeksi = {c: i for i, c in enumerate(self.seznam)}\n", " \n", " def __len__(self): # stevilo ciljev\n", " return len(self.seznam)\n", " \n", " def __getitem__(self, index): # vrne element seznama z indeksom index\n", " return self.seznam[index] # sigma^(-1)(index)\n", " \n", " def indeks(self, cilj): # vrne indeks nekega cilja\n", " return self.indeksi[cilj] # sigma(cilj)\n", " \n", "class ObratnaUreditev: # obratni vrstni red razporeditve ciljev iz razreda Ureditev\n", " def __init__(self, ureditev):\n", " self.ureditev = ureditev\n", " \n", " def __len__(self):\n", " return len(self.ureditev)\n", " \n", " def __getitem__(self, index):\n", " if isinstance(index, slice):\n", " index = slice(None if index.start is None else -index.start-1,\n", " None if index.stop is None else -index.stop-1,\n", " -1 if index.step is None else -index.step)\n", " else:\n", " index = -index-1\n", " return self.ureditev.seznam[index]\n", " \n", " def indeks(self, cilj):\n", " return len(self) - self.ureditev.indeks(cilj) - 1" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [], "source": [ "cilji = [Cilj(*p) for p in zip([1, 1, -1, -1, -1, 1, -1], [0, 5, 13, 15, 6, 8, 18], [2, 5, -3, 4, 1, -2, -7])]" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [], "source": [ "v = 0.3\n", "IPO = Ureditev(cilji, lambda c: c.pozicija(0, v))\n", "TO = Ureditev(cilji, lambda c: (c.d, c.pozicija(0, v)))\n", "IPOc = ObratnaUreditev(IPO)\n", "TOc = ObratnaUreditev(TO)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "slice(None, None, -1)\n" ] }, { "data": { "text/plain": [ "[Cilj(-1, 15, 4),\n", " Cilj(1, 5, 5),\n", " Cilj(-1, 6, 1),\n", " Cilj(1, 0, 2),\n", " Cilj(-1, 13, -3),\n", " Cilj(-1, 18, -7),\n", " Cilj(1, 8, -2)]" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "IPOc[:0]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 1, 4, 0, 2, 6, 5]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[IPOc.indeks(c) for c in cilji]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Cilj(1, 0, 2),\n", " Cilj(1, 5, 5),\n", " Cilj(-1, 13, -3),\n", " Cilj(-1, 15, 4),\n", " Cilj(-1, 6, 1),\n", " Cilj(1, 8, -2),\n", " Cilj(-1, 18, -7)]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cilji" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Cilj(-1, 6, 1)" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "IPOc[2]" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [], "source": [ "class SLMTTSP:\n", " def __init__(self, cilji, v):\n", " IPO = Ureditev(cilji, lambda c: c.pozicija(0, v))\n", " TO = Ureditev(cilji, lambda c: (c.d, c.pozicija(0, v)))\n", " IPOc = ObratnaUreditev(IPO)\n", " TOc = ObratnaUreditev(TO)\n", " self.v = v\n", " self.cilji = cilji\n", " self.ureditve = [IPO, IPOc, TO, TOc]\n", " self.F = {} # tu so shranjena stanja (C, i) in najkrajši časi, ko do tega stanja lahko pridemo (elementi se dodajajo v metodi f)\n", " \n", " def g(self, t, j, i): # najhitrejši čas obiska cilja i, če smo nazadnje obiskali cilj j ob času t\n", " posi = i.pozicija(t, self.v)\n", " posj = 0 if j is None else j.pozicija(t, self.v)\n", " razlika = posi - posj\n", " delta = 1 if razlika > 0 else -1 # hitrost gibanja agenta (pozitivna, če je razlika med zaporednima ciljema pozitivna)\n", " tt = t + razlika / (delta - self.v * i.d) # najmanjši potreben čas obiska i, dodan k trenutnemu času t\n", " if tt >= i.r:\n", " return (tt, [(t, posj), (tt, posi)]) # s paroma znotraj [] si zapomnimo odseke trgovčeve poti (na katerih pozicijah je bil ob časih t in tt)\n", " else:\n", " return (i.r, [(t, posj), (t + abs(i.p - posj), i.p), (i.r, i.p)]) \n", " # v zadnjem primeru (srednji element seznama) agent čaka na poziciji i.p do časa i.r (v tem primeru je delta = 0)\n", " \n", " def predhodno_stanje(self, C, i):\n", " if i is None: # primer, ko ni predhodnega cilja\n", " return None\n", " return tuple(min(C[l], self.ureditve[l].indeks(i)) for l in range(4))\n", " \n", " def phi(self, C): # vrne vsa že obiskana mesta v naboru C\n", " return {j for l, m in enumerate(C) for j in self.ureditve[l][:m]} # vrne mesta, ki so bolj na začetku seznama l kot mesto m\n", " \n", " def predhodnik(self, l, C, i): # j preteče indekse vseh ureditev\n", " if i is None:\n", " return None\n", " CC = self.predhodno_stanje(C, i)\n", " obiskana = self.phi(CC)\n", " for j in reversed(self.ureditve[l][:CC[l]]):\n", " if len(obiskana.difference(self.phi(self.predhodno_stanje(CC, j)))) == 1:\n", " return j\n", " return None\n", " \n", " def f(self, C, i): # minimalni čas, da dosežemo stanje (C, i)\n", " if (C, i) not in self.F: \n", " kandidati = []\n", " CC = self.predhodno_stanje(C, i)\n", " for l in range(4):\n", " j = self.predhodnik(l, C, i)\n", " # če trenutni seznam ne da kandidata, ga preskočimo\n", " if j is None:\n", " continue\n", " (t, _), _, _ = self.f(CC, j) # funkcija g potrebuje 3 argumente, prvi je čas, ki ga izračunamo s f\n", " kandidati.append((self.g(t, j, i), l, j)) \n", " # vsak kandidat je določen s 3 argumenti: \n", " # * rezultat funkcije g (najhitrejši čas obiska cilja i, če smo nazadnje obiskali cilj j ob času t), \n", " # * indeks seznama, iz katerega je prišel naslednji obiskani cilj (l) \n", " # * predhodnik (j) \n", " # najmanjšega od kandidatov shranimo v F pod ključ (C, i) - pripadajoče stanje\n", " # če ni kandidatov, gremo najprej proti cilju i\n", " self.F[C, i] = min(kandidati) if kandidati else (self.g(0, None, i), None, None)\n", " return self.F[C, i]\n", " \n", " def resi(self):\n", " n = len(self.cilji) # stevilo vseh ciljev\n", " # izvedemo f na vseh možnih naborih C za vsak cilj i\n", " for a in range(n):\n", " for b in range(n):\n", " for c in range(n):\n", " for d in range(n):\n", " for i in cilji:\n", " self.f((a, b, c, d), i) # shrani v F minimalni čas, da dosežemo stanje (C, i)\n", " return min(self.f((n, n, n, n), i) for i in cilji) # od zaključnega stanja \"nazaj\" poteka rekurzija\n", " \n", " def rekonstruiraj_resitev(self):\n", " resitev = sorted(self.F.items(), key = lambda c: c[1]) # uredi zapise v F po časih (kdaj smo kje)\n", " return [C_i[1] for C_i, _ in resitev]\n" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((25.769230769230766,\n", " [(21.384615384615383, -3.615384615384615),\n", " (25.769230769230766, 2.084615384615385)]),\n", " 2,\n", " Cilj(-1, 6, 1))" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tsp = SLMTTSP(cilji, 0.3)\n", "tsp.resi()" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((25.769230769230766,\n", " [(21.384615384615383, -3.615384615384615),\n", " (25.769230769230766, 2.084615384615385)]),\n", " 2,\n", " Cilj(-1, 6, 1))" ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tsp.F[(7, 7, 7, 7), cilji[3]]" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(7, 7, 7, 7) Cilj(-1, 15, 4)\n", "(6, 0, 3, 3) Cilj(1, 5, 5)\n", "(5, 0, 3, 0) Cilj(-1, 6, 1)\n", "(4, 0, 2, 0) Cilj(1, 0, 2)\n", "(3, 0, 2, 0) Cilj(-1, 13, -3)\n", "(2, 0, 1, 0) Cilj(-1, 18, -7)\n", "(1, 0, 0, 0) Cilj(1, 8, -2)\n", "(2, 0, 1, 0) Cilj(-1, 18, -7)\n", "(3, 0, 2, 0) Cilj(-1, 13, -3)\n", "(5, 0, 3, 0) Cilj(-1, 6, 1)\n", "(6, 0, 3, 3) Cilj(-1, 6, 1)\n", "(4, 0, 2, 3) Cilj(1, 0, 2)\n", "(3, 0, 2, 1) Cilj(-1, 13, -3)\n", "(2, 0, 1, 1) Cilj(-1, 18, -7)\n", "(1, 0, 0, 1) Cilj(1, 8, -2)\n", "(0, 0, 0, 1) Cilj(1, 5, 5)\n", "(1, 0, 0, 1) Cilj(1, 5, 5)\n", "(1, 0, 0, 0) Cilj(1, 8, -2)\n", "(2, 0, 1, 1) Cilj(-1, 18, -7)\n", "(2, 0, 1, 1) Cilj(1, 5, 5)\n", "(2, 0, 1, 0) Cilj(-1, 18, -7)\n", "(2, 0, 1, 0) Cilj(-1, 18, -7)\n", "(3, 0, 2, 1) Cilj(-1, 13, -3)\n", "(3, 0, 2, 1) Cilj(1, 5, 5)\n", "(3, 0, 2, 0) Cilj(-1, 13, -3)\n", "(3, 0, 2, 0) Cilj(-1, 13, -3)\n", "(4, 0, 2, 3) Cilj(-1, 13, -3)\n", "(2, 0, 1, 3) Cilj(-1, 18, -7)\n", "(1, 0, 0, 3) Cilj(1, 8, -2)\n", "(0, 0, 0, 2) Cilj(1, 0, 2)\n", "(0, 0, 0, 1) Cilj(1, 5, 5)\n", "(1, 0, 0, 3) Cilj(1, 8, -2)\n", "(2, 0, 1, 3) Cilj(-1, 18, -7)\n", "(2, 0, 1, 3) Cilj(1, 8, -2)\n", "(0, 0, 1, 2) Cilj(-1, 18, -7)\n", "(0, 0, 0, 2) Cilj(1, 0, 2)\n", "(0, 0, 1, 2) Cilj(1, 0, 2)\n", "(0, 0, 1, 1) Cilj(-1, 18, -7)\n", "(0, 0, 0, 1) Cilj(1, 5, 5)\n", "(0, 0, 1, 1) Cilj(1, 5, 5)\n", "(0, 0, 1, 0) Cilj(-1, 18, -7)\n", "(4, 0, 2, 3) Cilj(1, 8, -2)\n", "(0, 0, 2, 2) Cilj(-1, 13, -3)\n", "(0, 0, 1, 2) Cilj(-1, 18, -7)\n", "(0, 0, 1, 2) Cilj(1, 0, 2)\n", "(0, 0, 2, 2) Cilj(1, 0, 2)\n", "(0, 0, 2, 1) Cilj(-1, 13, -3)\n", "(0, 0, 1, 1) Cilj(-1, 18, -7)\n", "(0, 0, 1, 1) Cilj(1, 5, 5)\n", "(0, 0, 2, 1) Cilj(1, 5, 5)\n", "(0, 0, 2, 0) Cilj(-1, 13, -3)\n", "(0, 0, 1, 0) Cilj(-1, 18, -7)\n", "(6, 0, 3, 3) Cilj(1, 8, -2)\n", "(0, 0, 3, 2) Cilj(-1, 6, 1)\n", "(0, 0, 2, 2) Cilj(-1, 13, -3)\n", "(0, 0, 2, 2) Cilj(1, 0, 2)\n", "(0, 0, 3, 2) Cilj(1, 0, 2)\n", "(0, 0, 3, 1) Cilj(-1, 6, 1)\n", "(0, 0, 2, 1) Cilj(-1, 13, -3)\n", "(0, 0, 2, 1) Cilj(1, 5, 5)\n", "(0, 0, 3, 1) Cilj(1, 5, 5)\n", "(0, 0, 3, 0) Cilj(-1, 6, 1)\n", "(0, 0, 2, 0) Cilj(-1, 13, -3)\n" ] }, { "data": { "text/plain": [ "((25.769230769230766,\n", " [(21.384615384615383, -3.615384615384615),\n", " (25.769230769230766, 2.084615384615385)]),\n", " 2,\n", " Cilj(-1, 6, 1))" ] }, "execution_count": 119, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tsp = SLMTTSP(cilji, 0.3)\n", "tsp.f((len(cilji), )*4, cilji[3])" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[5, 6, 2, 0, 4, 1, 3],\n", " [3, 1, 4, 0, 2, 6, 5],\n", " [6, 2, 4, 3, 5, 0, 1],\n", " [1, 0, 5, 3, 4, 2, 6]]" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[[cilji.index(i) for i in l] for l in tsp.ureditve]" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[Cilj(1, 8, -2),\n", " Cilj(-1, 18, -7),\n", " Cilj(-1, 13, -3),\n", " Cilj(1, 0, 2),\n", " Cilj(-1, 6, 1),\n", " Cilj(1, 5, 5)],\n", " [],\n", " [Cilj(-1, 18, -7), Cilj(-1, 13, -3), Cilj(-1, 6, 1)],\n", " [Cilj(1, 5, 5), Cilj(1, 0, 2), Cilj(1, 8, -2)]]" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[tsp.ureditve[l][:m] for l, m in enumerate((6, 0, 3, 3))]" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((6, 0, 3, 3),\n", " (5, 0, 3, 0),\n", " {Cilj(-1, 13, -3),\n", " Cilj(-1, 18, -7),\n", " Cilj(-1, 6, 1),\n", " Cilj(1, 0, 2),\n", " Cilj(1, 5, 5),\n", " Cilj(1, 8, -2)},\n", " {Cilj(-1, 13, -3),\n", " Cilj(-1, 18, -7),\n", " Cilj(-1, 6, 1),\n", " Cilj(1, 0, 2),\n", " Cilj(1, 8, -2)})" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C1 = tsp.predhodno_stanje((len(cilji), )*4, cilji[3])\n", "C2 = tsp.predhodno_stanje(C1, cilji[1])\n", "C1, C2, tsp.phi(C1), tsp.phi(C2)" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(6, 0, 3, 3) Cilj(1, 5, 5)\n", "(5, 0, 3, 0) Cilj(-1, 6, 1)\n", "(4, 0, 2, 0) Cilj(1, 0, 2)\n", "(3, 0, 2, 0) Cilj(-1, 13, -3)\n", "(2, 0, 1, 0) Cilj(-1, 18, -7)\n" ] }, { "ename": "TypeError", "evalue": "unsupported operand type(s) for -: 'tuple' and 'int'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m[\u001b[0m\u001b[0mtsp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mC1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtsp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredhodnik\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ml\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mC1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcilji\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0ml\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m(.0)\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m[\u001b[0m\u001b[0mtsp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mC1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtsp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredhodnik\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ml\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mC1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcilji\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0ml\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mf\u001b[0;34m(self, C, i)\u001b[0m\n\u001b[1;32m 51\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mj\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 52\u001b[0m \u001b[0;32mcontinue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 53\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mCC\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# funkcija g potrebuje 3 argumente, prvi je čas, ki ga izračunamo s f\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 54\u001b[0m \u001b[0mkandidati\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ml\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 55\u001b[0m \u001b[0;31m# vsak kandidat je določen s 3 argumenti:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mf\u001b[0;34m(self, C, i)\u001b[0m\n\u001b[1;32m 51\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mj\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 52\u001b[0m \u001b[0;32mcontinue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 53\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mCC\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# funkcija g potrebuje 3 argumente, prvi je čas, ki ga izračunamo s f\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 54\u001b[0m \u001b[0mkandidati\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ml\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 55\u001b[0m \u001b[0;31m# vsak kandidat je določen s 3 argumenti:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mf\u001b[0;34m(self, C, i)\u001b[0m\n\u001b[1;32m 51\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mj\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 52\u001b[0m \u001b[0;32mcontinue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 53\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mCC\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# funkcija g potrebuje 3 argumente, prvi je čas, ki ga izračunamo s f\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 54\u001b[0m \u001b[0mkandidati\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ml\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 55\u001b[0m \u001b[0;31m# vsak kandidat je določen s 3 argumenti:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mf\u001b[0;34m(self, C, i)\u001b[0m\n\u001b[1;32m 52\u001b[0m \u001b[0;32mcontinue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 53\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mCC\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# funkcija g potrebuje 3 argumente, prvi je čas, ki ga izračunamo s f\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 54\u001b[0;31m \u001b[0mkandidati\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ml\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 55\u001b[0m \u001b[0;31m# vsak kandidat je določen s 3 argumenti:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 56\u001b[0m \u001b[0;31m# * rezultat funkcije g (najhitrejši čas obiska cilja i, če smo nazadnje obiskali cilj j ob času t),\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mg\u001b[0;34m(self, t, j, i)\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# najhitrejši čas obiska cilja i, če smo nazadnje obiskali cilj j ob času t\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m \u001b[0mposi\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpozicija\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 14\u001b[0m \u001b[0mposj\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mj\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpozicija\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0mrazlika\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mposi\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mposj\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mpozicija\u001b[0;34m(self, t, v)\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mpozicija\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# pozicija cilja ob casu t, v = hitrost\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mp\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mv\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0md\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 9\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__hash__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'tuple' and 'int'" ] } ], "source": [ "[tsp.f(C1, tsp.predhodnik(l, C1, cilji[3])) for l in range(4)]" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Cilj(1, 5, 5)" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tsp.cilji[1]" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 4 }