Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7175695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:21:21+00:00 2026-05-28T16:21:21+00:00

I have to solve the Travelling Salesman Problem using a genetic algorithm that I

  • 0

I have to solve the Travelling Salesman Problem using a genetic algorithm that I will have to write for homework.

The problem consists of 52 cities. Therefore, the search space is 52!. I need to randomly sample (say) 1000 permutations of range(1, 53) as individuals for the initial population of my genetic algorithm.

In order to do this, I tried:

>>> random.sample(itertools.permutations(range(1, 53)), 1000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/random.py", line 314, in sample
    n = len(population)
TypeError: object of type 'itertools.permutations' has no len()

So I tried

>>> random.sample(list(itertools.permutations(range(1, 53))), 1000)

However, given that 52! is VERY large, the list operation is maxing out the memory and swap space on my computer. I can’t just pick the first 1000 permutations generated by itertools.permutations because it’s very deterministic and that would bias my genetic algorithm.

Is there a better way to achieve this sampling?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:21:22+00:00Added an answer on May 28, 2026 at 4:21 pm

    You don’t need to permute at all. Call random.sample(range(52), 52) 1000 times.

    P.S.: You really should use zero-based indexing (range(52) as opposed to range(1, 53)) in all your work. Things generally work out better that way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Using A* to solve Travelling Salesman Problem I have recently learned that
I am trying to write a genetic algorithm for homework to solve the travelling
I've implemented a genetic algorithm in java to solve the traveling salesman problem for
Ok, so here's the problem I have to solve. I need to write a
I have a problem that i cant solve :( I have a user control
I am trying to solve this problem : https://www.spoj.pl/problems/CERC07S/ I have identified that i
I have created a puzzle which is a derivative of the travelling salesman problem,
I was a problem with drawing rectangle using mouse. I have solve my problem
I'm trying to use the Simple hill climbing algorithm to solve the travelling salesman
I have to write a C app that will act as both UDP sender

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.