Question

In: Statistics and Probability

R exercise implement a greedy nearest-neighbour approach to find a suitable route for visiting all houses...

R exercise

implement a greedy nearest-neighbour approach to find a suitable route for visiting all houses (pairs of coordinates generated).

The route should be as short as possible.

Starting at house 1, visit the house nearest to house 1 first. Then, each time move on to the nearest house not yet visited and, after visiting all houses, return to house 1.

Suppose house 1 is at point (0,1) .

Write a function called gp which will take the matrix of coordinates of the houses as argument and will return

the coordinates of the path found by the greedy algorithm.

( You can generate a set of coordinates of the houses using the function gen.h

gen.h=function(n=10) {
while (TRUE) {
r <- sqrt(runif(n))*0.9
phi <- runif(n) * 2 * pi
houses <- cbind(x=r*sin(phi), y=r*cos(phi))
if (min(dist(houses))>0.15)
return(houses)
}
}

houses <- gen.h ()

Solutions

Expert Solution

Below is the R Code for function gp.

gen.h=function(n=10) {
while (TRUE) {
r <- sqrt(runif(n))*0.9
phi <- runif(n) * 2 * pi
houses <- cbind(x=r*sin(phi), y=r*cos(phi))
if (min(dist(houses))>0.15)
return(houses)
}
}
houses <- gen.h ()

gp <- function(houses) {
house.visited <- c() # Initialize the house.visited and path vactor
path <- c()
for (i in 1:10) {
house.visited[i] <- 0 # Initializing that all houses are not visited
}
current.house <- 1 # Initialize the current house
house.visited[current.house] <- 1 # Initialize the current house visited
distance <- as.matrix(dist(houses, upper = TRUE)) # Calculate the distance of each houses from each other
path[1] <- current.house # Initialize the path
for (k in 1:9) {
min <- Inf # Initialize next minimum distance to infinity
for (i in 1:10) {
if (house.visited[i] == 0) { # House not visited
if (min > distance[i, current.house]) { # The current min distance is greater than the distance between i and current house
min <- distance[i, current.house] # New distance is the distance between i and current house
next.house <- i # Next house is the house i
}
}
}
current.house <- next.house # Next house will be current house in next iteration
path[k+1] <- current.house # Store the next house in the the path vector
house.visited[current.house] <- 1 # Make current house as visited
}
path.coordinates <- houses[path,] # Store the path corrdinates
path.coordinates
}


Related Solutions

Assign costs to customers by using an ABC approach. Round your answers and all intermediate calculations to the nearest dollar.
Activity-Based Customer CostingSleepeze Company produces mattresses for 20 retail outlets. Of the 20 retail outlets, 19 are small, separately owned furniture stores and one is a retail chain. The retail chain buys 60% of the mattresses produced. The 19 smaller customers purchase mattresses in approximately equal quantities, where the orders are about the same size. Data concerning Sleepeze’s customer activity are as follows:Large RetailerSmaller RetailersUnits purchased108,00072,000Orders placed363,600Number of sales calls18882Manufacturing costs$43,200,000$28,800,000Order filling costs allocated*$1,636,200$1,090,800Sales force costs allocated*$756,000$504,000*Currently allocated on sales...
The equation (sqrt(x^2+y^2) - R)^2 +z^2=r^2 represents a torus. (a) Find a suitable parametrization of torus....
The equation (sqrt(x^2+y^2) - R)^2 +z^2=r^2 represents a torus. (a) Find a suitable parametrization of torus. (b) Compute surface area of torus.
Use R functions search and objects to find all possible R built-in functions related to exp...
Use R functions search and objects to find all possible R built-in functions related to exp distribution. rexp should be one of them. dexp is another one. Explain what the following two lines of codes do: curve(dexp, from=0, to=4)
Exercise 4: Find all local extrema for f ? = sin^2 ? + cos ? on...
Exercise 4: Find all local extrema for f ? = sin^2 ? + cos ? on the interval [− ?/ 2 , ?/ 2 ]. Verify your answer by graphing.
1. Consider the following game: L R L 2,1 0,0 R 1,0 3,2 a)Find all Nash...
1. Consider the following game: L R L 2,1 0,0 R 1,0 3,2 a)Find all Nash equilibria and derive the players’ expected payoffs in each of the Nash equilibria. b)Now change the payoffs slightly so that L R L 2,1 2,0 R 1,0 3,2 i.Derive all Nash equilibria for this modified game. ii.Have any of the Nash equilibria changed? If so, for each player explain why the player has or has not changed her strategy. c) Do the players in...
Find all of the ideals of Q, M2(R) (the 2 x 2 matrices with entries in...
Find all of the ideals of Q, M2(R) (the 2 x 2 matrices with entries in R) and M2(Z) (the 2 x 2 matrices with entries in Z) and determine which ideals are maximal and which ideals are prime. Please explain why the ideals are maximal and/or prime.
For exercise 10, find all solutions exactly on the interval 0 ≤ θ < 2π. 10....
For exercise 10, find all solutions exactly on the interval 0 ≤ θ < 2π. 10. cot x + 1 = 0 For the following exercises, solve exactly on [0, 2π) 13. 2cos θ = √2 16. 2sin θ = − √3 19. 2cos(3θ) = −√2 22. 2cos (π/5 θ)= √3
Using the model found in the previous exercise, find f(10) and interpret the result. Round to the nearest hundredth. For the following exercises, use this scenario: A doctor prescribes 125 milligrams..
For the following exercises, use this scenario: A doctor prescribes 125 milligrams of a therapeutic drug that decays by about 30% each hour.Using the model found in the previous exercise, find f(10) and interpret the result. Round to the nearest hundredth.
1) find all values of theta E [ 0,2 pi], where the curve r= 1- sin...
1) find all values of theta E [ 0,2 pi], where the curve r= 1- sin theta has a horizontal tangent line 2)find all values of theta E [ 0,2 pi] where the curve r=1- sin theta has a vertical tangent line 3) find the area of the region enclosed by the intersection of the curves" r= sin theta and r= cos theta
Find all second order derivatives for r(x,y)=(xy)/2x+5y. (I need most detail at the rx/ry to rxx/ryy...
Find all second order derivatives for r(x,y)=(xy)/2x+5y. (I need most detail at the rx/ry to rxx/ryy part, I can't get the top correct)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT