Menu

[Solved]Need Code Solve Using Hill Climbing Algorithm Graph Class Graph Class Import Javautilarray Q37112038

I need a code to solve this using a hill climbing algorithm inmy graph class

Graph Class

*******

import java.util.ArrayList;
import java.util.Scanner;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class RectGraph {

   ArrayList<Node> nodes = newArrayList<Node>();
   ArrayList<Edge> edges = newArrayList<Edge>(); //indices of attached nodes
   int numNodes = 0;
   int numEdges = 0;
   int topIndex = 0;
   int iter = 0;
  
   public RectGraph (String filename) {
       try {
           Scanner reader =new Scanner (new BufferedReader (new FileReader (filename)));
          
           this.numNodes =Integer.parseInt(reader.next().trim());
           this.numEdges =Integer.parseInt(reader.next().trim());
          System.out.println(“Numnodes, numedges ” + numNodes + ” ” +numEdges);
           topIndex = (int)(Math.sqrt(numNodes) + 1);
          
           //ASSIGN NODEID’S and INITIAL POSTIONS ON GRID
           int k = 0;
           for (int i = 0;i < topIndex; i++) {
              for (int j = 0; j < topIndex; j++) {
                 
                  if (k < numNodes) {
                     nodes.add(new Node(i, j, k));
                      k++;
                  }
              }
           }
          
           //ADD EDGES TOLIST AND TO NODES
           for (int i = 0;i < numEdges; i++) {
              String s1 = “” ;
              String s2 = “” ;
              if (reader.hasNext()) {
                  s1 = reader.next();
              }
              if (reader.hasNext()) {
                  s2 =reader.next();              }
          
              //System.out.println(s1 + ” ” + s2);
              int n1 = Integer.parseInt(s1.trim());
              int n2 = Integer.parseInt(s2.trim());
             
              Edge e = new Edge (nodes.get(n1), nodes.get(n2),i);
             
              if (!edges.contains(e)) { // avoidduplicates
                  edges.add(e);
                 
                  nodes.get(n1).addEdge(e); //add same edge to both nodes
                 nodes.get(n2).addEdge(e);
              }
           }
   }
       catch (Exception ex) {
       System.out.println(“RectGraphConstruction failed with error” + ex.toString());
       ex.printStackTrace();
       }
   }

/**
* Swap, making a greedy decision to decrease cost
*/

//this is where i have a problem, i need to change this to ahill climbing algorithm
public void swapAlgorithm(int i) {
  
   int n1 = (int) (Math.random() * nodes.size());
   int n2 = (int) (Math.random() * nodes.size());
  
   int oldCost = graphCost();
  
   swap(n1, n2);
  
   int newCost = graphCost();
   int diff = newCost – oldCost;
  
   if (newCost > oldCost) {
       swap(n1, n2);
   }
   else {
       iter++;
   }
}

public int getIter () {
   return iter; //number of non-swap
}

/**
* Swap the location of two nodes
*/
public void swap(int n1, int n2) {
   int tempx = nodes.get(n1).x;
   int tempy = nodes.get(n1).y;
  
   nodes.get(n1).setLocation(nodes.get(n2).x,nodes.get(n2).y);
   nodes.get(n2).setLocation(tempx, tempy);
}

/**
*Add up distance cost for all the distances in the graph
*
* @return
*/
public int graphCost() {
   int cost = 0;
   for (Edge e : edges) {
       cost += e.distance;
   }
   return cost;
}

public void printAdjacencies () {
   for (Node n1 : nodes) {
       n1.printAdjacency();
   }
}
}

———————

Driver class

*******

public class GraphDriver {

   public static void main (String[] args ) {
       String filename = “newfile”;
       RectGraph g = newRectGraph(filename);
       int it = 3000000;
       System.out.println(“Graph cost ” +g.graphCost());
       for (int i = 0; i < it; i++){
          g.swapAlgorithm(i);
       }
       System.out.println(“Graph cost ” +g.graphCost());
       System.out.println(“Number ofincrease swaps” + g.getIter());
   }
}

———————————–

Node class

******

import java.util.ArrayList;

public class Node {
  
   int x; // x location
   int y; // y location
   int id; // unique id for node
  
   ArrayList<Edge> attachedEdges = new ArrayList<Edge> ();
  
   public Node (int x, int y, int id) {
       this.x = x;
       this.y = y;
       this.id = id;
      
   }
  
   public void addEdge (Edge edge) {
       attachedEdges.add(edge);
   }
   public int cost() {
       int total = 0;
       for (Edge e : attachedEdges){
           total +=e.distance;
       }
       return total;
   }

   public void setLocation (int x, int y) {
       this.x = x;
       this.y = y;
       for (Edge e : attachedEdges){
           e.updateDistance();
       }
   }
  
   public void printAdjacency () {
       System.out.println(“Adjacency for “+ this.id);
       for (int i = 0; i <attachedEdges.size(); i++) {
          System.out.println(“node: ” + attachedEdges.get(i).n1.id + “-” +attachedEdges.get(i).n2.id+ ” distance: ” +attachedEdges.get(i).distance);
       }
       System.out.println(“Total cost for” + id + “: ” + cost());
   }
}

——————————————————————————

Edge class

***********

public class Edge {
   Node n1;
   Node n2;
   int distance;
   int id;
  
   public Edge (Node n1, Node n2, int id) {
       this.n1 = n1;
       this.n2 = n2;
       this.id = id;
      
       distance = Math.abs(n1.x -n2.x)
              + Math.abs(n1.y – n2.y);
   }
  
   public void updateDistance() {
       distance = Math.abs(n1.x -n2.x)
              + Math.abs(n1.y – n2.y);
   }
  
   public boolean equals(Object obj) {
       if (obj instanceof Edge) {
           Edge e = (Edge)obj;
           if ((e.n1 == n1&& e.n2 == n2) || (e.n2 == n1 && e.n1== n2)){
              return true;
           }
       }
       return false;
   }

}

Expert Answer


Answer to I need a code to solve this using a hill climbing algorithm in my graph class Graph Class ******* import java.util.Array… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *