Question

How can I get started in this program for this DelivC?


Specification

Start with your Java program "prog340" which implements Deliverables A and B.

This assignment is based on the definition of the Traveling Salesperson Problem (the TSP): Given a set of cities, you want to find the shortest route that visits every city and ends up back at the original starting city. For the purposes of this problem, every city will be directly reachable from every other city (think flying from city to city).


Your goal is to use a non-genetic local search algorithm or algorithms from Chapter 4 of Poole \& Mackworth to find the shortest paths that you can. Note that for any reasonably sized problem, you will not be able to try every possibility, and so you won't ever know when you have the shortest possible path.

You should have two types of output: Summary Mode and Verbose Mode.

Verbose Mode should print to a file every path you try until your program terminates. Summary mode, which prints to the console, should print a path only when that path is better than any path that you have found previously. In all cases, print the single-letter mnemonic of the city (Node), not its full name.


Suggestions

You can start from any city you want, it really doesn't matter which. For example, you can start from the first city in the file, since it is the easiest. Create some tour. You can do this randomly, or you can use a greedy algorithm of some sort, or you can just visit the cities in the order they appear in the file, whatever you prefer. Your choice probably won't affect the goodness of your final tour, but it might affect how long it takes you to get to a good solution.

Selecting and using the techniques of local search, try to find a better tour. Be sure to document what you're trying to do.

The rest of this section assumes you want to create your own algorithm. Here are some thoughts on what is probably the main question: how to step from one possible tour to the next.

- One option would be two swap the order of two cities and see if this shortens the tour Q Mpls. \(\rightarrow\) Seattle \(\rightarrow\) Detroit \(\rightarrow\) Boston \(\rightarrow\) Chicago \(\rightarrow\) Miami \(\rightarrow\) Denver \(\rightarrow\) Mpls.

- Or you could pick one city in the tour, and try removing it and inserting it between every pair of cities to see which gives the shortest tour.

- There are other forms of Iterative Best Improvement, too.

- When deciding what cities to swap or what city to shuffle, you could use either a 1-stage or 2-stage choice algorithm.

With any sort of iterative best improvement, there are various techniques you could use if you wish. One of the big differences among peoples" algorithms will be their choices to use or not use these techniques, and the algorithm they use to decide when to use them (for example, how often to do a random step or restart, temperatures for simulated annealing, etc.)

- You could choose to use Simulated Annealing to decide whether to keep a given tour even if it's longer than the existing tour.

- You could inject randomness by sometimes making random permutations (random walk).

- You could decide after a certain number of iterations that you should just start over from scratch with a random order of cities (random restart).

- You could choose to keep a tabu list or not, and if you do, you could choose its size.

- You could keep multiple tours, and permute them in parallel (beam search).

Since you don't actually know when you have an optimal tour for any reasonably sized problem, you need to decide on a stopping criterion. Some possible suggestions:

- Stop after some number of steps (where the number of steps might be some function of the number \(n\) of cities).

- Stop after you have gone some number \(k\) of steps without improving on your best answer.

- Stop after the program has run for some amount of real-time (measured by something like System.time.millis()).

Program1.pngProgram2.png

//Graph Class:
import java.util.ArrayList;
//Graph is a class whose objects represent graphs.
 public class Graph {
    ArrayList nodeList;
    ArrayList edgeList;
   
    public Graph() {
        nodeList = new ArrayList();
        edgeList = new ArrayList();
   
    }
   
    public ArrayList getNodeList() {
        return nodeList;
   }
   public ArrayList getEdgeList() {
        return edgeList;
   }
   public void addNode(Node n) {
        nodeList.add(n);
   }
   public void addEdge(Edge e) {
        edgeList.add(e);
   }
   public String toString() {
        String s = "Graph g.\n";
        if (nodeList.size() > 0) {
            for (Node n : nodeList) {
        // Print node info
        String t = "\nNode " + n.getName() + ", abbrev " + n.getAbbrev() + ", value " + n.getVal() + "\n";
        s = s.concat(t);
        }
        s = s.concat("\n");
            }
        return s;
    }
 }

