<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Find the max type and set a constraint on it in Mathematical Optimization, Discrete-Event Simulation, and OR</title>
    <link>https://communities.sas.com/t5/Mathematical-Optimization/Find-the-max-type-and-set-a-constraint-on-it/m-p/900786#M4040</link>
    <description>&lt;P&gt;Hi. I have an optimization problem where we are selecting students from different states with the highest scores for a national competition. The objective is to select the best students aka maximize the score. The constraints are:&lt;/P&gt;&lt;P&gt;1. At least 50% of the students have to be female (weighted by score, not count)&lt;/P&gt;&lt;P&gt;2. The top state concentration &amp;lt;= 30%&amp;nbsp;(weighted by score, not count)&lt;/P&gt;&lt;P&gt;3. All other concentration &amp;lt;= 15%&amp;nbsp;(weighted by score, not count)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am pretty new to optmodel and I am struggling with constraints 2 &amp;amp;3. For #2, I am not sure how to identify the top state out of the list that will be selected by the solver and how to then put a upper bound to it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For #3, I could put the states in individual columns in the students dataset and assign them a 0 or 1, like I did for the sex column. I could then add a family of constraints like this, but&amp;nbsp;I'm not sure if that's the best way of going about it.&lt;/P&gt;&lt;P&gt;State1[s] * score[s] * inclusion[s] &amp;lt;=.15 * score[s] * inclusion[s]&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;/*sex 0 = male, 1 = female*/&lt;BR /&gt;dataset students;
	length name $32. state $2.;
	input name $ state $ score sex;
	datalines;

Ally TX 90 1
Sam VA 50 0
Pamela TX 76 1
Arnold WA 99 0&lt;BR /&gt;Nathan TX 85 0
;

dataset allStates;
	length state $2.;
	input state $;
	datalines;
AL
AK
AZ
.
.
.
WY
;&lt;BR /&gt;&lt;BR /&gt;proc optmodel;&lt;BR /&gt;&lt;BR /&gt;set &amp;lt;string&amp;gt; studentSet;&lt;BR /&gt;str stuName{studentSet};&lt;BR /&gt;str state{studentSet};&lt;BR /&gt;num score{studentSet};&lt;BR /&gt;num sex{studentSet};&lt;BR /&gt;&lt;BR /&gt;var Inclusion{studentSet} binary;&lt;BR /&gt;&lt;BR /&gt;read data students into studentSet = [stuName = name] state score sex;&lt;BR /&gt;&lt;BR /&gt;maximize totalScore = sum{s in studentSet} score[s] * Inclusion[s];&lt;BR /&gt;&lt;BR /&gt;con generCon: sum{s in studentSet} gender[s] * score[s] * Inclusion[s] &amp;gt;=.5 * sum{s in studentSet} score[s] * Inclusion[s];&lt;BR /&gt;&lt;BR /&gt;solve;&lt;BR /&gt;&lt;BR /&gt;print Inclusion;&lt;BR /&gt;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;
&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 30 Oct 2023 19:24:13 GMT</pubDate>
    <dc:creator>HiyaC</dc:creator>
    <dc:date>2023-10-30T19:24:13Z</dc:date>
    <item>
      <title>Find the max type and set a constraint on it</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Find-the-max-type-and-set-a-constraint-on-it/m-p/900786#M4040</link>
      <description>&lt;P&gt;Hi. I have an optimization problem where we are selecting students from different states with the highest scores for a national competition. The objective is to select the best students aka maximize the score. The constraints are:&lt;/P&gt;&lt;P&gt;1. At least 50% of the students have to be female (weighted by score, not count)&lt;/P&gt;&lt;P&gt;2. The top state concentration &amp;lt;= 30%&amp;nbsp;(weighted by score, not count)&lt;/P&gt;&lt;P&gt;3. All other concentration &amp;lt;= 15%&amp;nbsp;(weighted by score, not count)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am pretty new to optmodel and I am struggling with constraints 2 &amp;amp;3. For #2, I am not sure how to identify the top state out of the list that will be selected by the solver and how to then put a upper bound to it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For #3, I could put the states in individual columns in the students dataset and assign them a 0 or 1, like I did for the sex column. I could then add a family of constraints like this, but&amp;nbsp;I'm not sure if that's the best way of going about it.&lt;/P&gt;&lt;P&gt;State1[s] * score[s] * inclusion[s] &amp;lt;=.15 * score[s] * inclusion[s]&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;/*sex 0 = male, 1 = female*/&lt;BR /&gt;dataset students;
	length name $32. state $2.;
	input name $ state $ score sex;
	datalines;

Ally TX 90 1
Sam VA 50 0
Pamela TX 76 1
Arnold WA 99 0&lt;BR /&gt;Nathan TX 85 0
;

dataset allStates;
	length state $2.;
	input state $;
	datalines;
