2using System.Collections.Generic;
3using System.ComponentModel;
8using System.Windows.Forms;
10using System.Reflection;
11using System.Data.OleDb;
21 public partial class Form1 : Form
23 private List<string> cuts =
new List<string>() {
"i1",
"i2",
"i3",
"i4" };
24 private Dictionary<string, int> d =
new Dictionary<string, int>() { {
"i1", 97 }, {
"i2", 610 }, {
"i3", 395 }, {
"i4", 211 } };
25 private Dictionary<string, int> w =
new Dictionary<string, int>() { {
"i1", 47 }, {
"i2", 36 }, {
"i3", 31 }, {
"i4", 14 } };
27 private int maxpattern = 35;
28 private int nrCuts = 0;
29 private List<Color> colors =
new List<Color>() { Color.FromArgb(255, 192, 255, 255), Color.FromArgb(255, 255, 255, 192),
30 Color.FromArgb(255, 192, 255, 192), Color.FromArgb(255, 255, 224, 192),
31 Color.FromArgb(255, 255, 192, 255), Color.FromArgb(255, 129, 255, 192),
32 Color.FromArgb(255, 192, 192, 255), Color.FromArgb(255, 255, 192, 192),
33 Color.FromArgb(255, 100, 192, 220), Color.FromArgb(255, 129, 100, 192),
34 Color.FromArgb(255, 192, 192, 100), Color.FromArgb(255, 255, 192, 100) };
38 InitializeComponent();
40 nudMax.Value = maxpattern;
43 foreach (
string i
in cuts)
45 flowLayoutPanel1.Controls.Add(
new CutWidthsControl(i, w[i], (
int) nudWidth.Value, d[i], colors[nrCuts%colors.Count], nudWidth));
51 private void button3_Click(
object sender, EventArgs e)
53 flowLayoutPanel1.Controls.Add(
new CutWidthsControl(
"i" + (nrCuts + 1), (
int)nudWidth.Value, (
int)nudWidth.Value, 0, colors[nrCuts % colors.Count], nudWidth));
54 flowLayoutPanel1.VerticalScroll.Value = flowLayoutPanel1.VerticalScroll.Maximum;
55 flowLayoutPanel1.Update();
56 flowLayoutPanel1.VerticalScroll.Value = flowLayoutPanel1.VerticalScroll.Maximum;
61 private void button2_Click(
object sender, EventArgs e)
64 MessageBox.Show(
"Can't delete last cut width because there has to be at least one.");
67 flowLayoutPanel1.Controls.RemoveAt(nrCuts-1);
68 flowLayoutPanel1.Update();
73 private void button1_Click(
object sender, EventArgs e)
76 tabControl1.Visible =
false;
77 tabControl1.TabPages.Clear();
85 opt.AllModelTypes =
"Cbc";
87 opt.
Defines.Add(
"pmax", nudMax.Value.ToString());
88 opt.
Defines.Add(
"solveMasterAs",
"RMIP");
107 masterInitJob.
Run(opt, masterCP, cutstockData);
109 GAMSJob masterJob = ws.
AddJobFromString(
"execute_load 'csdata', aip, pp; solve master min z using %solveMasterAs%;", masterCP);
111 GAMSSet pattern = cutstockData.
AddSet(
"pp", 1,
"pattern index");
115 int patternCount = 0;
119 patternData.
AddRecord(rec.
Keys[0], pattern.AddRecord((++patternCount).ToString()).Keys[0]).Value = (
int)((int)nudWidth.Value / rec.
Value);
124 ws.AddJobFromString(GetSubModel()).Run(opt, subCP, cutstockData);
132 bool patternAdded =
true;
135 masterJob.Run(opt, masterCP, cutstockData);
144 if (patternCount == maxpattern)
146 richTextBox1.AppendText(
"Out of pattern. Increase maxpattern (currently " + nudMax.Value +
")." + Environment.NewLine);
147 patternAdded =
false;
152 richTextBox1.ScrollToCaret();
153 GAMSSetRecord s = pattern.AddRecord((++patternCount).ToString());
158 patternData.AddRecord(y.
Keys[0], s.Keys[0]).Value = Math.Round(y.
Level);
163 else patternAdded =
false;
164 }
while (patternAdded);
167 opt.Defines[
"solveMasterAs"] =
"MIP";
168 masterJob.Run(opt, cutstockData);
169 richTextBox1.AppendText(
"Optimal Solution: " + masterJob.OutDB.GetVariable(
"z").FindRecord().Level + Environment.NewLine); richTextBox1.ScrollToCaret();
170 tabControl1.Visible =
true;
175 richTextBox1.AppendText(String.Format(
" pattern {0,2} {1,4} times: ", xp.
Keys[0], Math.Round(xp.
Level))); richTextBox1.ScrollToCaret();
177 TabPage tp =
new TabPage(
"pattern " + xp.
Keys[0].ToString());
178 tp.BackColor = Color.White;
179 tabControl1.TabPages.Add(tp);
182 double scale = tp.Width/masterJob.OutDB.GetParameter(
"r").FirstRecord().Value;
185 richTextBox1.AppendText(
" " + aip.
Keys[0] +
": " + aip.
Value.ToString()); richTextBox1.ScrollToCaret();
187 for (
int i = 0; i < aip.
Value; i++)
189 Panel p =
new Panel();
190 p.BorderStyle = BorderStyle.FixedSingle;
191 p.BackColor = flowLayoutPanel1.Controls.Find(aip.
Keys[0],
true)[0].BackColor;
192 p.Height = tp.Height;
193 p.Width = (int) (width.FindRecord(aip.
Keys[0]).Value * scale);
200 Panel pExcess =
new Panel();
201 pExcess.BorderStyle = BorderStyle.FixedSingle;
202 pExcess.BackColor = Color.LightGray;
203 pExcess.Height = tp.Height;
204 pExcess.Width = (int) (rawWidth.FirstRecord().Value*scale - x);
206 tp.Controls.Add(pExcess);
208 richTextBox1.AppendText(Environment.NewLine); richTextBox1.ScrollToCaret();
212 cutstockData.Dispose();
218 richTextBox1.AppendText(
"Exception: " + ex.Message);
222 static String GetMasterModel()
225$Title Cutting Stock - Master problem
236$if not set pmax $set pmax 1000
237Set p possible patterns /1*%pmax%/
238 pp(p) dynamic subset of p
240 aip(i,p) number of width i in pattern growing in p;
243Variable xp(p) patterns used
245Integer variable xp; xp.up(p) = sum(i, d(i));
247Equation numpat number of patterns used
248 demand(i) meet demand;
250numpat.. z =e= sum(pp, xp(pp));
251demand(i).. sum(pp, aip(i,pp)*xp(pp)) =g= d(i);
253model master /numpat, demand/;";
257 static String GetSubModel()
260$Title Cutting Stock - Pricing problem is a knapsack model
272 demdual(i) duals of master demand constraint /#i eps/;
274Variable z, y(i) new pattern;
275Integer variable y; y.up(i) = ceil(r/w(i));
278 knapsack knapsack constraint;
280defobj.. z =e= 1 - sum(i, demdual(i)*y(i));
281knapsack.. sum(i, w(i)*y(i)) =l= r;
283model pricing /defobj, knapsack/; pricing.optfile=1";
288 private void button4_Click(
object sender, EventArgs e)
290 OpenFileDialog browser =
new OpenFileDialog();
292 browser.DefaultExt =
"accdb";
293 browser.Filter =
"Access Database (*.accdb)|*.accdb";
294 browser.InitialDirectory = Environment.CurrentDirectory;
296 if (browser.ShowDialog() == DialogResult.OK)
299 OleDbConnection connection =
null;
302 connection =
new OleDbConnection(
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + browser.FileName);
307 flowLayoutPanel1.Controls.RemoveAt(nrCuts-1);
310 flowLayoutPanel1.Update();
312 OleDbCommand cmd =
new OleDbCommand(
"SELECT Width FROM RawWidth", connection);
314 OleDbDataReader reader = cmd.ExecuteReader();
315 while (reader.Read())
317 nudWidth.Value = (int)reader.GetValue(0);
321 cmd =
new OleDbCommand(
"SELECT Label, Width, Demand FROM CutWidths", connection);
322 reader = cmd.ExecuteReader();
323 while (reader.Read()) {
324 flowLayoutPanel1.Controls.Add(
new CutWidthsControl(reader.GetString(0), (
int)reader.GetValue(1), (
int)nudWidth.Value, (
int)reader.GetValue(2), colors[nrCuts % colors.Count], nudWidth));
325 flowLayoutPanel1.VerticalScroll.Value = flowLayoutPanel1.VerticalScroll.Maximum;
326 flowLayoutPanel1.Update();
327 flowLayoutPanel1.VerticalScroll.Value = flowLayoutPanel1.VerticalScroll.Maximum;
334 MessageBox.Show(
"Error reading data from database. \n" + ex.Message);
GAMSModelInstance AddModelInstance(string modelInstanceName=null)
GAMSVariable GetVariable(string variableIdentifier)
GAMSSet AddSet(string identifier, int dimension, string explanatoryText="", SetType setType=SetType.multi)
GAMSParameter AddParameter(string identifier, int dimension, string explanatoryText="")
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
void Solve(SymbolUpdateType updateType=SymbolUpdateType.BaseCase, TextWriter output=null, GAMSModelInstanceOpt miOpt=null)
void Instantiate(string modelDefinition, params GAMSModifier[] modifiers)
Dictionary< string, string > Defines
new GAMSParameterRecord AddRecord(params string[] keys)
new GAMSSetRecord AddRecord(params string[] keys)
new GAMSVariableRecord FindRecord(params string[] keys)
GAMSJob AddJobFromString(string gamsSource, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)
GAMSCheckpoint AddCheckpoint(string checkpointName=null)
GAMSOptions AddOptions(GAMSOptions optFrom=null)