public void Run(int iModel, string sModelPath, string sDataPath) { ResultsWindow resWindow = new ResultsWindow(); // Create an AMPL instance using (AMPL a = new AMPL()) { DataFrame Gens; DataFrame P; a.SetOption("solver", "gurobi"); a.Read(sModelPath); a.ReadData(sDataPath); // Solve a.Solve(); // Get objective entity by AMPL name string sTotalCost = a.GetObjective("Total_Cost").Value.ToString("0.00"); int iNumGenUnits = Convert.ToInt16(a.GetSet("GENERATORS").Size.ToString()); if (iModel == CTUC_ES | iModel == CTUC_NON_MARKET_ES) { int iNumESUnits = a.GetSet("ENERGY_ST").Size; } switch (iModel) { case DTUC: Gens = a.GetParameter("GenData").GetValues(); P = a.GetVariable("P").GetValues(); DataTable dtP = generate_DT_2D_table(P); DataTable dtPRamp = calculate_ramping(dtP, true); DataTable dtI = generate_DT_2D_table(a.GetVariable("I").GetValues()); DataTable dtLMP = generate_1D_table(a.GetConstraint("Power_Balance").GetValues()); resWindow.ProcessDTUCResults(dtP, dtI, dtLMP, sTotalCost, dtPRamp); break; case CTUC: DataTable CTUC_dtP = array2d_to_datatable( CT_P_Post_Mortem(Dataframe_to_3d_array(a.GetVariable("G_H").GetValues(), iNumGenUnits, 24, 4), iNumGenUnits) ); DataTable CTUC_dtLamda = array1d_to_datatable( CT_Price_Post_Mortem(Dataframe_to_2d_array(a.GetConstraint("Load_Balance1").GetValues(), 24, 4, true), Dataframe_to_2d_array(a.GetConstraint("Load_Balance2").GetValues(), 24, 4, true), iNumGenUnits) ); DataTable CTUC_dtPRamp = calculate_ramping1( CT_P_Post_Mortem( Dataframe_to_3d_array(a.GetVariable("G_H").GetValues(), iNumGenUnits, 24, 4), iNumGenUnits) ); DataTable CTUC_dtI = generate_DT_2D_table(a.GetVariable("I").GetValues()); resWindow.ProcessCTUCResults(CTUC_dtP, CTUC_dtPRamp, CTUC_dtLamda, sTotalCost, CTUC_dtI); break; case CTUC_ES: double[,] arr_G_cont = CT_P_Post_Mortem( Dataframe_to_3d_array(a.GetVariable("G_H").GetValues(), iNumGenUnits, 24, 4), iNumGenUnits); double[] arr_Lambda_cont = CT_Price_Post_Mortem( Dataframe_to_2d_array(a.GetConstraint("Load_Balance1").GetValues(), 24, 4, true), Dataframe_to_2d_array(a.GetConstraint("Load_Balance2").GetValues(), 24, 4, true), iNumGenUnits); double[,] arr_ES_G_cont = CT_ES_CH_DIS_Post_Mortem( Dataframe_to_3d_array(a.GetVariable("D_H_S").GetValues(), iNumGenUnits, 24, 4), iNumESUnits); double[,] arr_ES_D_cont = CT_ES_CH_DIS_Post_Mortem( Dataframe_to_3d_array(a.GetVariable("G_H_S").GetValues(), iNumGenUnits, 24, 4), iNumESUnits); double[,] arr_ES_E_cont = CT_ES_Energy_Post_Mortem( Dataframe_to_3d_array(a.GetVariable("E_B_S").GetValues(), iNumGenUnits, 24, 5), iNumESUnits); double[,] arr_Gamma_E_cont = CT_ES_Gamma_Post_Mortem( Dataframe_to_3d_array(a.GetConstraint("Integral1_INI").GetValues(), iNumGenUnits, 24, 5), Dataframe_to_3d_array(a.GetConstraint("ES_minE").GetValues(), iNumGenUnits, 24, 5), Dataframe_to_3d_array(a.GetConstraint("ES_maxE").GetValues(), iNumGenUnits, 24, 5), iNumESUnits); DataTable dtCTUC_ES_P = array2d_to_datatable(arr_G_cont); DataTable dtCTUC_ES_Lamda = array1d_to_datatable(arr_Lambda_cont); DataTable dtCTUC_ES_PRamp = calculate_ramping1(arr_G_cont); DataTable dtCTUC_ES_I = generate_DT_2D_table(a.GetVariable("I").GetValues()); DataTable dtCTUC_ES_CHP = array2d_to_datatable(arr_ES_G_cont); DataTable dtCTUC_ES_CHPR = calculate_ramping1(arr_ES_G_cont); DataTable dtCTUC_ES_DISP = array2d_to_datatable(arr_ES_D_cont); DataTable dtCTUC_ES_DISPR = calculate_ramping1(arr_ES_D_cont); DataTable dtCTUC_ES_E = array2d_to_datatable(arr_ES_E_cont); DataTable dtCTUC_ES_NISSE = array2d_to_datatable(arr_Gamma_E_cont); resWindow.ProcessCTUCwithES_MarketResults(dtCTUC_ES_P, dtCTUC_ES_I, dtCTUC_ES_Lamda, sTotalCost, dtCTUC_ES_PRamp, dtCTUC_ES_CHP, dtCTUC_ES_CHPR, dtCTUC_ES_DISP, dtCTUC_ES_DISPR, dtCTUC_ES_NISSE, dtCTUC_ES_E); break; case CTUC_NON_MARKET_ES: double[,] arr_G_cont_NM = CT_P_Post_Mortem( Dataframe_to_3d_array(a.GetVariable("G_H").GetValues(), iNumGenUnits, 24, 4), iNumGenUnits); double[] arr_Lambda_cont_NM = CT_Price_Post_Mortem( Dataframe_to_2d_array(a.GetConstraint("Load_Balance1").GetValues(), 24, 4, true), Dataframe_to_2d_array(a.GetConstraint("Load_Balance2").GetValues(), 24, 4, true), iNumGenUnits); double[,] arr_ES_G_cont_NM = CT_ES_CH_DIS_Post_Mortem( Dataframe_to_3d_array(a.GetVariable("D_H_S").GetValues(), iNumGenUnits, 24, 4), iNumESUnits); double[,] arr_ES_D_cont_NM = CT_ES_CH_DIS_Post_Mortem( Dataframe_to_3d_array(a.GetVariable("G_H_S").GetValues(), iNumGenUnits, 24, 4), iNumESUnits); double[,] arr_ES_E_cont_NM = CT_ES_Energy_Post_Mortem( Dataframe_to_3d_array(a.GetVariable("E_B_S").GetValues(), iNumGenUnits, 24, 5), iNumESUnits); double[,] arr_Gamma_E_cont_NM = CT_ES_Gamma_Post_Mortem( Dataframe_to_3d_array(a.GetConstraint("Integral1_INI").GetValues(), iNumGenUnits, 24, 5), Dataframe_to_3d_array(a.GetConstraint("ES_minE").GetValues(), iNumGenUnits, 24, 5), Dataframe_to_3d_array(a.GetConstraint("ES_maxE").GetValues(), iNumGenUnits, 24, 5), iNumESUnits); DataTable dtCTUC_ES_NM_P = array2d_to_datatable(arr_G_cont_NM); DataTable dtCTUC_ES_NM_Lamda = array1d_to_datatable(arr_Lambda_cont_NM); DataTable dtCTUC_ES_NM_PRamp = calculate_ramping1(arr_G_cont_NM); DataTable dtCTUC_ES_NM_I = generate_DT_2D_table(a.GetVariable("I").GetValues()); DataTable dtCTUC_ES_NM_CHP = array2d_to_datatable(arr_ES_G_cont_NM); DataTable dtCTUC_ES_NM_CHPR = calculate_ramping1(arr_ES_G_cont_NM); DataTable dtCTUC_ES_NM_DISP = array2d_to_datatable(arr_ES_D_cont_NM); DataTable dtCTUC_ES_NM_DISPR = calculate_ramping1(arr_ES_D_cont_NM); DataTable dtCTUC_ES_NM_E = array2d_to_datatable(arr_ES_E_cont_NM); DataTable dtCTUC_ES_NM_NISSE = array2d_to_datatable(arr_Gamma_E_cont_NM); resWindow.ProcessCTUCwithES_MarketResults(dtCTUC_ES_NM_P, dtCTUC_ES_NM_I, dtCTUC_ES_NM_Lamda, sTotalCost, dtCTUC_ES_NM_PRamp, dtCTUC_ES_NM_CHP, dtCTUC_ES_NM_CHPR, dtCTUC_ES_NM_DISP, dtCTUC_ES_NM_DISPR, dtCTUC_ES_NM_NISSE, dtCTUC_ES_NM_E); break; default: P = a.GetVariable("P").GetValues(); break; } } }
/// <summary> /// This example shows a simple AMPL iterative procedure implemented in AMPL. /// Must be executed with a solver supporting the suffix dunbdd /// </summary> /// <param name="args"> /// The first string, if present, should point to the models directory /// </param> public static int Main(string[] args) { string modelDirectory = ((args != null) && (args.Length > 0)) ? args[0] : "../../models"; /* * // If the AMPL installation directory is not in the system search path: * ampl.Environment env = new ampl.Environment( * "full path to the AMPL installation directory"); * // Create an AMPL instance * using (AMPL a = new AMPL(env)) {} */ // Create an AMPL instance using (AMPL ampl = new AMPL()) { // Must be solved with a solver supporting the suffix dunbdd ampl.SetOption("solver", "cplex"); modelDirectory += "/locationtransportation"; ampl.SetOption("presolve", false); ampl.SetOption("omit_zero_rows", true); // Read the model. ampl.Read(modelDirectory + "/trnloc2.mod"); ampl.ReadData(modelDirectory + "/trnloc.dat"); // TODO: set data // programmatically // Get references to AMPL's model entities for easy access. Objective shipCost = ampl.GetObjective("Ship_Cost"); Variable maxShipCost = ampl.GetVariable("Max_Ship_Cost"); Variable buildVar = ampl.GetVariable("Build"); Constraint supply = ampl.GetConstraint("Supply"); Constraint demand = ampl.GetConstraint("Demand"); Parameter numCutParam = ampl.GetParameter("nCUT"); Parameter cutType = ampl.GetParameter("cut_type"); Parameter buildParam = ampl.GetParameter("build"); Parameter supplyPrice = ampl.GetParameter("supply_price"); Parameter demandPrice = ampl.GetParameter("demand_price"); numCutParam.Set(0); maxShipCost.Value = 0; double[] initialBuild = new double[ampl.GetSet("ORIG").Size]; for (int i = 0; i < initialBuild.Length; i++) { initialBuild[i] = 1; } buildParam.SetValues(initialBuild); int numCuts; for (numCuts = 1; ; numCuts++) { Console.WriteLine("Iteration {0}", numCuts); ampl.Display("build"); // Solve the subproblem. ampl.Eval("solve Sub;"); string result = shipCost.Result; if (result.Equals("infeasible")) { // Add a feasibility cut. numCutParam.Set(numCuts); cutType.Set(new ampl.Tuple(numCuts), "ray"); DataFrame dunbdd = supply.GetValues("dunbdd"); foreach (var row in dunbdd) { supplyPrice.Set(new ampl.Tuple(row[0], numCuts), row[1].Dbl); } dunbdd = demand.GetValues("dunbdd"); foreach (var row in dunbdd) { demandPrice.Set(new ampl.Tuple(row[0], numCuts), row[1].Dbl); } } else if (shipCost.Value > maxShipCost.Value + 0.00001) { // Add an optimality cut. numCutParam.Set(numCuts); cutType.Set(new ampl.Tuple(numCuts), "point"); ampl.Display("Ship"); DataFrame duals = supply.GetValues(); foreach (var row in duals) { supplyPrice.Set(new ampl.Tuple(row[0], numCuts), row[1].Dbl); } duals = demand.GetValues(); foreach (var row in duals) { demandPrice.Set(new ampl.Tuple(row[0], numCuts), row[1].Dbl); } } else { break; } // Re-solve the master problem. Console.WriteLine("RE-SOLVING MASTER PROBLEM"); ampl.Eval("solve Master;"); // Copy the data from the Build variable used in the master problem // to the build parameter used in the subproblem. DataFrame data = buildVar.GetValues(); buildParam.SetValues(data); } ampl.Display("Ship"); } return(0); }