I'm attempting to create a set of lineups for fictional fantasy teams using the reproducible data and code below. The constraint I'm attempting to implement is one that limits the number of players facing an opposing pitcher to exactly 0 in a given solution (we'll call this a bvp
constraint). My constraint matrix works when I set this constraint to <2 or <3 - meaning it produces solutions where there are 1 or two players facing an opposing pitcher. However, when I set the constraint to <1, a solution is found but it does not meet the constraint as there are solutions that contain 1 player vs an opposing pitcher.
There's a similar question in the link below but the solution posted doesn't contain any code to work with so I attempted to recreate the suggestion in the answer provided.
lpSolve - "Must Not Be Together" Constraint?
library(tidyverse)
library(lpSolve)
## Reproducible data:
lpdf <- structure(list(Player = structure(c(2L, 168L, 37L, 166L, 123L,
141L, 101L, 97L, 172L, 57L, 109L, 74L, 142L, 66L, 184L, 138L,
17L, 72L, 104L, 169L, 108L, 69L, 12L, 51L, 144L, 115L, 188L,
188L, 33L, 26L, 100L, 100L, 1L, 34L, 34L, 43L, 103L, 67L, 67L,
61L, 73L, 180L, 199L, 176L, 193L, 192L, 192L, 192L, 82L, 190L,
190L, 128L, 119L, 10L, 117L, 117L, 197L, 111L, 167L, 174L, 16L,
155L, 171L, 68L, 68L, 183L, 183L, 189L, 189L, 23L, 158L, 29L,
152L, 152L, 7L, 7L, 99L, 191L, 107L, 107L, 107L, 48L, 85L, 36L,
173L, 160L, 110L, 133L, 135L, 135L, 50L, 21L, 11L, 11L, 112L,
79L, 194L, 194L, 194L, 161L, 161L, 6L, 127L, 127L, 87L, 87L,
162L, 54L, 54L, 54L, 92L, 195L, 195L, 46L, 46L, 175L, 185L, 154L,
14L, 13L, 13L, 170L, 143L, 143L, 64L, 24L, 24L, 18L, 18L, 120L,
15L, 49L, 156L, 156L, 116L, 56L, 56L, 56L, 56L, 93L, 93L, 38L,
38L, 134L, 134L, 113L, 25L, 25L, 114L, 114L, 121L, 121L, 122L,
78L, 78L, 78L, 130L, 47L, 47L, 102L, 102L, 102L, 198L, 164L,
96L, 88L, 88L, 65L, 196L, 196L, 136L, 136L, 5L, 118L, 86L, 86L,
86L, 63L, 63L, 181L, 71L, 71L, 80L, 187L, 187L, 131L, 91L, 125L,
125L, 22L, 147L, 147L, 27L, 89L, 146L, 146L, 3L, 75L, 83L, 8L,
151L, 151L, 151L, 132L, 132L, 132L, 44L, 165L, 145L, 105L, 105L,
177L, 177L, 58L, 62L, 30L, 30L, 41L, 124L, 39L, 39L, 70L, 70L,
52L, 98L, 98L, 126L, 126L, 126L, 106L, 157L, 182L, 182L, 137L,
149L, 163L, 163L, 153L, 139L, 139L, 60L, 53L, 53L, 59L, 45L,
45L, 20L, 31L, 150L, 150L, 90L, 90L, 90L, 35L, 35L, 95L, 95L,
84L, 84L, 179L, 129L, 55L, 4L, 4L, 186L, 76L, 76L, 94L, 42L,
32L, 159L, 81L, 28L, 28L, 40L, 40L, 140L, 140L, 19L, 148L, 77L,
77L, 178L, 178L, 9L), .Label = c("Aaron Judge", "Aaron Nola",
"Adam Engel", "Adam Frazier", "Adolis Garcia", "AJ Pollock",
"Alejandro Kirk", "Alex Bregman", "Alex Kirilloff", "Alex Verdugo",
"Amed Rosario", "Andre Pallante", "Andres Gimenez", "Andrew Benintendi",
"Andrew McCutchen", "Andrew Vaughn", "Angel Zerpa", "Anthony Rizzo",
"Anthony Santander", "Austin Hays", "Austin Riley", "Austin Slater",
"Bo Bichette", "Bobby Dalbec", "Brandon Belt", "Brandon Lowe",
"Brandon Nimmo", "Brendan Donovan", "Brendan Rodgers", "Brian Serven",
"Bryan Shaw", "Buddy Kennedy", "Byron Buxton", "C.J. Cron", "Cal Raleigh",
"Carlos Correa", "Carlos Rodon", "Carlos Santana", "Carson Kelly",
"Cavan Biggio", "Cedric Mullins", "Cesar Hernandez", "Charlie Blackmon",
"Chas McCormick", "Christian Bethancourt", "Christian Vazquez",
"Christian Walker", "Christian Yelich", "Cody Bellinger", "Corey Seager",
"Dane Dunning", "Dansby Swanson", "Darick Hall", "Darin Ruf",
"David Villar", "DJ LeMahieu", "Dylan Bundy", "Dylan Carlson",
"Eddie Rosario", "Eduardo Escobar", "Eloy Jimenez", "Emmanuel Rivera",
"Eric Hosmer", "Eugenio Suarez", "Francisco Lindor", "Frankie Montas",
"Freddie Freeman", "Garrett Hampson", "Garrett Hill", "Gary Sanchez",
"Gavin Lux", "George Kirby", "George Springer", "German Marquez",
"Gleyber Torres", "Ha-Seong Kim", "Harold Castro", "Hunter Dozier",
"Hunter Renfroe", "Isaac Paredes", "J.D. Davis", "J.D. Martinez",
"J.P. Crawford", "J.T. Realmuto", "Jake Cronenworth", "Jake Lamb",
"Jared Walsh", "Javier Baez", "Jaylin Davis", "Jeimer Candelario",
"Jeremy Pena", "Jesse Winker", "Ji-Man Choi", "Jo Adell", "Jonah Heim",
"Jordan Luplow", "Jordan Montgomery", "Jorge Alfaro", "Jorge Polanco",
"Jose Abreu", "Jose Berrios", "Jose Miranda", "Jose Ramirez",
"Jose Suarez", "Josh Bell", "Josh Donaldson", "Josh Naylor",
"Josh Winckowski", "Josiah Gray", "Juan Soto", "Julio Rodriguez",
"Jurickson Profar", "Ketel Marte", "Kevin Plawecki", "Kirk McCarty",
"Kolten Wong", "Kris Bryant", "Kyle Lewis", "Kyle Schwarber",
"Kyle Tucker", "Leury Garcia", "Lourdes Gurriel Jr.", "Luis Garcia",
"Luis Gonzalez", "Luis Rengifo", "Luis Urias", "Luke Voit", "Manny Machado",
"Marcell Ozuna", "Marcus Semien", "Mark Canha", "Matt Carpenter",
"Matt Chapman", "Matt Olson", "Max Muncy", "Max Stassi", "Michael A. Taylor",
"Michael Kopech", "Miguel Cabrera", "Miguel Sano", "Mike Clevinger",
"Mitch White", "MJ Melendez", "Mookie Betts", "Myles Straw",
"Nathaniel Lowe", "Nick Pratto", "Nolan Gorman", "Nomar Mazara",
"Omar Narvaez", "Owen Miller", "Pete Alonso", "Ramon Laureano",
"Randal Grichuk", "Randy Arozarena", "Rhys Hoskins", "Riley Greene",
"Rob Refsnyder", "Roman Quinn", "Ronald Acuna Jr.", "Rowdy Tellez",
"Ryan McMahon", "Ryan Mountcastle", "Sam Hilliard", "Santiago Espinal",
"Shane McClanahan", "Shohei Ohtani", "Spencer Strider", "Spenser Watkins",
"Starling Marte", "Steven Kwan", "Taijuan Walker", "Taylor Ward",
"Teoscar Hernandez", "Thairo Estrada", "Tim Anderson", "Tommy Edman",
"Travis d'Arnaud", "Trayce Thompson", "Trea Turner", "Trent Grisham",
"Trey Mancini", "Ty France", "Tyler Gilbert", "Tyler O'Neill",
"Victor Robles", "Vinnie Pasquantino", "Vladimir Guerrero Jr.",
"Whit Merrifield", "Will Smith", "Willy Adames", "Wilmer Flores",
"Xander Bogaerts", "Yandy Diaz", "Yasmani Grandal", "Yermin Mercedes",
"Yoan Moncada", "Yolmer Sanchez", "Yordan Alvarez"), class = "factor"),
Pos = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 5L,
6L, 1L, 4L, 5L, 2L, 1L, 4L, 5L, 1L, 4L, 5L, 3L, 1L, 4L, 5L,
5L, 7L, 5L, 7L, 7L, 1L, 3L, 4L, 5L, 4L, 1L, 3L, 5L, 5L, 5L,
3L, 3L, 5L, 5L, 5L, 5L, 5L, 5L, 2L, 5L, 1L, 4L, 2L, 5L, 7L,
2L, 2L, 1L, 4L, 4L, 1L, 2L, 7L, 1L, 5L, 4L, 5L, 2L, 7L, 5L,
5L, 5L, 3L, 2L, 3L, 7L, 3L, 7L, 5L, 5L, 5L, 1L, 3L, 4L, 1L,
4L, 5L, 1L, 4L, 1L, 4L, 3L, 1L, 5L, 4L, 5L, 4L, 1L, 4L, 1L,
2L, 5L, 5L, 5L, 2L, 7L, 5L, 4L, 1L, 3L, 1L, 4L, 1L, 4L, 5L,
5L, 5L, 1L, 4L, 2L, 2L, 1L, 3L, 4L, 1L, 4L, 1L, 4L, 1L, 4L,
2L, 1L, 4L, 4L, 1L, 2L, 7L, 5L, 1L, 5L, 4L, 2L, 1L, 4L, 1L,
3L, 4L, 2L, 5L, 5L, 7L, 2L, 7L, 4L, 1L, 4L, 1L, 5L, 5L, 1L,
3L, 4L, 1L, 4L, 5L, 2L, 5L, 3L, 1L, 4L, 5L, 7L, 2L, 7L, 5L,
1L, 4L, 5L, 5L, 1L, 4L, 5L, 2L, 7L, 3L, 1L, 2L, 4L, 1L, 3L,
4L, 5L, 2L, 5L, 1L, 4L, 2L, 7L, 5L, 3L, 4L, 1L, 5L, 5L, 4L,
1L, 4L, 1L, 7L, 4L, 1L, 3L, 7L, 2L, 3L, 5L, 1L, 4L, 5L, 5L,
1L, 4L, 5L, 1L, 4L, 3L, 1L, 4L, 5L, 4L, 1L, 5L, 6L, 4L, 1L,
1L, 3L, 4L, 4L, 1L, 4L, 1L, 4L, 1L, 5L, 5L, 3L, 2L, 5L, 5L,
7L, 3L, 5L, 2L, 3L, 5L, 3L, 2L, 7L, 2L, 3L, 1L, 4L, 5L, 2L,
2L, 3L, 4L, 1L, 5L), .Label = c("1B", "2B", "3B", "C", "OF",
"P", "SS"), class = "factor"), Tm = structure(c(18L, 2L,
21L, 23L, 9L, 19L, 25L, 16L, 15L, 14L, 26L, 6L, 12L, 17L,
1L, 7L, 10L, 20L, 11L, 3L, 4L, 8L, 22L, 24L, 12L, 5L, 25L,
25L, 14L, 23L, 7L, 7L, 16L, 6L, 6L, 6L, 5L, 12L, 12L, 7L,
25L, 12L, 9L, 7L, 4L, 21L, 21L, 21L, 4L, 12L, 12L, 19L, 18L,
4L, 6L, 6L, 7L, 20L, 11L, 25L, 7L, 23L, 5L, 6L, 6L, 20L,
20L, 10L, 10L, 25L, 4L, 6L, 15L, 15L, 25L, 25L, 14L, 13L,
5L, 5L, 5L, 13L, 19L, 14L, 11L, 2L, 26L, 25L, 12L, 12L, 24L,
2L, 5L, 5L, 19L, 13L, 23L, 23L, 23L, 13L, 13L, 7L, 19L, 19L,
11L, 11L, 6L, 21L, 21L, 21L, 20L, 7L, 7L, 4L, 4L, 21L, 22L,
6L, 10L, 5L, 5L, 15L, 10L, 10L, 20L, 4L, 4L, 16L, 16L, 9L,
13L, 12L, 18L, 18L, 13L, 16L, 16L, 16L, 16L, 23L, 23L, 20L,
20L, 2L, 2L, 1L, 21L, 21L, 4L, 4L, 7L, 7L, 25L, 10L, 10L,
10L, 24L, 1L, 1L, 14L, 14L, 14L, 4L, 6L, 1L, 8L, 8L, 15L,
21L, 21L, 11L, 11L, 24L, 20L, 12L, 12L, 12L, 19L, 19L, 19L,
12L, 12L, 23L, 10L, 10L, 15L, 9L, 11L, 11L, 21L, 10L, 10L,
15L, 4L, 24L, 24L, 7L, 16L, 20L, 9L, 5L, 5L, 5L, 16L, 16L,
16L, 9L, 25L, 5L, 26L, 26L, 22L, 22L, 22L, 10L, 6L, 6L, 3L,
21L, 1L, 1L, 14L, 14L, 2L, 19L, 19L, 13L, 13L, 13L, 16L,
8L, 3L, 3L, 10L, 19L, 3L, 3L, 17L, 8L, 8L, 15L, 18L, 18L,
2L, 23L, 23L, 3L, 5L, 13L, 13L, 8L, 8L, 8L, 20L, 20L, 24L,
24L, 18L, 18L, 12L, 2L, 21L, 20L, 20L, 26L, 19L, 19L, 11L,
26L, 1L, 23L, 15L, 22L, 22L, 25L, 25L, 14L, 14L, 3L, 22L,
8L, 8L, 2L, 2L, 14L), .Label = c("ARI", "ATL", "BAL", "BOS",
"CLE", "COL", "CWS", "DET", "HOU", "KC", "LAA", "LAD", "MIL",
"MIN", "NYM", "NYY", "OAK", "PHI", "SD", "SEA", "SF", "STL",
"TB", "TEX", "TOR", "WSH"), class = "factor"), Opp = c("ATL",
"PHI", "ARI", "BAL", "OAK", "DET", "STL", "NYM", "NYY", "MIL",
"LAD", "CWS", "WSH", "HOU", "SF", "COL", "LAA", "TEX", "KC",
"TB", "CLE", "SD", "TOR", "SEA", "WSH", "BOS", "STL", "STL",
"MIL", "BAL", "COL", "COL", "NYM", "CWS", "CWS", "CWS", "BOS",
"WSH", "WSH", "COL", "STL", "WSH", "OAK", "COL", "CLE", "ARI",
"ARI", "ARI", "CLE", "WSH", "WSH", "DET", "ATL", "CLE", "CWS",
"CWS", "COL", "TEX", "KC", "STL", "COL", "BAL", "BOS", "CWS",
"CWS", "TEX", "TEX", "LAA", "LAA", "STL", "CLE", "CWS", "NYY",
"NYY", "STL", "STL", "MIL", "MIN", "BOS", "BOS", "BOS", "MIN",
"DET", "MIL", "KC", "PHI", "LAD", "STL", "WSH", "WSH", "SEA",
"PHI", "BOS", "BOS", "DET", "MIN", "BAL", "BAL", "BAL", "MIN",
"MIN", "COL", "DET", "DET", "KC", "KC", "CWS", "ARI", "ARI",
"ARI", "TEX", "COL", "COL", "CLE", "CLE", "ARI", "TOR", "CWS",
"LAA", "BOS", "BOS", "NYY", "LAA", "LAA", "TEX", "CLE", "CLE",
"NYM", "NYM", "OAK", "MIN", "WSH", "ATL", "ATL", "MIN", "NYM",
"NYM", "NYM", "NYM", "BAL", "BAL", "TEX", "TEX", "PHI", "PHI",
"SF", "ARI", "ARI", "CLE", "CLE", "COL", "COL", "STL", "LAA",
"LAA", "LAA", "SEA", "SF", "SF", "MIL", "MIL", "MIL", "CLE",
"CWS", "SF", "SD", "SD", "NYY", "ARI", "ARI", "KC", "KC",
"SEA", "TEX", "WSH", "WSH", "WSH", "DET", "DET", "DET", "WSH",
"WSH", "BAL", "LAA", "LAA", "NYY", "OAK", "KC", "KC", "ARI",
"LAA", "LAA", "NYY", "CLE", "SEA", "SEA", "COL", "NYM", "TEX",
"OAK", "BOS", "BOS", "BOS", "NYM", "NYM", "NYM", "OAK", "STL",
"BOS", "LAD", "LAD", "TOR", "TOR", "TOR", "LAA", "CWS", "CWS",
"TB", "ARI", "SF", "SF", "MIL", "MIL", "PHI", "DET", "DET",
"MIN", "MIN", "MIN", "NYM", "SD", "TB", "TB", "LAA", "DET",
"TB", "TB", "HOU", "SD", "SD", "NYY", "ATL", "ATL", "PHI",
"BAL", "BAL", "TB", "BOS", "MIN", "MIN", "SD", "SD", "SD",
"TEX", "TEX", "SEA", "SEA", "ATL", "ATL", "WSH", "PHI", "ARI",
"TEX", "TEX", "LAD", "DET", "DET", "KC", "LAD", "SF", "BAL",
"NYY", "TOR", "TOR", "STL", "STL", "MIL", "MIL", "TB", "TOR",
"SD", "SD", "PHI", "PHI", "MIL"), Order = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 2, 2, 1, 2, 4, 4, 2, 4, 4, 1, 3, 3, 3, 5, 1, 2, 3,
1, 3, 2, 2, 2, 4, 4, 4, 3, 1, 2, 3, 3, 2, 1, 1, 5, 3, 3,
1, 2, 2, 2, 2, 1, 1, 4, 1, 5, 4, 4, 3, 3, 3, 2, 4, 4, 4,
1, 2, 2, 2, 1, 3, 7, 6, 6, 2, 4, 2, 2, 1, 6, 1, 1, 1, 3,
3, 7, 5, 5, 5, 5, 6, 3, 3, 3, 5, 6, 6, 5, 5, 5, 3, 7, 2,
6, 6, 2, 3, 3, 4, 8, 8, 3, 3, 5, 4, 8, 2, 2, 5, 1, 1, 1,
1, 4, 4, 3, 3, 3, 3, 3, 6, 6, 6, 6, 8, 8, 6, 4, 4, 4, 1,
4, 4, 5, 5, 5, 7, 8, 2, 2, 2, 3, 4, 4, 4, 4, 4, 6, 5, 5,
5, 6, 6, 8, 7, 7, 5, 5, 5, 5, 2, 3, 3, 1, 7, 7, 1, 9, 5,
5, 9, 4, 7, 4, 5, 5, 5, 5, 5, 5, 1, 8, 9, 4, 4, 1, 1, 2,
6, 9, 9, 1, 9, 1, 1, 8, 8, 2, 7, 7, 7, 7, 7, 6, 1, 2, 2,
8, 4, 3, 3, 2, 4, 4, 6, 4, 4, 6, 7, 7, 5, 0, 8, 8, 5, 5,
5, 8, 8, 3, 3, 3, 3, 9, 7, 7, 9, 9, 1, 9, 9, 6, 2, 5, 8,
8, 5, 5, 9, 9, 9, 9, 4, 6, 3, 3, 5, 5, 7), Salary = c(10600L,
9100L, 10800L, 11600L, 9500L, 8100L, 8900L, 8700L, 9400L,
7500L, 9000L, 7200L, 6400L, 9800L, 6400L, 7700L, 6900L, 8600L,
6600L, 6800L, 8000L, 6500L, 6000L, 7800L, 4200L, 5700L, 3900L,
3900L, 3700L, 3000L, 4100L, 4100L, 4700L, 4500L, 4500L, 4100L,
4200L, 4300L, 4300L, 2700L, 3800L, 4100L, 4500L, 3900L, 3000L,
2800L, 2800L, 2800L, 3300L, 3000L, 3000L, 4000L, 3500L, 2400L,
3800L, 3800L, 3400L, 3900L, 3900L, 3600L, 3800L, 3200L, 3100L,
2500L, 2500L, 3600L, 3600L, 2700L, 2700L, 3400L, 2500L, 3600L,
4000L, 4000L, 3300L, 3300L, 3400L, 3900L, 3200L, 3200L, 3200L,
3100L, 3600L, 2900L, 2900L, 4100L, 3900L, 3100L, 2400L, 2400L,
3600L, 4200L, 3400L, 3400L, 2700L, 3600L, 2900L, 2900L, 2900L,
2800L, 2800L, 2700L, 3100L, 3100L, 2500L, 2500L, 3000L, 2600L,
2600L, 2600L, 2300L, 3000L, 3000L, 2700L, 2700L, 2500L, 2700L,
2800L, 2900L, 3200L, 3200L, 3500L, 2600L, 2600L, 3000L, 2200L,
2200L, 3600L, 3600L, 3700L, 3000L, 2800L, 2900L, 2900L, 2500L,
3200L, 3200L, 3200L, 3200L, 2600L, 2600L, 2600L, 2600L, 3900L,
3900L, 3600L, 2400L, 2400L, 2000L, 2000L, 2600L, 2600L, 3000L,
2600L, 2600L, 2600L, 3200L, 3000L, 3000L, 2300L, 2300L, 2300L,
2100L, 2500L, 2400L, 2600L, 2600L, 3600L, 2300L, 2300L, 2200L,
2200L, 3500L, 2200L, 2200L, 2200L, 2200L, 2400L, 2400L, 2500L,
2600L, 2600L, 2600L, 2200L, 2200L, 2300L, 2900L, 2200L, 2200L,
2500L, 2000L, 2000L, 2900L, 2000L, 2800L, 2800L, 3100L, 3300L,
2900L, 3000L, 2200L, 2200L, 2200L, 4200L, 4200L, 4200L, 2300L,
2500L, 2600L, 3100L, 3100L, 3300L, 3300L, 2800L, 2100L, 2500L,
2500L, 3300L, 2300L, 2900L, 2900L, 2300L, 2300L, 3700L, 2500L,
2500L, 2600L, 2600L, 2600L, 2700L, 2500L, 2500L, 2500L, 2400L,
2200L, 2800L, 2800L, 3100L, 2300L, 2300L, 2400L, 2900L, 2900L,
2300L, 2000L, 2000L, 3000L, 5500L, 2100L, 2100L, 2300L, 2300L,
2300L, 2300L, 2300L, 2600L, 2600L, 3000L, 3000L, 2200L, 3000L,
2300L, 2500L, 2500L, 2100L, 2200L, 2200L, 2100L, 2300L, 2300L,
2000L, 2200L, 2500L, 2500L, 2400L, 2400L, 2200L, 2200L, 3300L,
2600L, 2200L, 2200L, 2900L, 2900L, 2200L), Proj = c(35.91,
35.83, 34.36, 33.54, 32.98, 32.49, 31.36, 26.92, 26.45, 26.2,
25.6, 25.58, 24.85, 24.02, 23.78, 23.64, 22.65, 22.3, 21.9,
21.61, 20.15, 20, 17.81, 17.76, 13.32, 13.23, 12.94, 12.94,
12.92, 12.83, 12.73, 12.73, 12.71, 12.62, 12.62, 12.36, 12.35,
12.35, 12.35, 12.32, 12.3, 12.28, 12.25, 12.24, 12.19, 12.13,
12.13, 12.13, 12.1, 12.04, 12.04, 11.95, 11.69, 11.69, 11.69,
11.69, 11.65, 11.62, 11.51, 11.33, 11.32, 11.28, 11.12, 11.09,
11.09, 11.08, 11.08, 11.07, 11.07, 11.06, 11.03, 11.02, 11.02,
11.02, 10.99, 10.99, 10.93, 10.88, 10.88, 10.88, 10.88, 10.87,
10.86, 10.83, 10.77, 10.75, 10.7, 10.68, 10.68, 10.68, 10.65,
10.61, 10.54, 10.54, 10.53, 10.51, 10.51, 10.51, 10.51, 10.51,
10.51, 10.51, 10.5, 10.5, 10.47, 10.47, 10.47, 10.44, 10.44,
10.44, 10.43, 10.42, 10.42, 10.38, 10.38, 10.36, 10.3, 10.27,
10.27, 10.26, 10.26, 10.22, 10.22, 10.22, 10.2, 10.17, 10.17,
10.15, 10.15, 10.14, 10.13, 10.12, 10.1, 10.1, 10.06, 10.06,
10.06, 10.06, 10.06, 9.97, 9.97, 9.91, 9.91, 9.9, 9.9, 9.89,
9.88, 9.88, 9.88, 9.88, 9.84, 9.84, 9.77, 9.77, 9.77, 9.77,
9.75, 9.73, 9.73, 9.68, 9.68, 9.68, 9.65, 9.63, 9.58, 9.57,
9.57, 9.56, 9.54, 9.54, 9.49, 9.49, 9.49, 9.48, 9.47, 9.47,
9.47, 9.47, 9.47, 9.45, 9.43, 9.43, 9.39, 9.39, 9.39, 9.37,
9.33, 9.25, 9.25, 9.19, 9.19, 9.19, 9.13, 9.12, 9.11, 9.11,
9.08, 9.07, 9.06, 9.06, 9.04, 9.04, 9.04, 8.93, 8.93, 8.93,
8.89, 8.87, 8.86, 8.86, 8.86, 8.81, 8.81, 8.8, 8.79, 8.78,
8.78, 8.77, 8.74, 8.72, 8.72, 8.72, 8.72, 8.71, 8.69, 8.69,
8.68, 8.68, 8.68, 8.63, 8.62, 8.6, 8.6, 8.55, 8.55, 8.54,
8.54, 8.52, 8.51, 8.51, 8.47, 8.47, 8.47, 8.47, 8.43, 8.43,
8.43, 8.42, 8.42, 8.42, 8.42, 8.42, 8.42, 8.37, 8.37, 8.36,
8.36, 8.36, 8.36, 8.34, 8.34, 8.31, 8.31, 8.31, 8.29, 8.27,
8.27, 8.22, 8.22, 8.22, 8.19, 8.18, 8.16, 8.16, 8.15, 8.15,
8.15, 8.15, 8.1, 8.09, 8.05, 8.05, 8.02, 8.02, 8.01), vsPitcher = structure(c(33L,
2L, 36L, 34L, 14L, 15L, 3L, 35L, 19L, 6L, 29L, 27L, 23L,
25L, 8L, 17L, 21L, 9L, 5L, 32L, 24L, 28L, 20L, 16L, 23L,
22L, 3L, 3L, 6L, 34L, 17L, 17L, 35L, 27L, 27L, 27L, 22L,
23L, 23L, 17L, 3L, 23L, 14L, 17L, 24L, 36L, 36L, 36L, 24L,
23L, 23L, 15L, 33L, 24L, 27L, 27L, 17L, 9L, 5L, 3L, 17L,
34L, 22L, 27L, 27L, 9L, 9L, 21L, 21L, 3L, 24L, 27L, 19L,
19L, 3L, 3L, 6L, 10L, 22L, 22L, 22L, 10L, 15L, 6L, 5L, 2L,
29L, 3L, 23L, 23L, 16L, 2L, 22L, 22L, 15L, 10L, 34L, 34L,
34L, 10L, 10L, 17L, 15L, 15L, 5L, 5L, 27L, 36L, 36L, 36L,
9L, 17L, 17L, 24L, 24L, 36L, 20L, 27L, 21L, 22L, 22L, 19L,
21L, 21L, 9L, 24L, 24L, 35L, 35L, 14L, 10L, 23L, 33L, 33L,
10L, 35L, 35L, 35L, 35L, 34L, 34L, 9L, 9L, 2L, 2L, 8L, 36L,
36L, 24L, 24L, 17L, 17L, 3L, 21L, 21L, 21L, 16L, 8L, 8L,
6L, 6L, 6L, 24L, 27L, 8L, 28L, 28L, 19L, 36L, 36L, 5L, 5L,
16L, 9L, 23L, 23L, 23L, 15L, 15L, 15L, 23L, 23L, 34L, 21L,
21L, 19L, 14L, 5L, 5L, 36L, 21L, 21L, 19L, 24L, 16L, 16L,
17L, 35L, 9L, 14L, 22L, 22L, 22L, 35L, 35L, 35L, 14L, 3L,
22L, 29L, 29L, 20L, 20L, 20L, 21L, 27L, 27L, 32L, 36L, 8L,
8L, 6L, 6L, 2L, 15L, 15L, 10L, 10L, 10L, 35L, 28L, 32L, 32L,
21L, 15L, 32L, 32L, 25L, 28L, 28L, 19L, 33L, 33L, 2L, 34L,
34L, 32L, 22L, 10L, 10L, 28L, 28L, 28L, 9L, 9L, 16L, 16L,
33L, 33L, 23L, 2L, 36L, 9L, 9L, 29L, 15L, 15L, 5L, 29L, 8L,
34L, 19L, 20L, 20L, 3L, 3L, 6L, 6L, 32L, 20L, 28L, 28L, 2L,
2L, 6L), .Label = c("A.J. Puk", "Aaron Nola", "Andre Pallante",
"Andres Munoz", "Angel Zerpa", "Brad Boxberger", "Bryan Shaw",
"Carlos Rodon", "Dane Dunning", "Dylan Bundy", "Dylan Coleman",
"Erik Swanson", "Felix Bautista", "Frankie Montas", "Garrett Hill",
"George Kirby", "German Marquez", "Hector Neris", "Jordan Montgomery",
"Jose Berrios", "Jose Suarez", "Josh Winckowski", "Josiah Gray",
"Kirk McCarty", "Luis Garcia", "Matt Bush", "Michael Kopech",
"Mike Clevinger", "Mitch White", "Ryan Borucki", "Sam Moll",
"Shane McClanahan", "Spencer Strider", "Spenser Watkins",
"Taijuan Walker", "Tyler Gilbert", "Zach Jackson"), class = "factor")), row.names = c(1L,
2L, 3L, 4L, 6L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
20L, 21L, 22L, 23L, 25L, 27L, 28L, 30L, 31L, 32L, 36L, 37L, 38L,
39L, 41L, 42L, 44L, 45L, 47L, 48L, 49L, 51L, 52L, 53L, 54L, 56L,
57L, 58L, 59L, 63L, 64L, 66L, 67L, 68L, 70L, 72L, 74L, 75L, 76L,
77L, 79L, 80L, 81L, 82L, 84L, 86L, 87L, 88L, 90L, 91L, 92L, 93L,
94L, 99L, 100L, 101L, 102L, 104L, 105L, 106L, 108L, 110L, 111L,
112L, 113L, 114L, 115L, 117L, 118L, 119L, 120L, 122L, 123L, 124L,
125L, 126L, 127L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L,
144L, 145L, 147L, 148L, 149L, 151L, 152L, 157L, 158L, 159L, 160L,
162L, 164L, 166L, 167L, 169L, 173L, 174L, 175L, 176L, 177L, 178L,
179L, 180L, 182L, 183L, 185L, 186L, 191L, 192L, 194L, 198L, 199L,
200L, 201L, 203L, 204L, 205L, 206L, 207L, 209L, 210L, 215L, 216L,
221L, 222L, 224L, 225L, 226L, 228L, 230L, 234L, 235L, 236L, 237L,
238L, 239L, 241L, 245L, 246L, 248L, 249L, 250L, 252L, 254L, 255L,
256L, 257L, 258L, 259L, 261L, 262L, 264L, 268L, 272L, 274L, 275L,
276L, 278L, 279L, 281L, 282L, 283L, 284L, 286L, 287L, 289L, 290L,
294L, 295L, 298L, 299L, 300L, 302L, 303L, 305L, 306L, 317L, 318L,
319L, 321L, 325L, 326L, 327L, 329L, 330L, 331L, 333L, 337L, 338L,
339L, 340L, 342L, 343L, 344L, 345L, 346L, 348L, 349L, 350L, 351L,
353L, 354L, 356L, 357L, 358L, 360L, 361L, 362L, 363L, 364L, 365L,
366L, 367L, 369L, 370L, 371L, 372L, 374L, 376L, 377L, 379L, 380L,
381L, 383L, 384L, 386L, 390L, 391L, 392L, 394L, 395L, 396L, 397L,
399L, 401L, 405L, 407L, 417L, 419L, 420L, 421L, 422L, 423L, 424L,
427L, 428L, 429L, 430L, 432L, 433L, 434L, 436L, 437L, 438L, 439L,
440L, 441L, 442L, 444L, 445L, 446L, 447L, 448L, 450L, 451L), class = "data.frame")
## Reproducible code:
solutions <- vector("list", length = 100)
for (z in seq_along(solutions))
{
dir <- c('=', #P
'=', #C/1B
'=', #util
'=', #2b
'=', #3b
'=', #SS
'=', #OF
'<=', #max salary
'>=', #min salary
'=') #max players
rhs <- c(1, #P
1, #C/1b
1, #util
1, #2b
1, #3b
1, #SS
3, #OF
35000, #max salary
34000, #min salary
9) #max players
sim_data <- lpdf$Proj + rnorm(n = nrow(lpdf), mean = 20, sd = 10)
df <- cbind(lpdf, sim_data, 1)
cols <- c("Pos","Player","vsPitcher","Salary","1")
mm <- cbind(
model.matrix(
object = ~ .,
data = df[,cols],
contrasts.arg = lapply(subset(df[,cols], select = c(Pos,Player,vsPitcher)), contrasts, contrasts=FALSE)
)
)[,-1]
mm <- as.data.frame.matrix(mm)
mm$SalaryMin <- mm$Salary
colnames(mm)[ncol(mm)-1] <- "pCount"
mm <- mm[,c( grep("PosP",colnames(mm)),
grep("PosC",colnames(mm)),
grep("Pos1B",colnames(mm)),
grep("Pos2B",colnames(mm)),
grep("Pos3B",colnames(mm)),
grep("PosSS",colnames(mm)),
grep("PosOF",colnames(mm)),
grep("Salary",colnames(mm)),
grep("pCount",colnames(mm)),
grep("Player",colnames(mm)),
grep("vsPitcher",colnames(mm)))]
## unique player constraint
numtimes <- length(grep("Player",colnames(mm)))
dir <- c(dir, rep('<=', times = numtimes))
rhs <- c(rhs, rep(1, times = numtimes))
## bvp constraint
numtimes <- length(grep("vsPitcher",colnames(mm)))
dir <- c(dir, rep('<', times = numtimes))
rhs <- c(rhs, rep(1, times = numtimes)) ## <1 does not produce the correct solution; <2 or <3 produces correct solution
mm <- t(mm)
obj <- df$sim_data
lp <- lp(direction = 'max',
objective.in = obj,
all.bin = TRUE,
const.rhs = rhs,
const.dir = dir,
const.mat = mm)
lp
df$Tm <- as.character(df$Tm)
df$sol <- lp$solution
df <- df[df$sol==1,]
df$oppPitcher <- df[df$Pos=="P","Opp"]
df$bvp <- NA
df$bvp <- ifelse(df$Tm==df$oppPitcher, 1, 0)
df$id <- z
solutions[[z]] <- df[,c(1:5,12:14)]
}
## if bvp>=1 solutions contains a batter(s) v pitcher, if bvp=0 solutions do not contain batter(s) v pitcher (desired outcome)
do.call(rbind, solutions) %>%
dplyr::group_by(id) %>%
dplyr::summarise(bvp = sum(bvp)) %>%
dplyr::ungroup(.) %>%
dplyr::arrange(desc(bvp))