2using System.Collections.Generic;
3using System.ComponentModel;
8using System.Windows.Forms;
10using System.Windows.Forms.DataVisualization.Charting;
11using System.Diagnostics;
12using System.Data.OleDb;
13using System.Reflection;
22 public partial class Form1 : Form
25 private String[] periods =
new String[] {
"jan",
"feb",
"mar",
"apr",
"may",
"jun",
"jul",
"aug",
"sep",
"oct",
"nov",
"dec" };
26 private Dictionary<Tuple<String, String>,
double> landreq =
new Dictionary<Tuple<String, String>,
double>()
28 {
new Tuple<string,string> (
"wheat",
"jan"), 1 },
29 {
new Tuple<string,string> (
"wheat",
"feb"), 1 },
30 {
new Tuple<string,string> (
"wheat",
"mar"), 1 },
31 {
new Tuple<string,string> (
"wheat",
"apr"), 1 },
32 {
new Tuple<string,string> (
"wheat",
"may"), 1 },
33 {
new Tuple<string,string> (
"wheat",
"nov"), 0.5 },
34 {
new Tuple<string,string> (
"wheat",
"dec"), 1 },
36 {
new Tuple<string,string> (
"clover",
"jan"), 1 },
37 {
new Tuple<string,string> (
"clover",
"feb"), 1 },
38 {
new Tuple<string,string> (
"clover",
"mar"), 0.5 },
39 {
new Tuple<string,string> (
"clover",
"nov"), 0.25 },
40 {
new Tuple<string,string> (
"clover",
"dec"), 1 },
42 {
new Tuple<string,string> (
"beans",
"jan"), 1 },
43 {
new Tuple<string,string> (
"beans",
"feb"), 1 },
44 {
new Tuple<string,string> (
"beans",
"mar"), 1 },
45 {
new Tuple<string,string> (
"beans",
"apr"), 1 },
46 {
new Tuple<string,string> (
"beans",
"nov"), 0.25 },
47 {
new Tuple<string,string> (
"beans",
"dec"), 1 },
49 {
new Tuple<string,string> (
"onions",
"jan"), 1 },
50 {
new Tuple<string,string> (
"onions",
"feb"), 1 },
51 {
new Tuple<string,string> (
"onions",
"mar"), 1 },
52 {
new Tuple<string,string> (
"onions",
"apr"), 1 },
53 {
new Tuple<string,string> (
"onions",
"may"), 0.25 },
54 {
new Tuple<string,string> (
"onions",
"nov"), 0.5 },
55 {
new Tuple<string,string> (
"onions",
"dec"), 1 },
57 {
new Tuple<string,string> (
"cotton",
"mar"), 0.5 },
58 {
new Tuple<string,string> (
"cotton",
"apr"), 1 },
59 {
new Tuple<string,string> (
"cotton",
"may"), 1 },
60 {
new Tuple<string,string> (
"cotton",
"jun"), 1 },
61 {
new Tuple<string,string> (
"cotton",
"jul"), 1 },
62 {
new Tuple<string,string> (
"cotton",
"aug"), 1 },
63 {
new Tuple<string,string> (
"cotton",
"sep"), 1 },
64 {
new Tuple<string,string> (
"cotton",
"oct"), 1 },
65 {
new Tuple<string,string> (
"cotton",
"nov"), 0.75 },
67 {
new Tuple<string,string> (
"maize",
"may"), 0.25 },
68 {
new Tuple<string,string> (
"maize",
"jun"), 1 },
69 {
new Tuple<string,string> (
"maize",
"jul"), 1 },
70 {
new Tuple<string,string> (
"maize",
"aug"), 1 },
71 {
new Tuple<string,string> (
"maize",
"sep"), 1 },
72 {
new Tuple<string,string> (
"maize",
"oct"), 0.5 },
74 {
new Tuple<string,string> (
"tomato",
"jul"), 0.75 },
75 {
new Tuple<string,string> (
"tomato",
"aug"), 1 },
76 {
new Tuple<string,string> (
"tomato",
"sep"), 1 },
77 {
new Tuple<string,string> (
"tomato",
"oct"), 1 },
78 {
new Tuple<string,string> (
"tomato",
"nov"), 0.75 }
81 private Dictionary<Tuple<String, String>,
double> laborreq =
new Dictionary<Tuple<String, String>,
double>()
83 {
new Tuple<string,string> (
"wheat",
"jan"), 1.72 },
84 {
new Tuple<string,string> (
"wheat",
"feb"), 0.5 },
85 {
new Tuple<string,string> (
"wheat",
"mar"), 1 },
86 {
new Tuple<string,string> (
"wheat",
"apr"), 1 },
87 {
new Tuple<string,string> (
"wheat",
"may"), 17.16 },
88 {
new Tuple<string,string> (
"wheat",
"jun"), 2.34 },
89 {
new Tuple<string,string> (
"wheat",
"nov"), 2.43 },
90 {
new Tuple<string,string> (
"wheat",
"dec"), 1.35 },
92 {
new Tuple<string,string> (
"clover",
"jan"), 4.5 },
93 {
new Tuple<string,string> (
"clover",
"feb"), 1 },
94 {
new Tuple<string,string> (
"clover",
"mar"), 8 },
95 {
new Tuple<string,string> (
"clover",
"nov"), 2.5 },
96 {
new Tuple<string,string> (
"clover",
"dec"), 7.5 },
98 {
new Tuple<string,string> (
"beans",
"jan"), 0.75 },
99 {
new Tuple<string,string> (
"beans",
"feb"), 0.75 },
100 {
new Tuple<string,string> (
"beans",
"mar"), 0.75 },
101 {
new Tuple<string,string> (
"beans",
"apr"), 16 },
102 {
new Tuple<string,string> (
"beans",
"nov"), 7.5 },
103 {
new Tuple<string,string> (
"beans",
"dec"), 0.75 },
105 {
new Tuple<string,string> (
"onions",
"jan"), 5.16 },
106 {
new Tuple<string,string> (
"onions",
"feb"), 5 },
107 {
new Tuple<string,string> (
"onions",
"mar"), 5 },
108 {
new Tuple<string,string> (
"onions",
"apr"), 19.58 },
109 {
new Tuple<string,string> (
"onions",
"may"), 2.42 },
110 {
new Tuple<string,string> (
"onions",
"nov"), 11.16 },
111 {
new Tuple<string,string> (
"onions",
"dec"), 4.68 },
113 {
new Tuple<string,string> (
"cotton",
"mar"), 5 },
114 {
new Tuple<string,string> (
"cotton",
"apr"), 5 },
115 {
new Tuple<string,string> (
"cotton",
"may"), 9 },
116 {
new Tuple<string,string> (
"cotton",
"jun"), 2 },
117 {
new Tuple<string,string> (
"cotton",
"jul"), 1.5 },
118 {
new Tuple<string,string> (
"cotton",
"aug"), 2 },
119 {
new Tuple<string,string> (
"cotton",
"sep"), 1 },
120 {
new Tuple<string,string> (
"cotton",
"oct"), 26 },
121 {
new Tuple<string,string> (
"cotton",
"nov"), 12 },
123 {
new Tuple<string,string> (
"maize",
"may"), 4.3 },
124 {
new Tuple<string,string> (
"maize",
"jun"), 5.04 },
125 {
new Tuple<string,string> (
"maize",
"jul"), 7.16 },
126 {
new Tuple<string,string> (
"maize",
"aug"), 7.97 },
127 {
new Tuple<string,string> (
"maize",
"sep"), 4.41 },
128 {
new Tuple<string,string> (
"maize",
"oct"), 1.12 },
130 {
new Tuple<string,string> (
"tomato",
"jul"), 17 },
131 {
new Tuple<string,string> (
"tomato",
"aug"), 15 },
132 {
new Tuple<string,string> (
"tomato",
"sep"), 12 },
133 {
new Tuple<string,string> (
"tomato",
"oct"), 7 },
134 {
new Tuple<string,string> (
"tomato",
"nov"), 6 }
137 private Dictionary<String, double> yield =
new Dictionary<String, double>()
148 private Dictionary<String, double> price =
new Dictionary<String, double>()
158 private Dictionary<String, double> miscost =
new Dictionary<String, double>()
168 private double land = 4;
169 private double owage = 3;
170 private double twage = 4;
174 InitializeComponent();
177 foreach (String p
in periods)
179 this.dataGridLandReq.Rows.Add();
180 this.dataGridLaborReq.Rows.Add();
181 this.dataGridLandReq.Rows[this.dataGridLandReq.Rows.Count - 1].HeaderCell.Value = p;
182 this.dataGridLaborReq.Rows[this.dataGridLaborReq.Rows.Count - 1].HeaderCell.Value = p;
186 this.dataGridYPM.Rows.Add(3);
187 this.dataGridYPM.Rows[0].HeaderCell.Value =
"Yield";
188 this.dataGridYPM.Rows[0].HeaderCell.ToolTipText =
"Crop Yield (tons per ha)";
189 this.dataGridYPM.Rows[1].HeaderCell.Value =
"Price";
190 this.dataGridYPM.Rows[1].HeaderCell.ToolTipText =
"Crop Prices ($ per ton)";
191 this.dataGridYPM.Rows[2].HeaderCell.Value =
"Miscost";
192 this.dataGridYPM.Rows[2].HeaderCell.ToolTipText =
"Miscellaneous Cash Costs ($ per ha)";
195 foreach (KeyValuePair<Tuple<String, String>,
double> kv
in landreq)
197 foreach (DataGridViewRow r
in this.dataGridLandReq.Rows)
198 if (kv.Key.Item2.Equals(r.HeaderCell.Value))
200 r.Cells[kv.Key.Item1 +
"_land"].Value = kv.Value;
206 foreach (KeyValuePair<Tuple<String, String>,
double> kv
in laborreq)
208 foreach (DataGridViewRow r
in this.dataGridLaborReq.Rows)
209 if (kv.Key.Item2.Equals(r.HeaderCell.Value))
211 r.Cells[kv.Key.Item1 +
"_lab"].Value = kv.Value;
217 foreach (DataGridViewRow r
in this.dataGridYPM.Rows)
219 if (
"Yield".Equals(r.HeaderCell.Value))
221 foreach (KeyValuePair<String, double> kv
in yield)
222 r.Cells[kv.Key +
"_ypm"].Value = kv.Value;
224 else if (
"Price".Equals(r.HeaderCell.Value))
226 foreach (KeyValuePair<String, double> kv
in price)
227 r.Cells[kv.Key +
"_ypm"].Value = kv.Value;
229 else if (
"Miscost".Equals(r.HeaderCell.Value))
231 foreach (KeyValuePair<String, double> kv
in miscost)
232 r.Cells[kv.Key +
"_ypm"].Value = kv.Value;
237 this.textBoxLand.Text = this.land.ToString();
238 this.textBoxOwage.Text = this.owage.ToString();
239 this.textBoxTwage.Text = this.twage.ToString();
242 this.dataGridCR.Rows.Add(3);
243 this.dataGridCR.Rows[0].HeaderCell.Value =
"landuse";
244 this.dataGridCR.Rows[1].HeaderCell.Value =
"output";
245 this.dataGridCR.Rows[2].HeaderCell.Value =
"revenue";
247 this.dataGridViewLRS.Rows.Add(13);
248 for (
int i = 0; i < periods.Length; i++)
249 this.dataGridViewLRS.Rows[i].HeaderCell.Value = periods[i];
250 this.dataGridViewLRS.Rows[12].HeaderCell.Value =
"total";
254 private void button1_Click_1(
object sender, EventArgs e)
256 this.button1.Enabled =
false;
258 this.chart1.Series.Clear();
259 this.chart1.ResetAutoValues();
260 this.chart1.Visible =
true;
261 Series s = this.chart1.Series.Add(
"landuse");
262 s.ChartType = SeriesChartType.Pie;
264 s[
"PieLabelStyle"] =
"Outside";
265 s.Label =
"#VALX (#PERCENT)";
266 this.chart1.ChartAreas[0].Area3DStyle.Enable3D =
true;
267 this.chart1.Legends[0].Enabled =
false;
269 this.chart2.Series.Clear();
270 this.chart2.ResetAutoValues();
271 this.chart2.Visible =
true;
272 this.chart2.Series.Add(
"totlanduse").ChartType = SeriesChartType.Column;
273 this.chart2.ChartAreas[0].Area3DStyle.Enable3D =
true;
274 this.chart2.ChartAreas[0].AxisX.Interval = 1;
275 this.chart2.ChartAreas[0].AxisY.Title =
"ha";
276 this.chart2.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Horizontal;
284 foreach (String p
in periods)
288 GAMSParameter landreq = data.AddParameter(
"landreq", 2,
"months of land occupation by crop (hectares)");
289 foreach (DataGridViewRow row
in this.dataGridLandReq.Rows)
291 String period = (String)row.HeaderCell.Value;
292 for (
int i = 0; i < row.Cells.Count; i++)
293 if (row.Cells[i].Value !=
null && !row.Cells[i].Value.Equals(
""))
294 landreq.
AddRecord(period,
this.dataGridLandReq.Columns[i].HeaderText).
Value = Convert.ToDouble(row.Cells[i].Value);
297 GAMSParameter laborreq = data.AddParameter(
"laborreq", 2,
"crop labor requirements (man-days per hectare)");
298 foreach (DataGridViewRow row
in this.dataGridLaborReq.Rows)
300 String period = (String)row.HeaderCell.Value;
301 for (
int i = 0; i < row.Cells.Count; i++)
302 if (row.Cells[i].Value !=
null && !row.Cells[i].Value.Equals(
""))
303 laborreq.
AddRecord(period,
this.dataGridLaborReq.Columns[i].HeaderText).
Value = Convert.ToDouble(row.Cells[i].Value);
306 GAMSParameter yield = data.AddParameter(
"yield", 1,
"crop yield (tons per hectare)");
307 GAMSParameter price = data.AddParameter(
"price", 1,
"crop prices (dollars per ton)");
308 GAMSParameter miscost = data.AddParameter(
"miscost", 1,
"misc cash costs (dollars per hectare)");
309 foreach (DataGridViewRow row
in this.dataGridYPM.Rows)
311 for (
int i = 0; i < row.Cells.Count; i++)
312 if (row.Cells[i].Value !=
null && !row.Cells[i].Value.Equals(
""))
313 data.GetParameter(row.HeaderCell.Value.ToString()).AddRecord(this.dataGridYPM.Columns[i].HeaderCell.Value.ToString()).Value = Convert.ToDouble(row.Cells[i].Value);
316 data.AddParameter(
"land", 0,
"farm size (hectares)").
AddRecord().
Value = Convert.ToDouble(this.textBoxLand.Text);
317 data.AddParameter(
"owage", 0,
"hire-out wage rate (dollars per day)").AddRecord().Value = Convert.ToDouble(this.textBoxOwage.Text);
318 data.AddParameter(
"twage", 0,
"temporary labor wage (dollars per day)").AddRecord().Value = Convert.ToDouble(this.textBoxTwage.Text);
322 opt.
Defines.Add(
"data", data.Name);
323 GAMSJob j = ws.AddJobFromString(GetModelText());
329 this.chart1.Series[
"landuse"].Points.AddXY(rec.
Key(0), rec.
Level);
331 Dictionary<String, double> vals =
new Dictionary<string, double>();
333 vals.Add(rec.
Key(0), 0);
335 vals[rec.
Key(0)] = rec.Value;
336 foreach (KeyValuePair<String, double> kv
in vals)
337 this.chart2.Series[
"totlanduse"].Points.AddXY(kv.Key, kv.Value);
339 ClearDataGridView(this.dataGridCR);
341 SetCellValue(this.dataGridCR, rec.
Key(0), rec.
Key(1), rec.Value);
343 ClearDataGridView(this.dataGridViewLRS);
345 SetCellValue(this.dataGridViewLRS, rec.
Key(0), rec.
Key(1), rec.Value);
347 this.chart1.Update();
348 this.chart2.Update();
349 this.label4.Text =
"Farm income ($): " + j.OutDB.GetVariable(
"yfarm").FindRecord().Level.ToString(
"0.####");
351 this.button1.Enabled =
true;
354 public void SetCellValue(DataGridView dgv, String rowHeader, String colHeader,
double value)
356 foreach (DataGridViewColumn col
in dgv.Columns)
358 if (col.HeaderText.Equals(colHeader))
360 foreach (DataGridViewRow row
in dgv.Rows)
362 if (row.HeaderCell.Value.Equals(rowHeader))
364 row.Cells[col.Index].Value = value;
371 public void ClearDataGridView(DataGridView dgv)
373 for (
int i = 0; i < dgv.Columns.Count; i++)
375 for (
int j = 0; j < dgv.Rows.Count; j++)
377 dgv.Rows[j].Cells[i].Value =
"";
382 static String GetModelText()
385Sets c crops / wheat, clover, beans, onions,
386 cotton, maize, tomato /
393Parameter landreq(t,c) months of land occupation by crop (hectares);
394Parameter laborreq(t,c) crop labor requirements (man-days per hectare);
398Parameters yield(c) crop yield (tons per hectare)
399 price(c) crop prices (dollars per ton)
400 miscost(c) misc cash costs (dollars per hectare)
402* farm data, size labor availability etc.
404Scalars land farm size (hectares)
405 famlab family labor available (days per month) / 25 /
406 owage hire-out wage rate (dollars per day)
407 twage temporary labor wage (dollars per day)
408 dpm number of working days per month / 25 /
411$load landreq laborreq land owage twage yield price miscost
414$Stitle endogenous variables and equations
416Variables xcrop(c) cropping activity (hectares)
417 yfarm farm income (dollars)
418 revenue value of production (dollars)
419 mcost misc cash cost (dollars)
420 labcost labor cost (dollars)
421 labearn labor income (dollars)
422 flab(t) family labor use (days)
423 fout(t) hiring out (days)
424 tlab(t) temporary labor (days)
426Positive Variable xcrop, flab ,fout, tlab
428Equations landbal(t) land balance (hectares)
429 laborbal(t) labor balance (days)
430 flabor(t) family labor balance (days)
431 arev revenue accounting (dollars)
432 acost cash cost accounting (dollars)
433 alab labor cost accounting (dollars)
434 aout labor income accounting (dollars)
435 income income definition (dollars);
438landbal(t).. sum(c, xcrop(c)*landreq(t,c)) =l= land ;
440laborbal(t).. sum(c, xcrop(c)*laborreq(t,c)) =l= flab(t) + tlab(t);
442flabor(t).. famlab =e= flab(t) + fout(t) ;
444arev.. revenue =e= sum(c, xcrop(c)*yield(c)*price(c)) ;
446acost.. mcost =e= sum(c, xcrop(c)*miscost(c)) ;
448alab.. labcost =e= sum(t, tlab(t)*twage) ;
450aout.. labearn =e= sum(t, fout(t)*owage) ;
452income.. yfarm =e= revenue + labearn - labcost - mcost ;
455Model demo1 farm labor model / all /
457Solve demo1 using lp maximizing yfarm;
459$Stitle report on solution
461Sets crep / landuse, output, revenue /
462 lrep / demand, family, temporary
465Parameters croprep crop report summary
466 labrep labor report summary(days);
468croprep('landuse',c) = xcrop.l(c);
469croprep('output',c) = xcrop.l(c)*yield(c);
470croprep('revenue',c) = croprep('output',c)*price(c);
471croprep(crep,'total') = sum(c, croprep(crep,c));
473labrep(t,'demand') = sum(c, xcrop.l(c)*laborreq(t,c));
474labrep(t,'family') = flab.l(t);
475labrep(t,'temporary') = tlab.l(t);
476labrep(t,'unused') = -laborbal.l(t);
477labrep(t,'hire-out') = fout.l(t);
478labrep('total',lrep) = sum(t, labrep(t,lrep));
480parameter totalLandUse(t);
481totalLandUse(t) = sum(c, xcrop.l(c)*landreq(t,c))";
GAMSSet AddSet(string identifier, int dimension, string explanatoryText="", SetType setType=SetType.multi)
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
Dictionary< string, string > Defines
new GAMSParameterRecord AddRecord(params string[] keys)
new GAMSSetRecord AddRecord(params string[] keys)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)