代写代考 title: “Optimal Route LP”

title: “Optimal Route LP”
output: html_document

“`{r setup, include=FALSE}

Copyright By PowCoder代写 加微信 powcoder

knitr::opts_chunk$set(echo = TRUE)

library(lpSolveAPI)
library(dplyr)

Robin works for a major utility company. Robin’s territory stretches over the southwestern US between Southern California and Northern Texas. One of the job responsibilities is performing monthly checks of the substations along the major routes that traverse Robin’s territory. Robin varies the route each trip, in order to check different substations.

The number of transformers in between the major cities along the potential routes are shown in red on the network schematic below. For each trip, Robin leaves from Los Angeles and arrives in Amarillo, TX. Robin would like to identify the route between these two cities that maximizes the number of substations along the way. The route can only travel in the direction of the arrow on the schematic (no back-tracking, etc.)

data <- read.csv(file.choose()) data$route <- 1:nrow(data) ```{r include=FALSE} model <- make.lp(0, nrow(data)) lp.control(model, sense = "max") all_nodes <- unique(c(data$Start_Node, data$End_Node)) Nodes <- data.frame(node = all_nodes) Nodes$Required_Flow <- 0 Nodes[Nodes$node == min(Nodes$node), 'Required_Flow'] <- -1 Nodes[Nodes$node == max(Nodes$node), 'Required_Flow'] <- 1 out_routes <- data[data$Start_Node == 4, 'route'] in_routes <- data[data$End_Node == 4, 'route'] out_routes c(rep(1, length(in_routes)), rep(-1, length(out_routes))) c(in_routes, out_routes) for (n in Nodes$node){ out_routes <- data[data$Start_Node == n, 'route'] in_routes <- data[data$End_Node == n, 'route'] add.constraint(model, c(rep(1, length(in_routes)), rep(-1, length(out_routes))), type = '=', rhs = Nodes[Nodes$node == n, 'Required_Flow'], indices = c(in_routes, out_routes)) write.lp(model, filename = 'model.lp', 'lp') set.objfn(model, data$Substations) set.type(model, c(1:nrow(data)), "binary") dimnames(model)[[2]] <- data$route write.lp(model, "model.lp", "lp") solve(model) total_substations <- get.objective(model) total_substations opt_route <- get.variables(model) names(opt_route) <- data$route data_opt <- data data_opt$select_route <- get.variables(model) data_opt[data_opt$select_route == 1, ] 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com