Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
  • 1.  OPL CPLEX Analyst Studio-VSTO 'Warehouse example' Problems- Please Help

    Posted Sat March 20, 2010 11:42 AM

    Originally posted by: pentunia


    Has anybody tried the 'Warehouse example' and managed to get it to work:
    'Integrating OPL with Excel using Visual Studio Tools for Office'
    http://publib.boulder.ibm.com/infocenter/odmeinfo/v3r3/index.jsp?topic=/ilog.odms.ide.odme.help/Content/Optimization/Documentation/ODME/_pubskel/ODME_pubskels/startALL_ODME_Eclipse_and_Xplatform_doc1279.html

    If you have, would you please paste your entire code, pleeeease :)

    I'm suppose to learn these tutorials, but I'm having the hardest time :/

    1. At this point I have managed to whittle my errors down to only 6- Visual Studio Output window reply after running Debug/Run Debugging:
    code
    ThisAddIn.cs(56,17): error CS0103: The name 'MessageBox' does not exist in the current context
    ThisAddIn.cs(56,56): error CS0103: The name 'MessageBoxButtons' does not exist in the current context
    ThisAddIn.cs(56,78): error CS0103: The name 'MessageBoxIcon' does not exist in the current context
    ThisAddIn.cs(173,21): error CS0103: The name 'MessageBox' does not exist in the current context
    ThisAddIn.cs(173,119): error CS0103: The name 'MessageBoxButtons' does not exist in the current context
    ThisAddIn.cs(177,21): error CS0103: The name 'MessageBox' does not exist in the current context
    [/code]

    2. Now if I add (near the top) 'using System.Windows.Forms;' I no longer receive the above errors. Instead:
    A blank Excel Book1 opens... I wait and wait, nothing- I close Excel. VS Output window states:
    code<snip>
    A first chance exception of type 'System.Deployment.Application.DeploymentException' occurred in System.Deployment.dll
    A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
    The thread '<No Name>' (0x1e84) has exited with code 0 (0x0).
    <snip>
    The thread 0x1e78 has exited with code 0 (0x0).
    The program '7804 excel.exe: Managed' has exited with code 0 (0x0).
    [/code]
    (If it's important I have pasted the entire VS Output text to here http://pastebin.com/n8eMF1HN )
    3. This is my complete code (I have already added the oplall.dll to the References):
    code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Excel = Microsoft.Office.Interop.Excel;
    using Office = Microsoft.Office.Core;
    using System.Windows.Forms;

    using ILOG.Concert;
    using ILOG.CPLEX;
    using ILOG.OPL;
    using Exception = System.Exception;

    namespace ExcelWarehouse
    {
    public partial class ThisAddIn
    {
    private Office.CommandBar AddInMenuBar;
    private Office.CommandBarButton SetupButton;
    private Office.CommandBarButton SolveButton;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
    CreateOplCommands();
    }
    private void CreateOplCommands()
    {
    try
    {
    // new command bar for OPL:
    AddInMenuBar = Application.CommandBars.Add(
    "OPL Commands", Office.MsoBarPosition.msoBarTop, missing, true);
    // setup button:
    SetupButton = (Office.CommandBarButton)AddInMenuBar.Controls.Add(
    Office.MsoControlType.msoControlButton,
    missing, missing, missing, true);
    SetupButton.Caption = "OPL Warehouse Setup";
    SetupButton.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;
    SetupButton.Click += new
    Office._CommandBarButtonEvents_ClickEventHandler(
    SetupButton_Click);

    // solve button:
    SolveButton = (Office.CommandBarButton)AddInMenuBar.Controls.Add(
    Office.MsoControlType.msoControlButton,
    missing, missing, missing, true);
    SolveButton.Caption = "OPL Warehouse Solve";
    SolveButton.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;
    SolveButton.Click += new
    Office._CommandBarButtonEvents_ClickEventHandler(
    SolveButton_Click);
    AddInMenuBar.Visible = true;
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    RemoveOPLCommands();
    }
    private void RemoveOPLCommands()
    {
    SolveButton.Delete(false);
    SolveButton = null;
    SetupButton.Delete(false);
    SetupButton = null;
    AddInMenuBar = null;
    }

    private void SolveButton_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
    {
    // respond to solve button click
    Excel.Worksheet sheet = (Excel.Worksheet)this.Application.ActiveWorkbook.ActiveSheet;
    System.IO.StringWriter errHandlerErrors = new System.IO.StringWriter();
    try
    {
    setValue(sheet, "B9", "Computing...");
    SolveProblem(sheet, errHandlerErrors);
    setValue(sheet, "B9", "Success");
    }
    catch (Exception ex)
    {
    setValue(sheet, "B9", "Error:" + ex.ToString() + "(" + errHandlerErrors.ToString() + ")");
    }

    }

    private void SetupButton_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
    {
    // respond to setup button click
    Excel.Worksheet sheet = (Excel.Worksheet)this.Application.ActiveWorkbook.ActiveSheet;
    setValue(sheet, "A1", "Data used by the OPL model:");
    setValue(sheet, "A2", "nbWarehouses:");
    setValue(sheet, "B2", "5");
    setValue(sheet, "A3", "nbStores:");
    setValue(sheet, "B3", "10");
    setValue(sheet, "A4", "fixed:");
    setValue(sheet, "B4", "30");
    setValue(sheet, "A5", "disaggregate:");
    setValue(sheet, "B5", "1");

    setValue(sheet, "A8", "Result computed by the OPL model:");
    setValue(sheet, "A9", "State:");
    setValue(sheet, "A10", "Objective:");

    }

    private void SolveProblem(Excel.Worksheet sheet, System.IO.StringWriter errHandlerErrors)
    {
    // actually solve the problem with OPL
    OplFactory.DebugMode = true;
    OplFactory oplF = new OplFactory();
    OplErrorHandler errHandler = oplF.CreateOplErrorHandler(errHandlerErrors);
    OplModelSource modelSource = oplF.CreateOplModelSourceFromString(GetModelText(), "warehouse");
    OplSettings settings = oplF.CreateOplSettings(errHandler);
    OplModelDefinition def = oplF.CreateOplModelDefinition(modelSource, settings);
    Cplex cplex = oplF.CreateCplex();
    OplModel opl = oplF.CreateOplModel(def, cplex);
    OplDataSource dataSource = new MyParams(oplF, sheet);
    opl.AddDataSource(dataSource);
    opl.Generate();
    if (cplex.Solve())
    {
    setValue(sheet, "B10", "" + opl.Cplex.ObjValue);
    }
    else
    {
    setValue(sheet, "B10", "No solution");
    }
    oplF.End();
    }
    /**
    * This class feeds data to the OPL model from the appropriate cells of the
    * input Excel worksheet.
    */
    class MyParams : CustomOplDataSource
    {
    private Excel.Worksheet _xlDataSheet;

    public MyParams(OplFactory oplF, Excel.Worksheet xlDataSheet)
    : base(oplF)
    {
    _xlDataSheet = xlDataSheet;
    }

    public override void CustomRead()
    {
    OplDataHandler handler = this.DataHandler;

    try
    {
    handler.StartElement("nbWarehouses");
    handler.AddIntItem(Int16.Parse(getValue(_xlDataSheet, "B2")));
    handler.EndElement();

    handler.StartElement("nbStores");
    handler.AddIntItem(Int16.Parse(getValue(_xlDataSheet, "B3")));
    handler.EndElement();

    handler.StartElement("fixed");
    handler.AddIntItem(Int16.Parse(getValue(_xlDataSheet, "B4")));
    handler.EndElement();

    handler.StartElement("disaggregate");
    handler.AddIntItem(Int16.Parse(getValue(_xlDataSheet, "B5")));
    handler.EndElement();
    }
    catch (System.FormatException ex)
    {
    MessageBox.Show("Error in the data, please check that you're using integers only.", "Data Error", MessageBoxButtons.OK);
    }
    catch (Exception ex)
    {
    MessageBox.Show("Exception in custom data source:" + ex.ToString());
    }
    }
    }

    /**
    * This is the warehouse OPL model:
    */
    static String GetModelText()
    {
    String model = "";
    model += "int fixed = ...;";
    model += "int nbWarehouses = ...;";
    model += "int nbStores = ...;";
    model += "int disaggregate = ...;";
    model += "assert nbStores > nbWarehouses;";

    model += "range Warehouses = 1..nbWarehouses;";
    model += "range Stores = 1..nbStores;";

    model += "int capacityw in Warehouses = nbStores div nbWarehouses + w mod (nbStores div nbWarehouses);";
    model += "int supplyCosts in Storesw in Warehouses = 1+((s+10*w) mod 100);";

    model += "dvar float openWarehouses in 0..1;";
    model += "dvar float supplyStoresWarehouses in 0..1;";

    model += "minimize ";
    model += "sum(w in Warehouses) fixed * open[w] +";
    model += "sum(w in Warehouses, s in Stores) supplyCost[s][w] * supply[s][w];";

    model += "constraints {";
    model += " forall(s in Stores)";
    model += " sum(w in Warehouses) supply[s][w] == 1;";
    model += " forall(w in Warehouses)";
    model += " sum(s in Stores) supply[s][w] <= open[w]*capacity[w];";
    model += " if (disaggregate == 1) {";
    model += " forall(w in Warehouses, s in Stores)";
    model += " supply[s][w] <= open[w];";
    model += " }";
    model += "}";
    return model;
    }
    static private void setValue(Excel.Worksheet sheet, String cell, String value)
    {
    sheet.get_Range(cell, cell).set_Value(Excel.XlRangeValueDataType.xlRangeValueDefault, value);
    }

    static private String getValue(Excel.Worksheet sheet, String cell)
    {
    return (String)sheet.get_Range(cell, cell).Text;
    }

    #region VSTO generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InternalStartup()
    {
    this.Startup += new System.EventHandler(ThisAddIn_Startup);
    this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
    }

    #endregion
    }
    }

    [/code]

    (I'm using Windows XP SP3, MS Office 2007 SP2 (running all from computer; not CD including Primary Interop Assemblies), Visual Studios 2008 SP1, VSTO 3.0, OPL version 6.3, .NET.
    I do have: C:\WINDOWS\system32\Com\comadmin.dll & C:\WINDOWS\system32\dllcache\comadmin.dll
    I do have 'mscorlib.dll' (9 locations) and
    'System.Deployment' is registered in my: C:\WINDOWS\assembly\
    and is located in 2 places:
    C:\WINDOWS\assembly\GAC_MSIL\System.Deployment\2.0.0.0__b03f5f7f11d50a3a\System.Deployment.dll
    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Deployment.dll
    )

    I have rebooted and retried- same issue.
    I have in VS menu- Debug/Exceptions/ searched for 'InvalidDeploymentException' / Then selected it/OK - same issue.

    Thank you :)
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: OPL CPLEX Analyst Studio-VSTO 'Warehouse example' Problems- Please Help

    Posted Mon March 22, 2010 01:54 PM

    Originally posted by: SystemAdmin


    did you try the Microsoft Visual Studio Solution that is located in <OPL dir>\examples\opl_interfaces\dotnet\x86_windows_vs2008\CSharp\ExcelWarehouse ?
    After updating the path to the oplall.dll, the Excel2007AddIn project works fine for me.
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: OPL CPLEX Analyst Studio-VSTO 'Warehouse example' Problems- Please Help

    Posted Mon March 22, 2010 09:48 PM

    Originally posted by: pentunia


    Thank you vbl for your time :)
    My wild guess for these errors I have above for 'MessageBox' (before I add the line: 'using System.Windows.Forms;'), are asking me to add more code, something to define/start this MessageBox?

    Yes I did try the CSharp one too, and it is where I copied the order of the code to get the one above down to those 6 errors only haha.
    C:\ILOG\OPL63\examples\opl_interfaces\dotnet\x86_.net2005_8.0\CSharp\ExcelWarehouse\ExcelWarehouse.sln
    The path in the References was correct to the C:\ILOG\OPL63\lib\oplall.dll but I removed it and added it again just in case.
    When I select Build/Build Excel2007Addin/ it is even a bigger mess of errors:
    ------ Build started: Project: Excel2007AddIn, Configuration: Debug Any CPU ------
    c:\WINDOWS\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /nowarn:1701,1702 /warn:4 /define:DEBUG;TRACE /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Accessibility.dll /reference:"C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Excel.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\VSTO\v9.0\Microsoft.Office.Tools.Common.v9.0.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\VSTO\v9.0\Microsoft.Office.Tools.Excel.v9.0.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\VSTO\v9.0\Microsoft.Office.Tools.v9.0.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\VSTA\v9.0\Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll" /reference:"C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office12\Office.dll" /reference:..\..\..\..\..\..\..\lib\oplall.dll /reference:"C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office11\stdole.dll" /reference:"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.AddIn.dll" /reference:"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll" /reference:"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.DataSetExtensions.dll" /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.XML.dll /reference:"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq.dll" /debug+ /debug:full /optimize- /out:obj\Debug\Excel2007AddIn.dll /resource:obj\Debug\Excel2007AddIn.Properties.Resources.resources /target:library Properties\AssemblyInfo.cs Properties\Resources.Designer.cs Properties\Settings.Designer.cs ExcelWarehouse.cs ExcelWarehouse.Designer.cs
    C:\ILOG\OPL63\examples\opl_interfaces\dotnet\x86_.net2005_8.0\CSharp\ExcelWarehouse\Excel2007AddIn\ExcelWarehouse.cs(37,18): error CS1061: 'ExcelWarehouse.ExcelWarehouse' does not contain a definition for 'Application' and no extension method 'Application' accepting a first argument of type 'ExcelWarehouse.ExcelWarehouse' could be found (are you missing a using directive or an assembly reference?)
    C:\ILOG\OPL63\examples\opl_interfaces\dotnet\x86_.net2005_8.0\CSharp\ExcelWarehouse\Excel2007AddIn\ExcelWarehouse.cs(37,138): error CS1061: 'ExcelWarehouse.ExcelWarehouse' does not contain a definition for 'Application' and no extension method 'Application' accepting a first argument of type 'ExcelWarehouse.ExcelWarehouse' could be found (are you missing a using directive or an assembly reference?)
    C:\ILOG\OPL63\examples\opl_interfaces\dotnet\x86_.net2005_8.0\CSharp\ExcelWarehouse\Excel2007AddIn\ExcelWarehouse.cs(47,44): error CS0117: 'System.Windows.Forms.Application' does not contain a definition for 'CommandBars'
    C:\ILOG\OPL63\examples\opl_interfaces\dotnet\x86_.net2005_8.0\CSharp\ExcelWarehouse\Excel2007AddIn\ExcelWarehouse.cs(48,70): error CS0103: The name 'missing' does not exist in the current context
    C:\ILOG\OPL63\examples\opl_interfaces\dotnet\x86_.net2005_8.0\CSharp\ExcelWarehouse\Excel2007AddIn\ExcelWarehouse.cs(52,21): error CS0103: The name 'missing' does not exist in the current context
    C:\ILOG\OPL63\examples\opl_interfaces\dotnet\x86_.net2005_8.0\CSharp\ExcelWarehouse\Excel2007AddIn\ExcelWarehouse.cs(52,30): error CS0103: The name 'missing' does not exist in the current context
    C:\ILOG\OPL63\examples\opl_interfaces\dotnet\x86_.net2005_8.0\CSharp\ExcelWarehouse\Excel2007AddIn\ExcelWarehouse.cs(52,39): error CS0103: The name 'missing' does not exist in the current context
    C:\ILOG\OPL63\examples\opl_interfaces\dotnet\x86_.net2005_8.0\CSharp\ExcelWarehouse\Excel2007AddIn\ExcelWarehouse.cs(62,21): error CS0103: The name 'missing' does not exist in the current context
    C:\ILOG\OPL63\examples\opl_interfaces\dotnet\x86_.net2005_8.0\CSharp\ExcelWarehouse\Excel2007AddIn\ExcelWarehouse.cs(62,30): error CS0103: The name 'missing' does not exist in the current context
    C:\ILOG\OPL63\examples\opl_interfaces\dotnet\x86_.net2005_8.0\CSharp\ExcelWarehouse\Excel2007AddIn\ExcelWarehouse.cs(62,39): error CS0103: The name 'missing' does not exist in the current context
    C:\ILOG\OPL63\examples\opl_interfaces\dotnet\x86_.net2005_8.0\CSharp\ExcelWarehouse\Excel2007AddIn\ExcelWarehouse.cs(92,59): error CS1061: 'ExcelWarehouse.ExcelWarehouse' does not contain a definition for 'Application' and no extension method 'Application' accepting a first argument of type 'ExcelWarehouse.ExcelWarehouse' could be found (are you missing a using directive or an assembly reference?)
    C:\ILOG\OPL63\examples\opl_interfaces\dotnet\x86_.net2005_8.0\CSharp\ExcelWarehouse\Excel2007AddIn\ExcelWarehouse.cs(110,59): error CS1061: 'ExcelWarehouse.ExcelWarehouse' does not contain a definition for 'Application' and no extension method 'Application' accepting a first argument of type 'ExcelWarehouse.ExcelWarehouse' could be found (are you missing a using directive or an assembly reference?)
    C:\ILOG\OPL63\examples\opl_interfaces\dotnet\x86_.net2005_8.0\CSharp\ExcelWarehouse\Excel2007AddIn\ExcelWarehouse.cs(253,18): error CS1061: 'ExcelWarehouse.ExcelWarehouse' does not contain a definition for 'Startup' and no extension method 'Startup' accepting a first argument of type 'ExcelWarehouse.ExcelWarehouse' could be found (are you missing a using directive or an assembly reference?)
    C:\ILOG\OPL63\examples\opl_interfaces\dotnet\x86_.net2005_8.0\CSharp\ExcelWarehouse\Excel2007AddIn\ExcelWarehouse.cs(254,18): error CS1061: 'ExcelWarehouse.ExcelWarehouse' does not contain a definition for 'Shutdown' and no extension method 'Shutdown' accepting a first argument of type 'ExcelWarehouse.ExcelWarehouse' could be found (are you missing a using directive or an assembly reference?)
    C:\ILOG\OPL63\examples\opl_interfaces\dotnet\x86_.net2005_8.0\CSharp\ExcelWarehouse\Excel2007AddIn\ExcelWarehouse.cs(187,47): warning CS0168: The variable 'ex' is declared but never used
    C:\ILOG\OPL63\examples\opl_interfaces\dotnet\x86_.net2005_8.0\CSharp\ExcelWarehouse\Excel2007AddIn\ExcelWarehouse.Designer.cs(55,18): error CS1061: 'Excel2007AddIn.ExcelWarehouse' does not contain a definition for 'InternalStartup' and no extension method 'InternalStartup' accepting a first argument of type 'Excel2007AddIn.ExcelWarehouse' could be found (are you missing a using directive or an assembly reference?)
     
    Compile complete -- 15 errors, 1 warnings
    ========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: OPL CPLEX Analyst Studio-VSTO 'Warehouse example' Problems- Please Help

    Posted Mon June 14, 2010 11:33 PM

    Originally posted by: SystemAdmin


    Looking at the log, it seems that you are using a project for Visual Studio 2005 in Visual Studio 2008.
    If you work with Visual Studio 2008, could you try the example located in C:\ILOG\OPL63\examples\opl_interfaces\dotnet\x86_windows_vs2008\CSharp\ExcelWarehouse\Excel2007AddIn ?
    #CPLEXOptimizers
    #DecisionOptimization