Question: #### Review this Rscript provided for the following question. If there is any inconsistency or incomplete part, fix it . ( DONT USE AI )

#### Review this Rscript provided for the following question. If there is any inconsistency or incomplete part, fix it.(DONT USE AI)
set.seed(1)
generate_reward_matrix <- function(n_cities){
v <- matrix(0, nrow = n_cities, ncol = n_cities)
upper <- runif(n_cities *(n_cities -1)/2)
v[upper.tri(v)]<- upper
v[lower.tri(v)]<- t(v)[lower.tri(v)]
diag(v)<-0
return(v)
}
total_reward <- function(x, v){
sum(v[cbind(c(0, x[-length(x)]), x)])
}
n_iterations <-10000
temperature <-1
n_cities <-10
v <- generate_reward_matrix(n_cities)
x <- sample(1:n_cities, n_cities)
swap_cities <- function(x){
i <- sample(1:length(x),2)
x_new <- x
x_new[i]<- x[i[2]]
x_new[i[2]]<- x[i[1]]
return(x_new)
}
itinerary <- matrix(nrow = n_iterations, ncol = length(x))
itinerary[1,]<- x
parameters <- c(0.5,1,10)
par(mfrow=c(3,1), mar=c(2,2,1,1))
for (param in parameters){
temperature <- param
current_reward <- total_reward(x, v)
for (i in 2:n_iterations){
x_new <- swap_cities(itinerary[i -1,])
new_reward <- total_reward(x_new, v)
if (exp((new_reward - current_reward)/ temperature)>= runif(1)){
itinerary[i,]<- x_new
current_reward <- new_reward
} else {
itinerary[i,]<- itinerary[i -1,]
}
}
plot(1:n_iterations, apply(itinerary,1, function(x) total_reward(x, v)), type ='l',
xlab = 'Iteration', ylab = 'Total Reward', main = paste('Parameter:', param))
avg_reward <- mean(apply(itinerary,1, function(x) total_reward(x, v)))
cat("Average reward for parameter", param, ":", avg_reward, "
")
}
temperature <- initial_temperature
for (i in 2:n_iterations){
x_new <- swap_cities(itinerary[i -1,])
new_reward <- total_reward(x_new, v)
current_reward <- total_reward(itinerary[i -1,], v)
acceptance_prob <- exp((new_reward - current_reward)/ temperature)
if (runif(1)< acceptance_prob){
itinerary[i,]<- x_new
} else {
itinerary[i,]<- itinerary[i -1,]
}
temperature <- temperature *0.99 # Adjust the cooling rate as needed
}
plot(1:n_iterations, apply(itinerary,1, function(x) total_reward(x, v)),
type ='l', xlab = 'Iteration', ylab = 'Total Reward',
main = 'Simulated Annealing with Dynamic Temperature')
avg_reward <- mean(apply(itinerary,1, function(x) total_reward(x, v)))
cat("Average reward with simulated annealing:", avg_reward, "
")
Foodys CEO needs to visit cities 1,2,..., r with city 0 being the city she is currently located at. Suppose a non-negative reward v(i, j) is associated with the CEO going from city i to city j. So if the CEO visits the cities in permutation x1,..., xr then the reward of this choice x =(x1,..., xr) is:
V (x)=(sum i=1 to r) v(xi1, xi),
where x0=0. Note that there is no reward for coming back to city 0. To generate v(i, j), set seed to 1 and generate v(i, j) using Uniform(0,1) random variables starting from i =0 and j =1,...,10,then v(1, j) for j =2,...,10, v(2, j) for j =1,3,4,...,10 and so on.
(i) Use MCMC and r =10 to simulate high reward itineraries for the CEO. Define the stationary distribution that we want our Markov Chain to converge in such a way that high valued solutions are given extremely high probability and include a tunable parameter. Find 3 values of this parameter where the generated Markov Chain behaves differently in the long run for each of these 3 values and demonstrate this using plots and averages.
(ii) If you could change this parameter during one simulation of the stationary distribution, how would you change it in order to improve the solutions. Demonstrate that you can get better solutions than the fixed 3 values that you used in (i) by running it in R. Present all your results.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!