Grasshopper

algorithmic modeling for Rhino

Hello,

I constructed the attached Python module to reproduce the traveling salesman algorithm.

I did this in Rhino 5 64 and gh 9.0014. The script worked wonderfully.  But then things went bad.  I copied copied and pasted the module and it turned red. This is curious.  I can do this in the same canvas and the minute I paste it, it goes red again while the original continues to work.  This was my first adventure into the Python after successfully going through all the MOdeLab tutorials on Python for Grasshopper. I was hoping that someone with experience in Python might be able to advise. The file is attached and all external geometry is internalized.  I will also paste the script.

Thanks for looking at this,'

robin

import rhinoscriptsyntax

firstPt = allPts[startIndex]
allPts.RemoveAt(startIndex)

visitedPts = [firstPt]
def TravelingSalesMan(fromPt,i):
    nextIndex = Rhino.Collections.Point3dList.ClosestIndexInList(allPts,fromPt)
    nextPt = allPts[nextIndex]
    visitedPts.append(nextPt)
    allPts.RemoveAt(nextIndex)
    if(i>0):
        TravelingSalesMan(nextPt,i-1)
TravelingSalesMan(firstPt,iterations)
a = visitedPts

Views: 3145

Attachments:

Replies to This Discussion

Hi again,

I forgot to mention that the original script came from an ex Lab tutorial.

robin

I'm not a python expert, but I think I've gotten it to work. I had to add the statement "import Rhino" at the top, and change all instances of listName.RemoveAt(index) to the statement del listName[index]. As far as I know RemoveAt is simply not a python list function, so I'm not sure how the script was ever working before... when I opened it it simply gave an error. 

Attachments:

Andrew,

Thanks so much for this. Yes, it now works as expected.  I was perplexed as well by the replaceAt statement but it seemed to work somehow.  This gives me a bit more confidence in moving ahead with Python.

robin

Hi,

Thanks for the nice script. 

I am trying to write a code -aiming to keep it super simple- to draw a continuous line in the travelingsalesman logic. This means the visited points list should collect first the points that are in the layerheight tolerance and then the closest one. 

import Rhino

#make a new list from the gh point list
if "allPt" not in globals() or reset:
allPt = allPts


firstPt = allPts[startIndex]
allPt.RemoveAt(startIndex)

visitedPts = [firstPt]

def TravelingSalesMan(fromPt,i):
nextIndex = Rhino.Collections.Point3dList.ClosestIndexInList(allPt,fromPt)
nextPt = allPt[nextIndex]


if nextPt.Z > fromPt.Z + LH:  #check if the closest point is within the layerheight tolerance.
allPt.RemoveAt(nextIndex) #first remove the next point from the list
allPt.append(nextPt) #then add it to the end of the list
TravelingSalesMan(nextPt,i-1)


allPt.RemoveAt(nextIndex)
visitedPts.append(nextPt)


if(i>0):
TravelingSalesMan(nextPt,i-1)

TravelingSalesMan(firstPt,iterations)
a = visitedPts

here is : 

any ideas_?

cheers,asy

Attachments:

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service