Loading...
Searching...
No Matches
Form1.cs
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using GAMS;
10using System.Windows.Forms.DataVisualization.Charting;
11using System.Diagnostics;
12using System.Data.OleDb;
13using System.Reflection;
14
15namespace Demo1GUI
16{
22 public partial class Form1 : Form
23 {
24
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>()
27 {
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 },
35
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 },
41
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 },
48
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 },
56
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 },
66
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 },
73
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 }
79 };
80
81 private Dictionary<Tuple<String, String>, double> laborreq = new Dictionary<Tuple<String, String>, double>()
82 {
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 },
91
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 },
97
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 },
104
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 },
112
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 },
122
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 },
129
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 }
135 };
136
137 private Dictionary<String, double> yield = new Dictionary<String, double>()
138 {
139 { "wheat" , 1.5 },
140 { "clover", 6.5},
141 { "beans" , 1 },
142 { "onions", 3 },
143 { "cotton", 1.5 },
144 { "maize" , 2 },
145 { "tomato", 3 }
146 };
147
148 private Dictionary<String, double> price = new Dictionary<String, double>()
149 {
150 { "wheat" , 100 },
151 { "beans" , 200 },
152 { "onions", 125 },
153 { "cotton", 350 },
154 { "maize" , 70 },
155 { "tomato", 120 }
156 };
157
158 private Dictionary<String, double> miscost = new Dictionary<String, double>()
159 {
160 { "wheat" , 10 },
161 { "beans" , 5 },
162 { "onions", 50 },
163 { "cotton", 80 },
164 { "maize" , 5 },
165 { "tomato", 50 }
166 };
167
168 private double land = 4;
169 private double owage = 3;
170 private double twage = 4;
171
172 public Form1()
173 {
174 InitializeComponent();
175
176 // add default rows for the periods
177 foreach (String p in periods)
178 {
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;
183 }
184
185 // add default rows for yield, price and miscost
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)";
193
194 // bind the land requirement data
195 foreach (KeyValuePair<Tuple<String, String>, double> kv in landreq)
196 {
197 foreach (DataGridViewRow r in this.dataGridLandReq.Rows)
198 if (kv.Key.Item2.Equals(r.HeaderCell.Value))
199 {
200 r.Cells[kv.Key.Item1 + "_land"].Value = kv.Value;
201 break;
202 }
203 }
204
205 // bind the labor requirement data
206 foreach (KeyValuePair<Tuple<String, String>, double> kv in laborreq)
207 {
208 foreach (DataGridViewRow r in this.dataGridLaborReq.Rows)
209 if (kv.Key.Item2.Equals(r.HeaderCell.Value))
210 {
211 r.Cells[kv.Key.Item1 + "_lab"].Value = kv.Value;
212 break;
213 }
214 }
215
216 // bind yield, price, miscost
217 foreach (DataGridViewRow r in this.dataGridYPM.Rows)
218 {
219 if ("Yield".Equals(r.HeaderCell.Value))
220 {
221 foreach (KeyValuePair<String, double> kv in yield)
222 r.Cells[kv.Key + "_ypm"].Value = kv.Value;
223 }
224 else if ("Price".Equals(r.HeaderCell.Value))
225 {
226 foreach (KeyValuePair<String, double> kv in price)
227 r.Cells[kv.Key + "_ypm"].Value = kv.Value;
228 }
229 else if ("Miscost".Equals(r.HeaderCell.Value))
230 {
231 foreach (KeyValuePair<String, double> kv in miscost)
232 r.Cells[kv.Key + "_ypm"].Value = kv.Value;
233 }
234 }
235
236 // bind scalars
237 this.textBoxLand.Text = this.land.ToString();
238 this.textBoxOwage.Text = this.owage.ToString();
239 this.textBoxTwage.Text = this.twage.ToString();
240
241 // prepare result table
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";
246
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";
251
252 }
253
254 private void button1_Click_1(object sender, EventArgs e)
255 {
256 this.button1.Enabled = false;
257
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;
263 s.BorderWidth = 1;
264 s["PieLabelStyle"] = "Outside";
265 s.Label = "#VALX (#PERCENT)";
266 this.chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
267 this.chart1.Legends[0].Enabled = false;
268
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;
277
278 GAMSWorkspace ws = new GAMSWorkspace();
279
280 // get data from user input
281 GAMSDatabase data = ws.AddDatabase("data");
282
283 GAMSSet t = data.AddSet("t", 1, "period");
284 foreach (String p in periods)
285 t.AddRecord(p);
286
287
288 GAMSParameter landreq = data.AddParameter("landreq", 2, "months of land occupation by crop (hectares)");
289 foreach (DataGridViewRow row in this.dataGridLandReq.Rows)
290 {
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);
295 }
296
297 GAMSParameter laborreq = data.AddParameter("laborreq", 2, "crop labor requirements (man-days per hectare)");
298 foreach (DataGridViewRow row in this.dataGridLaborReq.Rows)
299 {
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);
304 }
305
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)
310 {
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);
314 }
315
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);
319
320 // run the GAMS job
321 GAMSOptions opt = ws.AddOptions();
322 opt.Defines.Add("data", data.Name);
323 GAMSJob j = ws.AddJobFromString(GetModelText());
324 j.Run(opt, data);
325
326 //Display some results
327 foreach (GAMSVariableRecord rec in j.OutDB.GetVariable("xcrop"))
328 if(rec.Level > 0)
329 this.chart1.Series["landuse"].Points.AddXY(rec.Key(0), rec.Level);
330
331 Dictionary<String, double> vals = new Dictionary<string, double>();
332 foreach(GAMSSetRecord rec in j.OutDB.GetSet("t"))
333 vals.Add(rec.Key(0), 0);
334 foreach (GAMSParameterRecord rec in j.OutDB.GetParameter("totalLandUse"))
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);
338
339 ClearDataGridView(this.dataGridCR);
340 foreach (GAMSParameterRecord rec in j.OutDB.GetParameter("croprep"))
341 SetCellValue(this.dataGridCR, rec.Key(0), rec.Key(1), rec.Value);
342
343 ClearDataGridView(this.dataGridViewLRS);
344 foreach (GAMSParameterRecord rec in j.OutDB.GetParameter("labrep"))
345 SetCellValue(this.dataGridViewLRS, rec.Key(0), rec.Key(1), rec.Value);
346
347 this.chart1.Update();
348 this.chart2.Update();
349 this.label4.Text = "Farm income ($): " + j.OutDB.GetVariable("yfarm").FindRecord().Level.ToString("0.####");
350
351 this.button1.Enabled = true;
352 }
353
354 public void SetCellValue(DataGridView dgv, String rowHeader, String colHeader, double value)
355 {
356 foreach (DataGridViewColumn col in dgv.Columns)
357 {
358 if (col.HeaderText.Equals(colHeader))
359 {
360 foreach (DataGridViewRow row in dgv.Rows)
361 {
362 if (row.HeaderCell.Value.Equals(rowHeader))
363 {
364 row.Cells[col.Index].Value = value;
365 }
366 }
367 }
368 }
369 }
370
371 public void ClearDataGridView(DataGridView dgv)
372 {
373 for (int i = 0; i < dgv.Columns.Count; i++)
374 {
375 for (int j = 0; j < dgv.Rows.Count; j++)
376 {
377 dgv.Rows[j].Cells[i].Value = "";
378 }
379 }
380 }
381
382 static String GetModelText()
383 {
384 String model = @"
385Sets c crops / wheat, clover, beans, onions,
386 cotton, maize, tomato /
387 t period;
388
389$gdxin data
390$load t
391$gdxin
392
393Parameter landreq(t,c) months of land occupation by crop (hectares);
394Parameter laborreq(t,c) crop labor requirements (man-days per hectare);
395
396$Eject
397
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)
401
402* farm data, size labor availability etc.
403
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 /
409
410$gdxin data
411$load landreq laborreq land owage twage yield price miscost
412$gdxin
413
414$Stitle endogenous variables and equations
415
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)
425
426Positive Variable xcrop, flab ,fout, tlab
427
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);
436
437
438landbal(t).. sum(c, xcrop(c)*landreq(t,c)) =l= land ;
439
440laborbal(t).. sum(c, xcrop(c)*laborreq(t,c)) =l= flab(t) + tlab(t);
441
442flabor(t).. famlab =e= flab(t) + fout(t) ;
443
444arev.. revenue =e= sum(c, xcrop(c)*yield(c)*price(c)) ;
445
446acost.. mcost =e= sum(c, xcrop(c)*miscost(c)) ;
447
448alab.. labcost =e= sum(t, tlab(t)*twage) ;
449
450aout.. labearn =e= sum(t, fout(t)*owage) ;
451
452income.. yfarm =e= revenue + labearn - labcost - mcost ;
453
454
455Model demo1 farm labor model / all /
456
457Solve demo1 using lp maximizing yfarm;
458
459$Stitle report on solution
460
461Sets crep / landuse, output, revenue /
462 lrep / demand, family, temporary
463 unused, hire-out /
464
465Parameters croprep crop report summary
466 labrep labor report summary(days);
467
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));
472
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));
479
480parameter totalLandUse(t);
481totalLandUse(t) = sum(c, xcrop.l(c)*landreq(t,c))";
482
483 return model;
484 }
485 }
486}
For this farming model, the input data can be entered directly into several tables....
Definition: Form1.cs:23
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)
string Key(int index)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)