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

  • Home
  • SEARCH
  • 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 5992639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:33:39+00:00 2026-05-22T23:33:39+00:00

I’m working on a mutation algorithm for a homework project and I’m stumped at

  • 0

I’m working on a mutation algorithm for a homework project and I’m stumped at why I’m getting duplicates in the set. I’m trying to find a solution for the travelling ant problem (find shortest path for food) problem using a genetic mutation heuristic:

private static ArrayList<Node> mutate(ArrayList<Node> mutator) {
    ArrayList<Node> mutant = new ArrayList<Node>(mutator);

    Collections.rotate(mutant, 1);
    //Collections.shuffle(mutant, r);

    for (int i = 0; i < mutant.size() - 1; i++) {
        double mutantCoupleDistance     = mutant.get(i).getDistanceToNext(mutant.get(i+1));
        double mutatorCoupleDistance    = mutator.get(i).getDistanceToNext(mutator.get(i+1));

        if (mutantCoupleDistance < mutatorCoupleDistance) {
            // If the mutant gene has worse "fitness" then we'll take the good gene
            // from the mutator.
            Node tmp = mutant.get(i);
            mutant.set(i, mutator.get(i));
            mutator.set(i, tmp);

            tmp = mutant.get(i+1);
            mutant.set(i+1, mutator.get(i+1));
            mutator.set(i+1, tmp);
        }
    }

    return 
        mutant;
}

The array that I’m passing into the method is 49999 elements. It is being used like in a way like so:

        long
            startTime = System.currentTimeMillis();

        do {
            nodeList = mutate(nodeList);
        } while (System.currentTimeMillis() - startTime < 500);

I do not get any duplicates when I remove the swapping code from my mutate algorithm which is understandable. However, I’m having trouble understanding why I’m getting duplicates in the mutated list.

The node list starts off with no duplicates. The code I’m using to report duplicates is the following code:

    HashSet<Node> set = new HashSet<Node>(nodeList);

    if(set.size() < nodeList.size()){
        System.out.println("duplicates found Set:" + set.size() + " vs. NodeList:" + nodeList.size());
    }

Thank you for any assistance.

  • 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-22T23:33:40+00:00Added an answer on May 22, 2026 at 11:33 pm

    What the code does now is rotate the copy by 1. Then at some points, you swap items from the copy and the original at the same index. Then after a swap, you have two equal elements next to each other. For example, a b c d and rotated copy d a b c:

    abcd
     ▲
     swap here
     ▼
    dabc
    

    Gives

    aacd
    dbbc
    

    Every swap will give a duplicate. The code rotates the array and makes duplicates.

    From your comment it sounds like you’re using Hill climbing for something close to the Traveling Salesman. But, if you want GA, you need a lot of ArrayList<Node> mutator or working paths, and randomly combine those into new ones.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want use html5's new tag to play a wav file (currently only supported
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.