*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX* $ontext * Exercise (3): MY FARM Model with sets and sum *### The objective of the exercise: 1) Getting acquainted with the GAMS programming language. *### The problem: A farmer wants to maximize his profit using 200 ha of land and 10000 hours of p_labor avaip_lable. He has the option of cultivating three types of cols: wheat, barley, rapeseed and sugarbeet. The profit received and p_labor hours required for producing one ha of each crop are presented in the table below. How much of each crop does he need to cultivate in order to maximize his profit? Item Wheat Barley Rapeseed Sugarbeet Profit in Eurp/ha 253 443 284 516 Required p_labor hours/ha 25 36 27 87 *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX* $offtext sets cols / wheat barley rapeseed sugarbeet / ; Parameters p_UVAG(cols) gross margin p_lab(cols) p_labour quantity ; p_UVAG("wheat") = 253; p_UVAG("barley") = 443; p_UVAG("rapeseed") = 284; p_UVAG("sugarbeet") = 516; p_lab("wheat") = 25; p_lab("barley") = 36; p_lab("rapeseed") = 27; p_lab("sugarbeet") = 87; Variables v_obje objective function value ; Positive variable v_actLevl(cols) land area planted with crop ; Equations e_land land constraint e_labour labour constraint e_obj objective function ; e_obj .. v_obje =E= sum (cols, v_actLevl(cols)*p_UVAG(cols)); e_land .. sum (cols, v_actLevl(cols)) =L= 200; e_labour .. sum (cols, p_lab(cols)*v_actLevl(cols)) =L= 10000; Model myfarm /all/; Solve myfarm using lp maximizing v_obje; *------------------------------------------------------------------------------------ * Exercise 7: add nutrient need per crop *------------------------------------------------------------------------------------ * Define a new parameter, where the nutrient requirments are stored per crop and fertilzer type * This could also be a table but using a parameter does not require to check spaces between columns * Q7.1: Paramter can have many dimensions (domians) (try out how many dimentions are allowed!!!) Parameter p_nutrientNeedperCrop(*,cols) "kg per hectar crop need of nutrients"; p_nutrientNeedperCrop("NITF","wheat") = 178; p_nutrientNeedperCrop("PHOF","wheat") = 77; p_nutrientNeedperCrop("POTF","wheat") = 151; p_nutrientNeedperCrop("NITF","barley") = 122; p_nutrientNeedperCrop("PHOF","barley") = 77; p_nutrientNeedperCrop("POTF","barley") = 120; p_nutrientNeedperCrop("NITF","rapeseed") = 218; p_nutrientNeedperCrop("PHOF","rapeseed") = 119; p_nutrientNeedperCrop("POTF","rapeseed") = 66; p_nutrientNeedperCrop("NITF","sugarbeet") = 321; p_nutrientNeedperCrop("PHOF","sugarbeet") = 178; p_nutrientNeedperCrop("POTF","sugarbeet") = 280; * define three new equations, one for each of the nutrient to multiply the hectare with the nutrient requirements equation e_NUTNED_NITF "equation for N",e_NUTNED_PHOF "for P",e_NUTNED_POTF "for K"; variable v_Nutrientneed(*) "total fertilzer required"; e_NUTNED_NITF.. sum( cols, v_actLevl(cols) * p_nutrientNeedperCrop("NITF",cols)) =E= v_Nutrientneed("NITF"); e_NUTNED_PHOF.. sum( cols, v_actLevl(cols) * p_nutrientNeedperCrop("PHOF",cols)) =E= v_Nutrientneed("PHOF"); e_NUTNED_POTF.. sum( cols, v_actLevl(cols) * p_nutrientNeedperCrop("POTF",cols)) =E= v_Nutrientneed("POTF"); parameter p_results(*,*) "collecting the differnt results"; * Q7.2: define a new model and solve and report what is the production program and how much nitrogen in tonnes is used model myfarm_Nutrientneed /all/; solve myfarm_Nutrientneed using lp maximizing v_obje; * catch the results p_results(cols,"Nutrientneed") = v_actLevl.l(cols); p_results("NITF","Nutrientneed") = v_Nutrientneed.l("NITF"); p_results("PHOF","Nutrientneed") = v_Nutrientneed.l("PHOF"); p_results("POTF","Nutrientneed") = v_Nutrientneed.l("POTF"); * we now show the real power of the GAMS language compared to other programms * we reduce the three very simular euqations by using additional dimensions for the equations * the dimension is defined over the nutrients using a set FNUT set FNUT / NITF "Nitrogen in fertiliser" PHOF "Phospate in fertiliser [P2O5]" POTF "Potassium in fertiliser [K2O]" /; * define a similar equation equation e_NUTNED_(FNUT); e_NUTNED_(FNUT).. sum( cols, v_actLevl(cols) * p_nutrientNeedperCrop(FNUT,cols)) =E= v_Nutrientneed(FNUT); * Todo: Now solve the model and show that the results are still the same model myfarm_Nutrientneed_compact /all - e_NUTNED_NITF -e_NUTNED_PHOF - e_NUTNED_POTF/; solve myfarm_Nutrientneed_compact using lp maximizing v_obje; p_results(cols,"compact") = v_actLevl.l(cols); p_results(FNUT,"compact") = v_Nutrientneed.l(FNUT); v_Nutrientneed.up("NITF") = v_Nutrientneed.l("NITF")*0.9; solve myfarm_Nutrientneed using lp maximizing v_obje; p_results(cols,"compact_10") = v_actLevl.l(cols); p_results(FNUT,"compact_10") = v_Nutrientneed.l(FNUT); *reset the upper bound to positive infitity v_Nutrientneed.up("NITF") = +inf; display p_results; *------------------------------------------------------------------------------------ * Exercise 8: lets assume that the requirements need to fulfilled by purchasing fertilizer to a given price *------------------------------------------------------------------------------------ parameter p_fertilzer_price(FNUT); p_fertilzer_price("NITF") = 0.18; p_fertilzer_price("PHOF") = 0.25; p_fertilzer_price("POTF") = 0.11; equation e_obj_fertlizer "new objective function for including in addition the costs of mineral fertilizer"; e_obj_fertlizer .. v_obje =E= sum (cols, v_actLevl(cols)* p_UVAG(cols)) - sum(FNUT, v_Nutrientneed(FNUT) * p_fertilzer_price(FNUT)); model myfarm_fert /all - e_obj - e_NUTNED_NITF -e_NUTNED_PHOF - e_NUTNED_POTF/; solve myfarm_fert using lp maximizing v_obje; p_results(cols,"fertcost") = v_actLevl.l(cols); p_results(FNUT,"fertcost") = v_Nutrientneed.l(FNUT); display p_results;