// Node Class:
import java.util.ArrayList;
 // Node is a class whose objects represent nodes (a.k.a., vertices) in the graph.
 public class Node {
   String name;
    String val; // The value of the Node
    String abbrev; // The abbreviation for the Node
    ArrayList outgoingEdges;
    ArrayList incomingEdges;
   
   
    String color; //Create the color of the TYPE Node List
    int start; //Create the Starting Time
    int end; //Create the Ending Time
   
   
    public Node( String theAbbrev ) {
        setAbbrev( theAbbrev );
        val = null;
        name = null;
        outgoingEdges = new ArrayList();
        incomingEdges = new ArrayList();
    }
   
    public String getAbbrev() {
        return abbrev;
    }
   
    public String getName() {
        return name;
    }
   
    public String getVal() {
        return val;
    }
   
    public ArrayList getOutgoingEdges() {
        return outgoingEdges;
    }
   
    public ArrayList getIncomingEdges() {
        return incomingEdges;
    }
   
    public void setAbbrev( String theAbbrev ) {
        abbrev = theAbbrev;
    }
   
    public void setName( String theName ) {
        name = theName;
    }
   
    public void setVal( String theVal ) {
        val = theVal;
    }
   
    public void addOutgoingEdge( Edge e ) {
        outgoingEdges.add( e );
    }
   
    public void addIncomingEdge( Edge e ) {
        incomingEdges.add( e );
    }
   
   
    //Create the Starting Time of the Node
    public int getStartTime() {
        return start;
    }
    //Create the Finshing Time of the Node
    public int getFinishTime() {
        return end;
    }
   public void setColor(String string) {
        color = string;
    }
   public String getColor() {
        return color;
    }
    //Set Starting-Time
    public void setStartTime(int time) {
        start = time;
       
    }
    //Set Finishing-Time
    public void setFinishTime(int time) {
        end = time;
 }
//Edge Class:
//import java.util.*;
// Edge between two nodes
 // Edge is a class whose objects represent edges (a.k.a., arcs) between nodes of the graph.
 public class Edge {
   
    String label;
    int dest;
    int dist;
    Node tail;
    Node head;
   
   //String Type(Forward,Backward,Cross,Tree)
    String name;
   
    public Edge( Node tailNode, Node headNode, String theLabel ) {
        setLabel( theLabel );
        setTail( tailNode );
        setHead( headNode );
    }
   
    public Edge(int dest, int dist,String label,String name)
    {
        //Create the destination Vertex Index
        this.dest = dest;
        this.dist = dist; // Source Vertex index
        this.label=label; // Label of Edge
        this.name= name; // (Locate the Type of Line(F,B,C,T)
        }
   
    public String getLabel() {
        return label;
    }
   
    public Node getTail() {
        return tail;
    }
   
    public Node getHead() {
        return head;
    }
   
    public int getDist() {
        return dist;
    }
   
    public void setLabel( String s ) {
        label = s;
    }
   
    public void setTail( Node n ) {
        tail = n;
    }
   
    public void setHead( Node n ) {
        head = n;
    }
   
    public void setDist( String s ) {
        try {
            dist = Integer.parseInt( s );
        }
        catch ( NumberFormatException nfe ) {
            dist = Integer.MAX_VALUE;
        }
    }
    public String getType() {
        return name;
    }
   public void setType(String string) {
        this.name = string;
    }
 }
//NEED HELP ON IMPLEMENT ON DELIVC CLASS
//DelivC Class (DelivC Need to be implemented) :
import java.io.File;
 import java.io.PrintWriter;
// Class DelivC does the work for deliverable DelivC of the Prog340
public class DelivC {
   File inputFile;
    File outputFile;
    PrintWriter output;
    Graph g;
   
    public DelivC( File in, Graph gr ) {
        inputFile = in;
        g = gr;
       
        // Get output file name.
        String inputFileName = inputFile.toString();
        String baseFileName = inputFileName.substring( 0, inputFileName.length()-4 ); // Strip off ".txt"
        String outputFileName = baseFileName.concat( "_out.txt" );
        outputFile = new File( outputFileName );
        if ( outputFile.exists() ) { // For retests
            outputFile.delete();
        }
       
        try {
            output = new PrintWriter(outputFile);          
        }
        catch (Exception x ) {
            System.err.format("Exception: %s%n", x);
            System.exit(0);
        }
        System.out.println( "DelivC: To be implemented");
        output.println( "DelivC: To be implemented");
        output.flush();
    }
 }


0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
How can I get started in this program for this DelivC?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • How can I solved my Java Program for DelivC

    //Graph Class: import java.util.ArrayList; //Graph is a class whose objects represent graphs.  public class Graph {     ArrayList<Node> nodeList;     ArrayList<Edge> edgeList;         public Graph() {         nodeList = new ArrayList<Node>();         edgeList = new ArrayList<Edge>();         }         public ArrayList<Node> getNodeList() {         return nodeList;    }    public ArrayList<Edge> getEdgeList() {         return edgeList;    }    public void addNode(Node n) {         nodeList.add(n);    }    public void addEdge(Edge e) {         edgeList.add(e);    }    public String toString() {         String s = "Graph g.\n";         if (nodeList.size() > 0) {             for (Node n : nodeList) {         // Print node info         String t = "\nNode " + n.getName() + ", abbrev " + n.getAbbrev() + ", value " + n.getVal() + "\n";         s = s.concat(t);         }         s = s.concat("\n");             }         return s;     }  } // Node Class: import java.util.ArrayList;  // Node is a class whose objects represent nodes (a.k.a., vertices) in the graph.  public class Node {    String name;     String val; // The value of the Node     String abbrev; // The abbreviation for the Node     ArrayList<Edge> outgoingEdges;     ArrayList<Edge> incomingEdges;             String color; //Create the color of the TYPE Node List     int start; //Create the Starting Time     int end; //Create the Ending Time             public Node( String theAbbrev ) {         setAbbrev( theAbbrev );         val = null;         name = null;         outgoingEdges = new ArrayList<Edge>();         incomingEdges = new ArrayList<Edge>();     }         public String getAbbrev() {         return abbrev;     }         public String getName() {         return name;     }         public String getVal() {         return val;     }         public ArrayList<Edge> getOutgoingEdges() {         return outgoingEdges;     }         public ArrayList<Edge> getIncomingEdges() {         return incomingEdges;...

  • How to solve my code for my Deliv C program?

    ProgramIntro.pngProgramIntro2.pngProgramIntro1.pngProgram1.pngProgram2.pngGraph Class:import java.util.ArrayList;//Graph is a class whose objects represent graphs. public class Graph {    ArrayList<Node> nodeList;    ArrayList<Edge> edgeList;       public Graph() {        nodeList = new ArrayList<Node>();        edgeList = new ArrayList<Edge>();       }       public ArrayList<Node> getNodeList() {        return nodeList;   }   public ArrayList<Edge> getEdgeList() {        return edgeList;   }   public void addNode(Node n) {        nodeList.add(n);   }   public void addEdge(Edge e) {        edgeList.add(e);   }   public String...

  • How to solve and code the following requirements (below) using the JAVA program? 1. Modify the...

    How to solve and code the following requirements (below) using the JAVA program? 1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface. 2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface. 3. Create main class to read products from a file, instantiate them, load them into...

  • For this assignment, you will write a program to work with Huffman encoding. Huffman code is...

    For this assignment, you will write a program to work with Huffman encoding. Huffman code is an optimal prefix code, which means no code is the prefix of another code. Most of the code is included. You will need to extend the code to complete three additional methods. In particular, code to actually build the Huffman tree is provided. It uses a data file containing the frequency of occurrence of characters. You will write the following three methods in the...

  • Can someone help me with my code.. I cant get an output. It says I do...

    Can someone help me with my code.. I cant get an output. It says I do not have a main method. HELP PLEASE. The instruction are in bold on the bottom of the code. package SteppingStones; //Denisse.Carbo import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class SteppingStone5_Recipe { private String recipeName; private int servings; private List<String> recipeIngredients; private double totalRecipeCalories; public String getRecipeName() { return recipeName; } public void setRecipeName (string recipeName){ this.recipeName = recipeName; } public int getServings() { return...

  • Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the...

    Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root). 3. A traverse function, that iterates the list and prints the elements in the linked list. For the insertAtBeginning(Node newNode) function: 1. Check if the root is null. If it is, just assign the new...

  • Can someone provide codes in C for the following questions: Write C programs grah.h and graph.c...

    Can someone provide codes in C for the following questions: Write C programs grah.h and graph.c to implement adjacent list graph representation and operation functions. Test with q1.c. graph.h #ifndef GRAPH_H #define GRAPH_H #include <stdio.h> #include <stdlib.h> #define INFINITY 99999 typedef struct adjnode { int vertex; int weight; struct adjnode *next; } ADJNODE; typedef struct graph { int order; //number of nodes int size; //number of edges ADJNODE **adjlist; //pointer to an array of pointers of neighbors } GRAPH; /*...

  • JAVA: How do I output all the data included for each employee? I can only get...

    JAVA: How do I output all the data included for each employee? I can only get it to output the name, monthly salary and annual salary, but only from the Employee.java file, not Salesman.java or Executive.java. Employee.java package project1; public class Employee { private String name; private int monthlySalary; public Employee(String name, int monthlySalary) { this.name = name; this.monthlySalary = monthlySalary; } public int getAnnualSalary() { int totalPay = 0; totalPay = 12 * monthlySalary; return totalPay; } public String...

  • The names of the two input files as well as the output file are supposed to be provided as arguments. Could you please fix it? Thanks. DPriorityQueue.java: // Import the required classes import java....

    The names of the two input files as well as the output file are supposed to be provided as arguments. Could you please fix it? Thanks. DPriorityQueue.java: // Import the required classes import java.io.*; import java.util.*; //Create the class public class DPriorityQueue { //Declare the private members variables. private int type1,type2; private String CostInTime[][], SVertex, DVertex; private List<String> listOfTheNodes; private Set<String> List; private List<Root> ListOfVisitedNode; private HashMap<String, Integer> minimalDistance; private HashMap<String, Integer> distOfVertices; private PriorityQueue<City> priorityQueue; // prove the definition...

  • This is my current output for my program. I am trying to get the output to...

    This is my current output for my program. I am trying to get the output to look like This is my program Student.java import java.awt.GridLayout; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.swing.JFileChooser; public class Student extends javax.swing.JFrame {     BufferedWriter outWriter;     StudentA s[];     public Student() {         StudentGUI();            }     private void StudentGUI() {         jScrollPane3 = new javax.swing.JScrollPane();         inputFileChooser = new javax.swing.JButton();         outputFileChooser = new javax.swing.JButton();         sortFirtsName = new...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT