(*OPTIMIZATIONFUNCTION*)(*maximize=truemeansfindmaxrank,falsemeansminrank*)OptimizeRank[k_Integer,maximize_:True]:=Module[{wG,wS,scores,zVars,M=10^6,epsilon=1,obj,vars,cons,solution},(*Definethevariables*)wG=Symbol["wG"];(*Defineweightsassymbolic*)wS=Symbol["wS"];scores=Table[wG*medals[[i,2]]+wS*medals[[i,3]]+medals[[i,4]],{i,numCountries}];zVars=Table[If[i≠k,z[i],Sequence[]],{i,numCountries}];(*Definezvariablesforallcountriesexceptk*)(*Constraints*) cons=Join[Flatten[Table[If[i≠k,{scores[[i]]≥scores[[k]]+epsilon-M*(1-z[i]),scores[[i]]≤scores[[k]]+M*z[i]},Nothing],{i,numCountries}]],Flatten[Table[If[i≠k,{0≤z[i]≤1},Nothing],{i,numCountries}]],{1≤wS≤20,wS≤wG≤20}];(*Objectivefunction*)obj=If[maximize,-1,1]*(Total[Table[If[i≠k,z[i],0],{i,numCountries}]]+1);(*Negateformaximization,asLinearOptimizationminimizesbydefault*)vars=Join[{wG,wS},Table[If[i≠k,z[i]∈Integers,Nothing],{i,numCountries}]]; (*Solvetheoptimizationproblem*)solution=LinearOptimization[obj,cons,vars]; (*uncommentifyouwanttheactualweights*) (*{If[maximize,-1,1]*obj/.solution,solution}*) If[maximize,-1,1]*obj/.solution]