Cargamos datos
library(XML)
#help(package="XML")
#Ir a googlwe docs y salvar en html
tabla=readHTMLTable(doc="/home/ricardo/Desktop/carpeta escritorio/docencia1516/ModelosII/AmigoInvisible/GrafAmigoInvisible.html",stringsAsFactors = FALSE)[[1]]
str(tabla)
## 'data.frame': 10 obs. of 10 variables:
## $ V1 : chr "" "Ricardo" "Biel" "Mercè" ...
## $ V2 : chr "Ricardo" "" "x" "" ...
## $ V3 : chr "Biel" "x" "" "xx" ...
## $ V4 : chr "Mercè" "xxx" "" "" ...
## $ V5 : chr "Arnau" "xx" "" "" ...
## $ V6 : chr "Joan C." "" "x" "" ...
## $ V7 : chr "Jairo" "xx" "xxx" "" ...
## $ V8 : chr "Cesc" "" "" "xx" ...
## $ V9 : chr "Lucia" "" "xxx" "xx" ...
## $ V10: chr "Receptors" "" "" "" ...
class(tabla)
## [1] "data.frame"
dimnames(tabla)
## [[1]]
## [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
##
## [[2]]
## [1] "V1" "V2" "V3" "V4" "V5" "V6" "V7" "V8" "V9" "V10"
Construimos al matriz de pesos
M=tabla[c(-1,-10),c(-1,-10)]
dimnames(M)=list(tabla$V1[c(-1,-10)],tabla$V1[c(-1,-10)])
M=apply(M,c(1,2),nchar)
M
## Ricardo Biel Mercè Arnau Joan C. Jairo Cesc Lucia
## Ricardo 0 1 3 2 0 2 0 0
## Biel 1 0 0 0 1 3 0 3
## Mercè 0 2 0 0 0 0 2 2
## Arnau 2 1 0 0 1 0 1 1
## Joan C. 1 1 1 0 0 0 1 1
## Jairo 1 1 0 1 2 0 3 0
## Cesc 0 2 1 3 1 0 0 0
## Lucia 0 2 0 1 2 3 0 0
Pintamos el grafo
library(igraph)
##
## Attaching package: 'igraph'
##
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
##
## The following object is masked from 'package:base':
##
## union
GM=graph.adjacency(M)
plot(GM,layout=layout_(GM,as_star()))
Aplicamos el algoritmo hungaro y la solución es…
library(clue)
library(knitr)
solM=solve_LSAP(M,maximum = TRUE)
str(solM)
## Class 'solve_LSAP' num [1:8] 3 6 2 1 8 7 4 5
sum(M[cbind(seq_along(solM), solM)])
## [1] 19
kable(data.frame(Regalador=rownames(M),Receptor=rownames(M)[solM[]]))
| Regalador | Receptor |
|---|---|
| Ricardo | Mercè |
| Biel | Jairo |
| Mercè | Biel |
| Arnau | Ricardo |
| Joan C. | Lucia |
| Jairo | Cesc |
| Cesc | Arnau |
| Lucia | Joan C. |
La solución será optima pero no me la creo (de hecho creo que es justo al revés), buscad el error.