Constraint Formulation doesnt work / is not applied to solution

Hi everybody,
I have a problem with a specific constraint formulation. the problem requires to choose 4 players from a player pool (its a soccer problem) and maximizes expected points.
I want the optimizer to only pick players from a certain number of teams. For this I tried to use this:
% defining a new optimization variable
used_teams = optimvar('used_teams',length(teams),1,'Type','integer','LowerBound',0,'UpperBound',1);
% run variables
i = 1:length(teams);
t = 1:length(player_def);
%apply team info to optimization variable
soccer_lineup.Constraints.stack1 = used_teams(i) <= sum(def_team_info(i,t)*defense_lineup(t));
%set the maximum numbers of teams
def_teams = sum(used_teams);
soccer_lineup.Constraints.stack2 = def_teams <= 2;
My inspiration to code it like this came from a Github code in another language looking very similar:
The problem is, that it isnt applied to the problem. I can set the number of used teams to 0 and I still get a feasible solution (which should be impossible).(Yes the example from the other code is for more than 3 teams, but i want the optimizer to stack players from 2 teams. )
Does anyone see the mistake?
I would appreciate your help.
Kind regards,
Christian

8 commentaires

When you say that you set the number of used teams to 0 do you mean that you change the upper bound in the optimization to 0 thereby forcing the vector to be all 0?
I tried to make it impossible for the algorithm to pick an optimal lineup.
I changed this part, which should enforce that the four players are only from two or less teams
%set the maximum numbers of teams
def_teams = sum(used_teams);
soccer_lineup.Constraints.stack2 = def_teams <= 2;
to this:
%set the maximum numbers of teams
def_teams = sum(used_teams);
soccer_lineup.Constraints.stack2 = def_teams <= 0;
since every player has a team (stored in def_team_info), it should be impossible to pick an optimal with the sum(used_teams) equaling zero or less then zero.
it seems that the problem lies within the other constraint:
i = 1:length(teams);
t = 1:length(player_def);
%apply team info to optimization variable
soccer_lineup.Constraints.stack1 = used_teams(i) <= sum(def_team_info(i,t)*defense_lineup(t));
but i am unable to find the mistake.
for more information: the def_team_info looks like this:
1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 1 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 1 1 1
the defense_lineup is defined like this:
defense_lineup = optimvar('defense_lineup',length(player_def),1,'Type','integer','LowerBound',0,'UpperBound',1);
def_players_in_lineup = sum(defense_lineup);
soccer_lineup.Constraints.numberofplayers1 = def_players_in_lineup == 4;
It would help to have all your code.
Thank you so much fpr your help! Here is the code. I will try to formulate the problem I have to solve here too:
objective function: maximizing the projected player points (working)
constraints:
- staying under the budget of 100 (working)
- 1 gk, 3-5 defenders, 3-5 midfielders, 1-3 forwards. (this constraints are programmed, but I set the amount by hand at the moment, mainly for testing reasons) (working)
- overlap constaints: for every iteration at least one player has to be new, so every iteration yields a new less optimal solution (working)
what the goal is:
-formulate a team constraints to restrict the numbers of teams used in the defense, so that more defensive players from one team end up in the optimal solution. (not working)
-restrict the solution so no defensive players face opposing players offensive players. (not implemented, if the other problem is solved this should work too).
%load data
clear all
load("test_file.mat")
GW1.Properties.VariableNames{22} = 'FT';
GW1.Properties.VariableNames{25} = 'FTPRICE';
row = GW1.FTPRICE > 0;
data = GW1(row,:);
%split in offense and defense
row0 = (data.Pos == "DEF" | data.Pos == "GK") ;
defense = data(row0,:);
row1 = (data.Pos == "MID" | data.Pos == "FOR");
offense = data(row1,:);
%variables
pos_off = offense.Pos;
pos_def = defense.Pos;
player_off = offense.Name;
player_def = defense.Name;
proj_off = offense.Average;
proj_def = defense.Average;
sal_off = offense.FTPRICE;
sal_def = defense.FTPRICE;
teams = unique(data.Team);
% create position arrays
for r = 1:length(player_off)
if(pos_off(r) == "MID")
mid(r,1) = 1;
fwd(r,1) = 0;
else
mid(r,1) = 0;
fwd(r,1) = 1;
end
end
for r = 1:length(player_def)
if(pos_def(r) == "GK")
gk(r,1) = 1;
def(r,1) = 0;
else
gk(r,1) = 0;
def(r,1) = 1;
end
end
%create team arrays
for r = 1:length(player_off)
for s = 1:length(teams)
if(offense.Team(r) == teams(s))
off_team_info(s,r) = 1;
else
off_team_info(s,r) = 0;
end
end
end
for r = 1:length(player_def)
for s = 1:length(teams)
if(defense.Team(r) == teams(s))
def_team_info(s,r) = 1;
else
def_team_info(s,r) = 0;
end
end
end
%create empty arrays for more iterations
off_previous_lineups = zeros(length(player_off),1);
def_previous_lineups = zeros(length(player_def),1);
%optimization loop for n lineups
for run = 1:20
%create optimization problems
soccer_lineup = optimproblem;
%binary optimization variables
offense_lineup = optimvar('offense_lineup',length(player_off),1,'Type','integer','LowerBound',0,'UpperBound',1);
defense_lineup = optimvar('defense_lineup',length(player_def),1,'Type','integer','LowerBound',0,'UpperBound',1);
%objective function
i = 1:length(player_off);
j = 1:length(player_def);
maximize = -(sum(proj_off(i).*offense_lineup(i)) + sum(proj_def(j).*defense_lineup(j)));
soccer_lineup.Objective = maximize;
%total player constraints
off_players_in_lineup = sum(offense_lineup);
soccer_lineup.Constraints.numberofplayers = off_players_in_lineup == 7;
def_players_in_lineup = sum(defense_lineup);
soccer_lineup.Constraints.numberofplayers1 = def_players_in_lineup == 4;
% position constraints
gks = sum(gk(j).*defense_lineup(j));
defs = sum(def(j).*defense_lineup(j));
mids = sum(mid(i).*offense_lineup(i));
fwds = sum(fwd(i).*offense_lineup(i));
soccer_lineup.Constraints.goalmin = gks >= 1;
soccer_lineup.Constraints.defmin = defs >= 3;
soccer_lineup.Constraints.midmin = mids >= 3;
soccer_lineup.Constraints.fwdmin = fwds >= 1;
soccer_lineup.Constraints.goalmax = gks <= 1;
soccer_lineup.Constraints.defmax = defs <= 5;
soccer_lineup.Constraints.midmax = mids <= 5;
soccer_lineup.Constraints.fwdmax = fwds <= 3;
%financial constraint
i = 1:length(player_off);
j = 1:length(player_def);
salary_cap = sum(sal_off(i).*offense_lineup(i)) + sum(sal_def(j).*defense_lineup(j));
soccer_lineup.Constraints.financialrule = salary_cap <= 100;
%overlap constraint every new lineup must at least contain one (or more)
%new players
soccer_lineup.Constraints.overlap = (sum(offense_lineup(i).*off_previous_lineups(i)) + sum(defense_lineup(j).*def_previous_lineups(j))) <= 10;
% stacking players from 2 teams
used_teams = optimvar('used_teams',length(teams),1,'Type','integer','LowerBound',0,'UpperBound',1);
% run variables
i = 1:length(teams);
t = 1:length(player_def);
%apply team info to optimization variable
soccer_lineup.Constraints.stack1 = used_teams(i) <= sum(def_team_info(i,t)*defense_lineup(t));
%set the maximum numbers of teams
def_teams = sum(used_teams);
soccer_lineup.Constraints.stack2 = def_teams <= 2;
%solve problem
[sol,fval] = solve(soccer_lineup);
%overlap arrays
off_previous_lineups = off_previous_lineups + sol.offense_lineup;
def_previous_lineups = def_previous_lineups + sol.defense_lineup;
%create string to display all lineups
a = 1;
for i = 1:length(player_off)
if(round(sol.offense_lineup(i)) == 1)
lineups_off(a,1) = player_off(i);
a = a+1;
end
end
a = 1;
for i = 1:length(player_def)
if(round(sol.defense_lineup(i)) == 1)
lineups_deff(a,1) = player_def(i);
a = a+1;
end
end
lineups(:,run) = [lineups_off;lineups_deff;fval]
end
i send the code in another comment, but it was flagged as potential spam, i hope you can still see it
Can you see the code now?
The input file would help. There is too much code for me to read it and mentally model it all and figure out all the combinations of circumstances in an input file that could potentially lead to problems.
I attached the input file below, I hope it helps. all inputs are either strings or doubles. the team info will be put later in a binary matrix via matlab.

Connectez-vous pour commenter.

Réponses (0)

Produits

Version

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by