#region License Information /* HeuristicLab * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using HeuristicLab.Data; using ILOG.OPL; namespace HeuristicLab.Problems.FacilityLocation.CplexSolver { public class FLPDataSource : CustomOplDataSource { #region Inputs public const string TotalDepots = "I"; public const string TotalCustomers = "J"; public const string OpeningCosts = "f"; public const string DepotCapacities = "s"; public const string CustomerDemands = "d"; public const string DeliveryCosts = "c"; #endregion #region Variables public const string CustomerDepotDeliverAmount = "x"; public const string DepotOpening = "y"; public const string CustomerDepotAssignment = "z"; #endregion private FacilityLocationProblem problem; public FLPDataSource(OplFactory factory, FacilityLocationProblem problem) : base(factory) { this.problem = problem; } public override void CustomRead() { NewElement(TotalDepots, () => Add(problem.DepotCapacitiesParameter.Value.Length)); NewElement(TotalCustomers, () => Add(problem.CustomerDemandsParameter.Value.Length)); NewElement(OpeningCosts, () => Add(problem.OpeningCostsParameter.Value)); NewElement(DepotCapacities, () => Add(problem.DepotCapacitiesParameter.Value)); NewElement(CustomerDemands, () => Add(problem.CustomerDemandsParameter.Value)); NewElement(DeliveryCosts, () => Add(problem.DeliveryCostsParameter.Value)); } protected void NewElement(string name, Action add) { DataHandler.StartElement(name); add(); DataHandler.EndElement(); } protected void Add(int value) { DataHandler.AddIntItem(value); } protected void Add(double value) { DataHandler.AddNumItem(value); } protected void Add(DoubleArray values) { DataHandler.StartArray(); foreach (double value in values) { DataHandler.AddNumItem(value); } DataHandler.EndArray(); } protected void Add(DoubleMatrix values) { DataHandler.StartArray(); for (var i = 0; i < values.Rows; i++) { DataHandler.StartArray(); for (var j = 0; j < values.Columns; j++) { Add(values[i, j]); } DataHandler.EndArray(); } DataHandler.EndArray(); } } }