AL
AK
AZ
.
.
.
WY
;&lt;BR /&gt;&lt;BR /&gt;proc optmodel;&lt;BR /&gt;&lt;BR /&gt;set &amp;lt;string&amp;gt; studentSet;&lt;BR /&gt;str stuName{studentSet};&lt;BR /&gt;str state{studentSet};&lt;BR /&gt;num score{studentSet};&lt;BR /&gt;num sex{studentSet};&lt;BR /&gt;&lt;BR /&gt;var Inclusion{studentSet} binary;&lt;BR /&gt;&lt;BR /&gt;read data students into studentSet = [stuName = name] state score sex;&lt;BR /&gt;&lt;BR /&gt;maximize totalScore = sum{s in studentSet} score[s] * Inclusion[s];&lt;BR /&gt;&lt;BR /&gt;con generCon: sum{s in studentSet} gender[s] * score[s] * Inclusion[s] &amp;gt;=.5 * sum{s in studentSet} score[s] * Inclusion[s];&lt;BR /&gt;&lt;BR /&gt;solve;&lt;BR /&gt;&lt;BR /&gt;print Inclusion;&lt;BR /&gt;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;
&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 19:24:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Find-the-max-type-and-set-a-constraint-on-it/m-p/900786#M4040</guid>
      <dc:creator>HiyaC</dc:creator>
      <dc:date>2023-10-30T19:24:13Z</dc:date>
    </item>
    <item>
      <title>Re: Find the max type and set a constraint on it</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Find-the-max-type-and-set-a-constraint-on-it/m-p/900847#M4044</link>
      <description>&lt;P&gt;Here's a way to enforce #2, because the top state is at most 30% if and only if all states are at most 30%.&amp;nbsp; By using the SETOF operator, you can avoid separately reading all the states from a data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   set STATES = setof {s in studentSet} state[s];
   con stateCon {st in STATES}:
      sum{s in studentSet: state[s] = st} score[s] * Inclusion[s] &amp;lt;= 0.3 * sum{s in studentSet} score[s] * Inclusion[s];
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Alternatively, you can reuse totalScore:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   set STATES = setof {s in studentSet} state[s];
   con stateCon {st in STATES}:
      sum{s in studentSet: state[s] = st} score[s] * Inclusion[s] &amp;lt;= 0.3 * totalScore;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 31 Oct 2023 02:22:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Find-the-max-type-and-set-a-constraint-on-it/m-p/900847#M4044</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2023-10-31T02:22:45Z</dc:date>
    </item>
    <item>
      <title>Re: Find the max type and set a constraint on it</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Find-the-max-type-and-set-a-constraint-on-it/m-p/900848#M4045</link>
      <description>&lt;P&gt;Hi Rob, thank you so much for your reply! that is very helpful in solving #2. However, now I'm not sure how to solve #2 and #3 together, because now all states might be lower than 30% but several might still be higher than 15%. Is there any way to add a constraint that says only 1 state is allowed to be 30%?&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 02:45:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Find-the-max-type-and-set-a-constraint-on-it/m-p/900848#M4045</guid>
      <dc:creator>HiyaC</dc:creator>
      <dc:date>2023-10-31T02:45:04Z</dc:date>
    </item>
    <item>
      <title>Re: Find the max type and set a constraint on it</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Find-the-max-type-and-set-a-constraint-on-it/m-p/900909#M4046</link>
      <description>&lt;P&gt;To handle #2 and #3 together, you can introduce new variables and constraints as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   var IsTopState {STATES} binary;
   con OneTopState:
      sum {st in STATES} IsTopState[st] = 1;
   con stateCon {st in STATES}:
      sum{s in studentSet: state[s] = st} score[s] * Inclusion[s]
   &amp;lt;= (0.15 + 0.15 * IsTopState[st]) * sum{s in studentSet} score[s] * Inclusion[s];
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The new stateCon constraint is nonlinear but can be automatically linearized with SAS Viya:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   solve linearize;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To manually linearize instead, you can use the following statements:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   var IsTopStateAndInclusion {STATES, studentSet} binary;
   con Linearize1 {st in STATES, s in studentSet}:
      IsTopStateAndInclusion[st,s] &amp;lt;= IsTopState[st];
   con Linearize2 {st in STATES, s in studentSet}:
      IsTopStateAndInclusion[st,s] &amp;lt;= Inclusion[s];
   con stateCon {st in STATES}:
      sum{s in studentSet: state[s] = st} score[s] * Inclusion[s]
   &amp;lt;= 0.15 * sum{s in studentSet} score[s] * (Inclusion[s] + IsTopStateAndInclusion[st,s]);

   solve;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 31 Oct 2023 13:36:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Find-the-max-type-and-set-a-constraint-on-it/m-p/900909#M4046</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2023-10-31T13:36:48Z</dc:date>
    </item>
  </channel>
</rss>

