Are you using the VD package? If so, have you also installed the patch I have uploaded?
This error looks like your computer just can't handle the (rather badly optimized) Firaxis Civ4 code, plus RFC: DoC code on top of that, plus the graphical strain of VD.Sorry, Leoreth, but should the patch solve all previous errors, like "Memory errors: bad allocation" ?
def isConnectedByRailroad(self, iPlayer, iStartX, iStartY, iTargetX, iTargetY):
startPlot = gc.getMap().plot(iStartX, iStartY)
if not (startPlot.isCity() and startPlot.getOwner() == iPlayer): return False
lNodes = [(-utils.calculateDistance(iStartX, iStartY, iTargetX, iTargetY), iStartX, iStartY)]
lVisitedNodes = []
while len(lNodes) > 0:
h, x, y = lNodes[0]
lNodes.remove((h, x, y))
lVisitedNodes.append((h, x, y))
for i in range(x-1, x+2):
for j in range(y-1, y+2):
plot = gc.getMap().plot(i, j)
if plot.getOwner() == iPlayer and (plot.isCity() or plot.getRouteType() == gc.getInfoTypeForString("ROUTE_RAILROAD")):
if (i, j) == (iTargetX, iTargetY): return True
tTuple = (-utils.calculateDistance(i, j, iTargetX, iTargetY), i, j)
[COLOR="Red"]if not tTuple in lVisitedNodes:
lNodes.append(tTuple)[/COLOR]
lNodes.sort()
return False
def isConnectedByRailroad(self, iPlayer, iStartX, iStartY, iTargetX, iTargetY):
startPlot = gc.getMap().plot(iStartX, iStartY)
if not (startPlot.isCity() and startPlot.getOwner() == iPlayer): return False
lNodes = [(-utils.calculateDistance(iStartX, iStartY, iTargetX, iTargetY), iStartX, iStartY)]
lVisitedNodes = []
while len(lNodes) > 0:
h, x, y = lNodes[0]
lNodes.remove((h, x, y))
lVisitedNodes.append((h, x, y))
for i in range(x-1, x+2):
for j in range(y-1, y+2):
plot = gc.getMap().plot(i, j)
if plot.getOwner() == iPlayer and (plot.isCity() or plot.getRouteType() == gc.getInfoTypeForString("ROUTE_RAILROAD")):
if (i, j) == (iTargetX, iTargetY): return True
tTuple = (-utils.calculateDistance(i, j, iTargetX, iTargetY), i, j)
[COLOR="Red"]if not tTuple in lVisitedNodes:[/COLOR]
[COLOR="Blue"]if tTuple not in lNodes:[/COLOR]
[COLOR="Red"]lNodes.append(tTuple)[/COLOR]
lNodes.sort()
return False
def isConnectedByRailroad(self, iPlayer, iStartX, iStartY, iTargetX, iTargetY):
startPlot = gc.getMap().plot(iStartX, iStartY)
if not (startPlot.isCity() and startPlot.getOwner() == iPlayer):
return False
iRailroad = gc.getInfoTypeForString("ROUTE_RAILROAD")
lNodes = [(iStartX, iStartY)]
lVisitedNodes = []
while True > 0:
lTempNodes = []
for (x, y) in lNodes:
if (x, y) not in lVisitedNodes:
lVisitedNodes.append((x, y))
for (i, j) in [(x-1, y-1), (x-1, y), (x-1, y+1),
(x, y-1), (x, y+1),
(x+1, y-1), (x+1, y), (x+1, y+1)]:
plot = gc.getMap().plot(i, j)
if not plot.isNone() and plot.getOwner() == iPlayer and (plot.isCity() or plot.getRouteType() == iRailroad):
if (i, j) == (iTargetX, iTargetY):
return True
if (i, j) not in lNodes:
if (i, j) not in lTempNodes:
lTempNodes.append((i, j))
if lTempNodes:
lNodes = lTempNodes
else:
return False
for tCoast in lSiberianCoast:
if self.isConnectedByRailroad(iRussia, con.tCapitals[0][iRussia][0], con.tCapitals[0][iRussia][1], tCoast[0], tCoast[1]):
bRailroad = True
break
def isConnectedByRailroadTuple(self, iPlayer, iStartX, iStartY, tTargets):
startPlot = gc.getMap().plot(iStartX, iStartY)
if not (startPlot.isCity() and startPlot.getOwner() == iPlayer):
return False
iRailroad = gc.getInfoTypeForString("ROUTE_RAILROAD")
lNodes = [(iStartX, iStartY)]
lVisitedNodes = []
while True > 0:
lTempNodes = []
for (x, y) in lNodes:
if (x, y) not in lVisitedNodes:
lVisitedNodes.append((x, y))
for (i, j) in [(x-1, y-1), (x-1, y), (x-1, y+1),
(x, y-1), (x, y+1),
(x+1, y-1), (x+1, y), (x+1, y+1)]:
plot = gc.getMap().plot(i, j)
if not plot.isNone() and plot.getOwner() == iPlayer and (plot.isCity() or plot.getRouteType() == iRailroad):
[COLOR="Red"]if (i, j) in tTargets:[/COLOR]
return True
if (i, j) not in lNodes:
if (i, j) not in lTempNodes:
lTempNodes.append((i, j))
if lTempNodes:
lNodes = lTempNodes
else:
return False
Actually, the algorithm you used is simple, just one-pass, but quite slow. Fortunately, we don't use it frequently. Thus the slowness is tolerable.
But I prefer another variation of this algorithm, which is much faster. Though by some famous algorithm's standard, it is still very slow.
Spoiler :
Code:def isConnectedByRailroad(self, iPlayer, iStartX, iStartY, iTargetX, iTargetY): startPlot = gc.getMap().plot(iStartX, iStartY) if not (startPlot.isCity() and startPlot.getOwner() == iPlayer): return False iRailroad = gc.getInfoTypeForString("ROUTE_RAILROAD") lNodes = [(iStartX, iStartY)] lVisitedNodes = [] while True > 0: lTempNodes = [] for (x, y) in lNodes: if (x, y) not in lVisitedNodes: lVisitedNodes.append((x, y)) for (i, j) in [(x-1, y-1), (x-1, y), (x-1, y+1), (x, y-1), (x, y+1), (x+1, y-1), (x+1, y), (x+1, y+1)]: plot = gc.getMap().plot(i, j) if not plot.isNone() and plot.getOwner() == iPlayer and (plot.isCity() or plot.getRouteType() == iRailroad): if (i, j) == (iTargetX, iTargetY): return True if (i, j) not in lNodes: if (i, j) not in lTempNodes: lTempNodes.append((i, j)) if lTempNodes: lNodes = lTempNodes else: return False
Furthermore, when you check Trans-Siberian Railroad, there are more than one target. You check them one by one
Spoiler :
Code:for tCoast in lSiberianCoast: if self.isConnectedByRailroad(iRussia, con.tCapitals[0][iRussia][0], con.tCapitals[0][iRussia][1], tCoast[0], tCoast[1]): bRailroad = True break
I prefer to use the following function to check them simultaneously.
Spoiler :
Code:def isConnectedByRailroadTuple(self, iPlayer, iStartX, iStartY, tTargets): startPlot = gc.getMap().plot(iStartX, iStartY) if not (startPlot.isCity() and startPlot.getOwner() == iPlayer): return False iRailroad = gc.getInfoTypeForString("ROUTE_RAILROAD") lNodes = [(iStartX, iStartY)] lVisitedNodes = [] while True > 0: lTempNodes = [] for (x, y) in lNodes: if (x, y) not in lVisitedNodes: lVisitedNodes.append((x, y)) for (i, j) in [(x-1, y-1), (x-1, y), (x-1, y+1), (x, y-1), (x, y+1), (x+1, y-1), (x+1, y), (x+1, y+1)]: plot = gc.getMap().plot(i, j) if not plot.isNone() and plot.getOwner() == iPlayer and (plot.isCity() or plot.getRouteType() == iRailroad): [COLOR="Red"]if (i, j) in tTargets:[/COLOR] return True if (i, j) not in lNodes: if (i, j) not in lTempNodes: lTempNodes.append((i, j)) if lTempNodes: lNodes = lTempNodes else: return False
As Lone Wolf said, it's possible that these errors are caused because your RAM is full. How many GB do you use?Sorry, Leoreth, but should the patch solve all previous errors, like "Memory errors: bad allocation" ?
Yes, I mentioned this in the VD thread OP in hopes everyone would notice it. Repackaging and reuploading the whole package is quite annoying so I released this small patch instead. See here:I downloaded version 1.9 and 'Varietas Delectat and Ethnic City Styles for DoC', that's all. Do I have to download something more?
Even more important note: There were some errors in the VD files. I'm going to repackage it with the fixes soon, but until the please also download this patch (unpack it into your Mods folder after you have installed VD), even when you've just downloaded VD. People who are using it already should do the same of course.
You're right, for some reason I thought I'd be fine if I removed the originally expanded node but that's obviously not the case.Victory.py, rev384
There is a defect in the function you use to check Trans-Siberian Railroad.
This is your code.
Spoiler :
Code:def isConnectedByRailroad(self, iPlayer, iStartX, iStartY, iTargetX, iTargetY): startPlot = gc.getMap().plot(iStartX, iStartY) if not (startPlot.isCity() and startPlot.getOwner() == iPlayer): return False lNodes = [(-utils.calculateDistance(iStartX, iStartY, iTargetX, iTargetY), iStartX, iStartY)] lVisitedNodes = [] while len(lNodes) > 0: h, x, y = lNodes[0] lNodes.remove((h, x, y)) lVisitedNodes.append((h, x, y)) for i in range(x-1, x+2): for j in range(y-1, y+2): plot = gc.getMap().plot(i, j) if plot.getOwner() == iPlayer and (plot.isCity() or plot.getRouteType() == gc.getInfoTypeForString("ROUTE_RAILROAD")): if (i, j) == (iTargetX, iTargetY): return True tTuple = (-utils.calculateDistance(i, j, iTargetX, iTargetY), i, j) [COLOR="Red"]if not tTuple in lVisitedNodes: lNodes.append(tTuple)[/COLOR] lNodes.sort() return False
In my opinion, it should be
Spoiler :
Code:def isConnectedByRailroad(self, iPlayer, iStartX, iStartY, iTargetX, iTargetY): startPlot = gc.getMap().plot(iStartX, iStartY) if not (startPlot.isCity() and startPlot.getOwner() == iPlayer): return False lNodes = [(-utils.calculateDistance(iStartX, iStartY, iTargetX, iTargetY), iStartX, iStartY)] lVisitedNodes = [] while len(lNodes) > 0: h, x, y = lNodes[0] lNodes.remove((h, x, y)) lVisitedNodes.append((h, x, y)) for i in range(x-1, x+2): for j in range(y-1, y+2): plot = gc.getMap().plot(i, j) if plot.getOwner() == iPlayer and (plot.isCity() or plot.getRouteType() == gc.getInfoTypeForString("ROUTE_RAILROAD")): if (i, j) == (iTargetX, iTargetY): return True tTuple = (-utils.calculateDistance(i, j, iTargetX, iTargetY), i, j) [COLOR="Red"]if not tTuple in lVisitedNodes:[/COLOR] [COLOR="Blue"]if tTuple not in lNodes:[/COLOR] [COLOR="Red"]lNodes.append(tTuple)[/COLOR] lNodes.sort() return False
If you don't add "if tTuple not in lNodes:", lNodes will contain duplicated items, namely some nodes will be processed multiple times.
Under some circumstances, the check will be extremely slow. It would take several minutes or even more time to finish the calculation.
It's not necessary, but should optimize the algorithm. I repurposed A* for this algorithm even though it's not strictly path-finding (since it only needs to find one path, not the best). Using the distance as first element means that the nodes list will be sorted by distance, so nodes close to the target will be checked before all others (otherwise the front would expand uniformly from the center instead of towards the target).Moreover, the first elemet of 3-tuple, -utils.calculateDistance(i, j, iTargetX, iTargetY) is completely useless. The coordinate pair is enough.
Did you actually profile them against my implementation? I'd be curious about the results.Actually, the algorithm you used is simple, just one-pass, but quite slow. Fortunately, we don't use it frequently. Thus the slowness is tolerable.
But I prefer another variation of this algorithm, which is much faster. Though by some famous algorithm's standard, it is still very slow.
Spoiler :
Code:def isConnectedByRailroad(self, iPlayer, iStartX, iStartY, iTargetX, iTargetY): startPlot = gc.getMap().plot(iStartX, iStartY) if not (startPlot.isCity() and startPlot.getOwner() == iPlayer): return False iRailroad = gc.getInfoTypeForString("ROUTE_RAILROAD") lNodes = [(iStartX, iStartY)] lVisitedNodes = [] while True > 0: lTempNodes = [] for (x, y) in lNodes: if (x, y) not in lVisitedNodes: lVisitedNodes.append((x, y)) for (i, j) in [(x-1, y-1), (x-1, y), (x-1, y+1), (x, y-1), (x, y+1), (x+1, y-1), (x+1, y), (x+1, y+1)]: plot = gc.getMap().plot(i, j) if not plot.isNone() and plot.getOwner() == iPlayer and (plot.isCity() or plot.getRouteType() == iRailroad): if (i, j) == (iTargetX, iTargetY): return True if (i, j) not in lNodes: if (i, j) not in lTempNodes: lTempNodes.append((i, j)) if lTempNodes: lNodes = lTempNodes else: return False
Furthermore, when you check Trans-Siberian Railroad, there are more than one target. You check them one by one
Spoiler :
Code:for tCoast in lSiberianCoast: if self.isConnectedByRailroad(iRussia, con.tCapitals[0][iRussia][0], con.tCapitals[0][iRussia][1], tCoast[0], tCoast[1]): bRailroad = True break
I prefer to use the following function to check them simultaneously.
Spoiler :
Code:def isConnectedByRailroadTuple(self, iPlayer, iStartX, iStartY, tTargets): startPlot = gc.getMap().plot(iStartX, iStartY) if not (startPlot.isCity() and startPlot.getOwner() == iPlayer): return False iRailroad = gc.getInfoTypeForString("ROUTE_RAILROAD") lNodes = [(iStartX, iStartY)] lVisitedNodes = [] while True > 0: lTempNodes = [] for (x, y) in lNodes: if (x, y) not in lVisitedNodes: lVisitedNodes.append((x, y)) for (i, j) in [(x-1, y-1), (x-1, y), (x-1, y+1), (x, y-1), (x, y+1), (x+1, y-1), (x+1, y), (x+1, y+1)]: plot = gc.getMap().plot(i, j) if not plot.isNone() and plot.getOwner() == iPlayer and (plot.isCity() or plot.getRouteType() == iRailroad): [COLOR="Red"]if (i, j) in tTargets:[/COLOR] return True if (i, j) not in lNodes: if (i, j) not in lTempNodes: lTempNodes.append((i, j)) if lTempNodes: lNodes = lTempNodes else: return False
def isConnectedByRailroad(self, iPlayer, tStart, lTargetList):
if len(lTargetList) == 0:
return False
iStartX, iStartY = tStart
iTargetX, iTargetY = lTargetList[0]
startPlot = gc.getMap().plot(iStartX, iStartY)
if not (startPlot.isCity() and startPlot.getOwner() == iPlayer): return False
lNodes = [(utils.calculateDistance(iStartX, iStartY, iTargetX, iTargetY), iStartX, iStartY)]
heapq.heapify(lNodes)
lVisitedNodes = []
while len(lNodes) > 0:
h, x, y = heapq.heappop(lNodes)
lVisitedNodes.append((h, x, y))
for i in range(x-1, x+2):
for j in range(y-1, y+2):
plot = gc.getMap().plot(i, j)
if plot.getOwner() == iPlayer and (plot.isCity() or plot.getRouteType() == gc.getInfoTypeForString("ROUTE_RAILROAD")):
if (i, j) in lTargetList: return True
tTuple = (utils.calculateDistance(i, j, iTargetX, iTargetY), i, j)
if not tTuple in lVisitedNodes:
if not tTuple in lNodes:
heapq.heappush(lNodes, tTuple)
return False
(1) (2) (3) (4)
Old Implementation 0.0916 2.7036 7.5538 ---
Without Heuristic 0.1121 2.3804 7.1167 35.3760
Heuristic and heapq 0.1121 2.4170 2.8812 4.1016
import time
def timer(f):
def g(*args):
start = time.time()
f(*args)
end = time.time()
print 'Time ', end - start
show()
return g
def show():
print 'Visited ', len(lVisitedNodes)
def dist(x1, y1, x2, y2):
dx = abs(x2-x1)
dy = abs(y2-y1)
return max(dx, dy)
def isValid(i, j):
return 1 <= i <= 50 and 1 <= j <= 50
@timer
def f1(iStartX, iStartY, iTargetX, iTargetY):
global lVisitedNodes
lNodes = [(-dist(iStartX, iStartY, iTargetX, iTargetY), iStartX, iStartY)]
lVisitedNodes = []
while len(lNodes) > 0:
h, x, y = lNodes[0]
lNodes.remove((h, x, y))
lVisitedNodes.append((h, x, y))
for i in range(x-1, x+2):
for j in range(y-1, y+2):
if isValid(i, j):
if (i, j) == (iTargetX, iTargetY): return True
tTuple = (-dist(i, j, iTargetX, iTargetY), i, j)
if not tTuple in lVisitedNodes:
if tTuple not in lNodes:
lNodes.append(tTuple)
lNodes.sort()
return False
@timer
def f2(iStartX, iStartY, iTargetX, iTargetY):
global lVisitedNodes
lNodes = [(iStartX, iStartY)]
lVisitedNodes = []
while len(lNodes) > 0:
x, y = lNodes[0]
lNodes.remove((x, y))
lVisitedNodes.append((x, y))
for i in range(x-1, x+2):
for j in range(y-1, y+2):
if isValid(i, j):
if (i, j) == (iTargetX, iTargetY): return True
tTuple = (i, j)
if not tTuple in lVisitedNodes:
if tTuple not in lNodes:
lNodes.append(tTuple)
lNodes.sort()
return False
@timer
def f3(iStartX, iStartY, iTargetX, iTargetY):
global lVisitedNodes
lNodes = [(iStartX, iStartY)]
lVisitedNodes = []
while True > 0:
lTempNodes = []
for (x, y) in lNodes:
if (x, y) not in lVisitedNodes:
lVisitedNodes.append((x, y))
for (i, j) in [(x-1, y-1), (x-1, y), (x-1, y+1),
(x, y-1), (x, y+1),
(x+1, y-1), (x+1, y), (x+1, y+1)]:
if isValid(i, j):
if (i, j) == (iTargetX, iTargetY):
return True
if (i, j) not in lNodes:
if (i, j) not in lTempNodes:
lTempNodes.append((i, j))
if lTempNodes:
lNodes = lTempNodes
else:
return False
@timer
def h(iStartX, iStartY, iTargetX, iTargetY):
global lVisitedNodes
lNodes = [(-dist(iStartX, iStartY, iTargetX, iTargetY), iStartX, iStartY)]
lVisitedNodes = []
while len(lNodes) > 0:
h, x, y = lNodes[0]
heapq.heappop(lNodes)
lVisitedNodes.append((h, x, y))
for i in range(x-1, x+2):
for j in range(y-1, y+2):
if isValid(i, j):
if (i, j) == (iTargetX, iTargetY): return True
tTuple = (-dist(i, j, iTargetX, iTargetY), i, j)
if not tTuple in lVisitedNodes:
if tTuple not in lNodes:
heapq.heappush(lNodes, tTuple)
return False
>>> isValid = lambda i,j:1<=i<=70 and 1<=j<=70
>>> h(1,1,70,70)
Time 5.69000005722
Visited 4897
>>> f1(1,1,70,70)
Time 5.72499990463
Visited 4897
>>> f2(1,1,70,70)
Time 5.58899998665
Visited 4829
>>> f3(1,1,70,70)
Time 1.63800001144
Visited 4761
>>> isValid = lambda i,j:1<=i<=80 and 1<=j<=80
>>> h(1,1,80,80)
Time 9.71899986267
Visited 6397
>>> f1(1,1,80,80)
Time 9.77900004387
Visited 6397
>>> f2(1,1,80,80)
Time 9.51099991798
Visited 6319
>>> f3(1,1,80,80)
Time 2.72699999809
Visited 6241
import time
import heapq
def timer(f):
def g(*args):
start = time.time()
f(*args)
end = time.time()
print 'Time ', end - start
show()
return g
def show():
print 'Visited ', len(lVisitedNodes)
def dist(x1, y1, x2, y2):
dx = abs(x2-x1)
dy = abs(y2-y1)
return max(dx, dy)
def isValid(i, j):
return 1 <= i <= 50 and 1 <= j <= 50
@timer
def h(iStartX, iStartY, iTargetX, iTargetY):
global lVisitedNodes
lNodes = [(-dist(iStartX, iStartY, iTargetX, iTargetY), iStartX, iStartY)]
lVisitedNodes = []
while len(lNodes) > 0:
h, x, y = lNodes[0]
heapq.heappop(lNodes)
lVisitedNodes.append((h, x, y))
for i in range(x-1, x+2):
for j in range(y-1, y+2):
if isValid(i, j):
if (i, j) == (iTargetX, iTargetY): return True
tTuple = (-dist(i, j, iTargetX, iTargetY), i, j)
if not tTuple in lVisitedNodes:
if tTuple not in lNodes:
heapq.heappush(lNodes, tTuple)
return False
@timer
def f1(iStartX, iStartY, iTargetX, iTargetY):
global lVisitedNodes
lNodes = [(-dist(iStartX, iStartY, iTargetX, iTargetY), iStartX, iStartY)]
lVisitedNodes = []
while len(lNodes) > 0:
h, x, y = lNodes[0]
lNodes.remove((h, x, y))
lVisitedNodes.append((h, x, y))
for i in range(x-1, x+2):
for j in range(y-1, y+2):
if isValid(i, j):
if (i, j) == (iTargetX, iTargetY): return True
tTuple = (-dist(i, j, iTargetX, iTargetY), i, j)
if not tTuple in lVisitedNodes:
if tTuple not in lNodes:
lNodes.append(tTuple)
lNodes.sort()
return False
@timer
def f2(iStartX, iStartY, iTargetX, iTargetY):
global lVisitedNodes
lNodes = [(iStartX, iStartY)]
lVisitedNodes = []
while len(lNodes) > 0:
x, y = lNodes[0]
lNodes.remove((x, y))
lVisitedNodes.append((x, y))
for i in range(x-1, x+2):
for j in range(y-1, y+2):
if isValid(i, j):
if (i, j) == (iTargetX, iTargetY): return True
tTuple = (i, j)
if not tTuple in lVisitedNodes:
if tTuple not in lNodes:
lNodes.append(tTuple)
lNodes.sort()
return False
@timer
def f3(iStartX, iStartY, iTargetX, iTargetY):
global lVisitedNodes
lNodes = [(iStartX, iStartY)]
lVisitedNodes = []
while True:
lTempNodes = []
for (x, y) in lNodes:
if (x, y) not in lVisitedNodes:
lVisitedNodes.append((x, y))
for (i, j) in [(x-1, y-1), (x-1, y), (x-1, y+1),
(x, y-1), (x, y+1),
(x+1, y-1), (x+1, y), (x+1, y+1)]:
if isValid(i, j):
if (i, j) == (iTargetX, iTargetY):
return True
if (i, j) not in lNodes:
if (i, j) not in lTempNodes:
lTempNodes.append((i, j))
if lTempNodes:
lNodes = lTempNodes
else:
return False
@timer
def f4(iStartX, iStartY, tTargets):
global lVisitedNodes
lNodes = [(iStartX, iStartY)]
lVisitedNodes = []
while True > 0:
lTempNodes = []
for (x, y) in lNodes:
if (x, y) not in lVisitedNodes:
lVisitedNodes.append((x, y))
for (i, j) in [(x-1, y-1), (x-1, y), (x-1, y+1),
(x, y-1), (x, y+1),
(x+1, y-1), (x+1, y), (x+1, y+1)]:
if isValid(i, j):
if (i, j) in tTargets:
return True
if (i, j) not in lNodes:
if (i, j) not in lTempNodes:
lTempNodes.append((i, j))
if lTempNodes:
lNodes = lTempNodes
else:
return False
@timer
def ff(iStartX, iStartY, iTargetX, iTargetY):
g[0] = 0
global lNodes
global lVisitedNodes
lNodes = [(iStartX, iStartY)]
lVisitedNodes = []
while True > 0:
lTemp = []
for (x, y) in lNodes:
if (x, y) not in lVisitedNodes:
lVisitedNodes.append((x, y))
for i in range(x-1, x+2):
for j in range(y-1, y+2):
if isValid(i, j):
if (i, j) == (iTargetX, iTargetY):
return True
if (i, j) not in lNodes:
if (i, j) not in lTemp:
lTemp.append((i, j))
if lTemp:
lNodes = lTemp
else:
return False
if debug:
print lNodes
@timer
def ff2(iStartX, iStartY, tTarget):
g[0] = 0
global lNodes
global lVisitedNodes
lNodes = [(iStartX, iStartY)]
lVisitedNodes = []
while True > 0:
lTemp = []
for (x, y) in lNodes:
if (x, y) not in lVisitedNodes:
lVisitedNodes.append((x, y))
for i in range(x-1, x+2):
for j in range(y-1, y+2):
if isValid(i, j):
if (i, j) in tTarget:
return True
if (i, j) not in lNodes:
if (i, j) not in lTemp:
lTemp.append((i, j))
if lTemp:
lNodes = lTemp
else:
return False
if debug:
print lNodes
def d(x, y, z):
d = {}
for i in range(1, x+1):
for j in range(1, y+1):
r = random.random()
d[(i, j)] = True if r > z else False
d[(1, 1)] = True
d[(x+1, y+1)] = True
return d
region = d(70, 70, 0.3)
def isValid(i, j):
return region.get((i, j), False)
It is trivial if you properly read the documentationYou are right. It does work, Much faster.
But this wrong negative sign is not trivial to debug.
I don't see how it could be slower. It's obvious that if there is no connection you're in the worst case because you have to traverse the whole graph. The presence of a heuristic doesn't really change that; you might traverse the graph in a different way but in the end you visit every node once.Then I found A* with heuristic function is much faster if target is connected.
But it has no advantage or is even slower when target is not connected.