Please i need help with these question, thanks.
4.) What is Total Revenue and How is it calculated (formula)? MacBook Air
Solution
Total revenue is the sum of the revenue generated by selling each unit of good. It is the revenues
which are generated by selling given level of input.
Total revenue can be computed by adding up Marginal revenue generated from selling each
additional unit of good.
Therefore, the Total revenue = MR1+MR2+MR3+MR4+.... MRn.
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Please implement in Java. comments would be appreciated 5.pdf
1. Please implement in Java. comments would be appreciated 5. Implement Algorithm 6.2 on your
system and run it on the problem instance of Exercise 1.
Solution
Branch and Bound:-
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
publicclass BranchandBound
{
staticint[][] wt; // Matrix of edge
// weights
static String[] city; // Vector of city
// names
staticint n; // Dimension for wt
// and city
static ArrayList soln = new ArrayList();
staticint bestTour; // Initialized in
// init()
staticint blocked; // Ditto
staticboolean DEBUG = true; // Show
// accept/reject
// decisions
staticboolean VERBOSE = true; // Show all tours
// discovered
2. @SuppressWarnings("rawtypes")
privatestaticclass Tour implements Comparable
{
int[] soln;
int index; // In branch-and-bound, start of variable
int dist;
staticint nTours = 0;
// Best-first based on dist, or DFS based on maxheap of index
staticboolean DFS = true;
staticboolean DBG = true;
/*
* Presumable edges up to [index-1] have been verified before
* this constructor has been called. So compute the fixed
* distance from [0] up to [index-1] as dist.
*/
private Tour(int[] vect, int index, int[][] wt)
{
dist = 0;
for (int k = 1; k < index; k++)
// Add edges
dist += wt[vect[k - 1]][vect[k]];
if (index == n)
dist += wt[vect[n - 1]][vect[0]]; // Return edge
soln = newint[n]; // Deep copy
System.arraycopy(vect, 0, soln, 0, n);
this.index = index; // Index to permute
nTours++; // Count up # of tours
if (DBG)
System.out.printf("Idx %d: %s ", index, toString());
}
publicint compareTo(Object o)
{
Tour rt = (Tour) o;
3. int c1 = rt.index - this.index, c2 = this.dist - rt.dist;
if (DFS)
return c1 == 0 ? c2 : c1;
else
return c2;
}
public String toString()
{
StringBuilder val = new StringBuilder(city[soln[0]]);
for (int k = 1; k < n; k++)
val.append(", " + city[soln[k]]);
val.append(", " + city[soln[0]]);
val.append(String.format(" for %d", dist));
return val.toString();
}
}
privatestaticvoid init(Scanner inp)
{
int sub1, sub2;
String line;
n = inp.nextInt();
wt = newint[n][n];
city = new String[n];
// Initially, there are NO edges; hence -1.
for (sub1 = 0; sub1 < n; sub1++)
Arrays.fill(wt[sub1], -1);
inp.nextLine(); // Discard rest of first line
for (sub1 = 0; sub1 < n; sub1++)
city[sub1] = inp.nextLine();
Arrays.sort(city); // Just to be sure (binarySearch)
inp.nextLine(); // Discard blank spacing line;
blocked = 0; // Accumulate ALL weights for upper bound
while (inp.hasNext())
{
4. int head, tail;
int dist;
String src, dst;
line = inp.nextLine(); // E.g.: "George" "Pasco" 91
// Chop out the double-quoted substrings.
head = line.indexOf('"') + 1;
tail = line.indexOf('"', head);
src = line.substring(head, tail);
head = line.indexOf('"', tail + 1) + 1;
tail = line.indexOf('"', head);
dst = line.substring(head, tail);
dist = Integer.parseInt(line.substring(tail + 1).trim());
sub1 = Arrays.binarySearch(city, src);
sub2 = Arrays.binarySearch(city, dst);
wt[sub1][sub2] = wt[sub2][sub1] = dist;
blocked += dist;
}
blocked += blocked; // Double the total
bestTour = blocked; // And initialize bestTour
}
// Used below in generating permutations.
privatestaticvoid swap(int[] x, int p, int q)
{
int tmp = x[p];
x[p] = x[q];
x[q] = tmp;
}
// Generate the available tours by branch-and-bound.
// Generate the initial permutation vector, then save that state
// as the first examined in the branch-and-bound.
publicstaticvoid tour()
{
int[] vect = newint[n];
int start;
5. Queue work = new PriorityQueue();
// First permutation vector.
for (int k = 0; k < n; k++)
vect[k] = k;
start = Arrays.binarySearch(city, "Spokane");
if (start >= 0)
{
vect[start] = 0;
vect[0] = start;
}
work.add(new Tour(vect, 1, wt));
while (!work.isEmpty()) // Branch-and-bound loop
{
Tour current = work.poll();
int index = current.index;
vect = current.soln;
if (index == n) // I.e., Full permutation vector
{
if (wt[vect[n - 1]][vect[0]] > 0) // Return edge?
{
if (current.dist < bestTour) // Better than earlier?
{// Save the state in the list
bestTour = current.dist;
soln.add(current);
if (DEBUG)
System.out.println("Accept " + current);
}
elseif (DEBUG)
System.out.println("Too long: " + current);
}
elseif (DEBUG)
System.out.println("Invalid: " + current);
}
else
// Continue generating permutations
{
6. int k; // Loop variable
int hold; // Used in regenerating the original state
for (k = index; k < n; k++)
{
swap(vect, index, k);
if (wt[vect[index - 1]][vect[index]] < 0)
continue;
work.add(new Tour(vect, index + 1, wt));
}
// Restore original permutation
hold = vect[index];
for (k = index + 1; k < n; k++)
vect[k - 1] = vect[k];
vect[n - 1] = hold;
}
}
}
@SuppressWarnings("unchecked")
publicstaticvoid main(String[] args) throws Exception
{
String filename = args.length == 0 ? "RoadSet.txt" : args[0];
Scanner inp = new Scanner(new java.io.File(filename));
System.out.println("Data read from file " + filename);
init(inp);
tour();
if (VERBOSE)
{
System.out.println("Tours discovered:");
for (Tour opt : soln)
System.out.println(opt);
}
if (soln.size() == 0)
{
System.out.println("NO tours discovered. Exiting.");
System.exit(0);
7. }
System.out.println(Tour.nTours + " Tour objects generated.");
Collections.sort(soln);
System.out.println("Best tour: ");
System.out.println(soln.get(0));
}
}
Best First Search:-
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.PriorityQueue;
import java.util.Scanner;
publicclass BestFirstSearch
{
private PriorityQueue priorityQueue;
privateint heuristicvalues[];
privateint numberOfNodes;
publicstaticfinalint MAX_VALUE = 999;
public BestFirstSearch(int numberOfNodes)
{
this.numberOfNodes = numberOfNodes;
this.priorityQueue = new PriorityQueue(this.numberOfNodes,
new Vertex());
}
publicvoid bestFirstSearch(int adjacencyMatrix[][], int[] heuristicvalues,int source)
{
int evaluationNode;
int destinationNode;
int visited[] = newint [numberOfNodes + 1];
this.heuristicvalues = heuristicvalues;
priorityQueue.add(new Vertex(source, this.heuristicvalues[source]));
8. visited[source] = 1;
while (!priorityQueue.isEmpty())
{
evaluationNode = getNodeWithMinimumHeuristicValue();
destinationNode = 1;
System.out.print(evaluationNode + "t");
while (destinationNode <= numberOfNodes)
{
Vertex vertex = new Vertex(destinationNode,this.heuristicvalues[destinationNode]);
if ((adjacencyMatrix[evaluationNode][destinationNode] != MAX_VALUE
&& evaluationNode != destinationNode)&& visited[destinationNode] == 0)
{
priorityQueue.add(vertex);
visited[destinationNode] = 1;
}
destinationNode++;
}
}
}
privateint getNodeWithMinimumHeuristicValue()
{
Vertex vertex = priorityQueue.remove();
return vertex.node;
}
publicstaticvoid main(String... arg)
{
int adjacency_matrix[][];
int number_of_vertices;
int source = 0;
int heuristicvalues[];
9. Scanner scan = new Scanner(System.in);
try
{
System.out.println("Enter the number of vertices");
number_of_vertices = scan.nextInt();
adjacency_matrix = newint[number_of_vertices + 1][number_of_vertices + 1];
heuristicvalues = newint[number_of_vertices + 1];
System.out.println("Enter the Weighted Matrix for the graph");
for (int i = 1; i <= number_of_vertices; i++)
{
for (int j = 1; j <= number_of_vertices; j++)
{
adjacency_matrix[i][j] = scan.nextInt();
if (i == j)
{
adjacency_matrix[i][j] = 0;
continue;
}
if (adjacency_matrix[i][j] == 0)
{
adjacency_matrix[i][j] = MAX_VALUE;
}
}
}
for (int i = 1; i <= number_of_vertices; i++)
{
for (int j = 1; j <= number_of_vertices; j++)
{
if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0)
{
adjacency_matrix[j][i] = 1;
}
}
}
10. System.out.println("Enter the heuristic values of the nodes");
for (int vertex = 1; vertex <= number_of_vertices; vertex++)
{
System.out.print(vertex + ".");
heuristicvalues[vertex] = scan.nextInt();
System.out.println();
}
System.out.println("Enter the source ");
source = scan.nextInt();
System.out.println("The graph is explored as follows");
BestFirstSearch bestFirstSearch = new BestFirstSearch(number_of_vertices);
bestFirstSearch.bestFirstSearch(adjacency_matrix, heuristicvalues,source);
} catch (InputMismatchException inputMismatch)
{
System.out.println("Wrong Input Format");
}
scan.close();
}
}
class Vertex implements Comparator
{
publicint heuristicvalue;
publicint node;
public Vertex(int node, int heuristicvalue)
{
this.heuristicvalue = heuristicvalue;
this.node = node;
}
public Vertex()
{