Initializing help system before first use

Facility location problem - Data as arrays or collections


Type: Facility location
Rating: 2 (easy-medium)
Description: Solve a facility location problem for which data is given as collections.
File(s): FacilityLocationArray.java, FacilityLocationCollection.java


FacilityLocationArray.java
// (c) 2023-2024 Fair Isaac Corporation

import static com.dashoptimization.objects.Utils.sum;

import com.dashoptimization.ColumnType;
import com.dashoptimization.XPRSenumerations;
import com.dashoptimization.objects.Variable;
import com.dashoptimization.objects.XpressProblem;

/**
 * This example demonstrates some modeling devices. We model a very simple
 * facility location problem: We have customers and facilities. The constraints
 * are: - each customer must be served from exactly one facility - customers can
 * only be served from open facilities - customer demand must be satisfied -
 * facility capacity must not be exceeded We minimize the sum of transport cost
 * (between customer and facility) and the cost for opening a facility. In this
 * example data is kept in arrays.
 */
public class FacilityLocationArray {
    /** Customer description. */
    private static final class Customer {
        /** Name of customer. */
        public final String name;
        /** Demand for customer. */
        public final double demand;

        public Customer(String name, double demand) {
            this.demand = demand;
            this.name = name;
        }
    }

    /** Facility descriptor. */
    private static final class Facility {
        /** Name of facility. */
        public final String name;
        /** Capacity of facility. */
        public final double capacity;
        /** Cost for opening this facility. */
        public final double cost;

        public Facility(String name, double capacity, double cost) {
            this.name = name;
            this.capacity = capacity;
            this.cost = cost;
        }
    }

    /** Customers in this example. */
    private static final Customer[] customers = new Customer[] { new Customer("Customer 1", 80),
            new Customer("Customer 2", 270), new Customer("Customer 3", 250) };
    /** Facilities in this example. */
    private static final Facility[] facilities = new Facility[] { new Facility("Facility 1", 500, 1000),
            new Facility("Facility 2", 500, 1000), new Facility("Facility 3", 500, 1000) };
    /** Cost for transporting one unit between customer and facility. */
    private static final double[][] transportCost = new double[][] { new double[] { 4, 5, 6 }, new double[] { 6, 4, 3 },
            new double[] { 9, 7, 4 } };

    public static void main(String[] args) {
        try (XpressProblem prob = new XpressProblem()) {
            Variable[] y = prob.addVariables(facilities.length).withType(ColumnType.Binary)
                    .withName(f -> facilities[f].name).toArray();
            Variable[][] x = prob.addVariables(facilities.length, customers.length).withLB(0.0)
                    .withUB(Double.POSITIVE_INFINITY)
                    .withName((f, c) -> String.format("x[%s,%s]", facilities[f].name, customers[c].name)).toArray();

            // for each customer c
            // sum(f=1..m) x[f,c] = d
            prob.addConstraints(customers.length, c -> sum(facilities.length, f -> x[f][c]).eq(customers[c].demand));

            // for each facility f
            // sum(c=1..n) x[f,c] <= capacity[j] * y[f]
            prob.addConstraints(facilities.length, f -> sum(x[f]).leq(y[f].mul(facilities[f].capacity)));

            // minimize sum(f=1..m) cost[f] * y[f] +
            // sum(c=1..n) sum(f=1..m) cost[f,c] * x[f,c]
            prob.setObjective(sum(sum(facilities.length, f -> y[f].mul(facilities[f].cost)),
                    sum(customers.length, c -> sum(facilities.length, f -> x[f][c].mul(transportCost[f][c])))));

            prob.writeProb("facilitylocationarray.lp", "l");

            prob.optimize();
            if (prob.attributes().getSolStatus() != XPRSenumerations.SolStatus.OPTIMAL)
                throw new RuntimeException("failed to optimize with status " + prob.attributes().getSolStatus());
            double[] sol = prob.getSolution();
            for (int f = 0; f < facilities.length; ++f) {
                if (y[f].getValue(sol) > 0.5) {
                    System.out.println("Facility " + facilities[f].name + " is open, serves");
                    for (int c = 0; c < customers.length; ++c) {
                        if (x[f][c].getValue(sol) > 0.0)
                            System.out.println("  " + customers[c].name + ": " + x[f][c].getValue(sol));
                    }
                }
            }
        }
    }
}

FacilityLocationCollection.java
// (c) 2023-2024 Fair Isaac Corporation

import static com.dashoptimization.objects.Utils.sum;
import static java.util.Arrays.asList;

import java.util.List;
import java.util.Map;

import com.dashoptimization.ColumnType;
import com.dashoptimization.XPRSenumerations;
import com.dashoptimization.maps.HashMap2;
import com.dashoptimization.objects.Variable;
import com.dashoptimization.objects.XpressProblem;

/**
 * This example demonstrates some modeling devices. We model a very simple
 * facility location problem: We have customers and facilities. The constraints
 * are: - each customer must be served from exactly one facility - customers can
 * only be served from open facilities - customer demand must be satisfied -
 * facility capacity must not be exceeded We minimize the sum of transport cost
 * (between customer and facility) and the cost for opening a facility. In this
 * example data is kept in collections.
 */
public class FacilityLocationCollection {
    /** Customer description. */
    private static final class Customer {
        /** Name of customer. */
        public final String name;
        /** Demand for customer. */
        public final double demand;

        public Customer(String name, double demand) {
            this.demand = demand;
            this.name = name;
        }
    }

    /** Facility descriptor. */
    private static final class Facility {
        /** Name of facility. */
        public final String name;
        /** Capacity of facility. */
        public final double capacity;
        /** Cost for opening this facility. */
        public final double cost;

        public Facility(String name, double capacity, double cost) {
            this.name = name;
            this.capacity = capacity;
            this.cost = cost;
        }
    }

    private static final List<Customer> customers = asList(new Customer("Customer 1", 80),
            new Customer("Customer 2", 270), new Customer("Customer 3", 250));
    private static final List<Facility> facilities = asList(new Facility("Facility 1", 500, 1000),
            new Facility("Facility 2", 500, 1000), new Facility("Facility 3", 500, 1000));

    private static final HashMap2<Facility, Customer, Double> transportCost = new HashMap2<Facility, Customer, Double>();
    static {
        transportCost.put(facilities.get(0), customers.get(0), 4.0);
        transportCost.put(facilities.get(0), customers.get(1), 5.0);
        transportCost.put(facilities.get(0), customers.get(2), 6.0);
        transportCost.put(facilities.get(1), customers.get(0), 6.0);
        transportCost.put(facilities.get(1), customers.get(1), 4.0);
        transportCost.put(facilities.get(1), customers.get(2), 3.0);
        transportCost.put(facilities.get(2), customers.get(0), 9.0);
        transportCost.put(facilities.get(2), customers.get(1), 7.0);
        transportCost.put(facilities.get(2), customers.get(2), 4.0);
    }

    public static void main(String[] args) {
        try (XpressProblem prob = new XpressProblem()) {
            Map<Facility, Variable> y = prob.addVariables(facilities).withType(ColumnType.Binary).withName(f -> f.name)
                    .toMap();
            HashMap2<Facility, Customer, Variable> x = prob.addVariables(facilities, customers).withLB(0.0)
                    .withUB(Double.POSITIVE_INFINITY).withName((f, c) -> String.format("x[%s,%s]", f.name, c.name))
                    .toMap();

            // for each customer c
            // sum(f=1..m) x[f,c] = d
            prob.addConstraints(customers, c -> sum(facilities, f -> x.get(f, c)).eq(c.demand));

            // for each facility f
            // sum(c=1..n) x[f,c] <= capacity[j] * y[f]
            prob.addConstraints(facilities, f -> sum(customers, c -> x.get(f, c)).leq(y.get(f).mul(f.capacity)));

            // minimize sum(j=1..m) cost[j] * y[j] +
            // sum(i=1..n) sum(j=1..m) cost[f,c] * x[f,c]
            prob.setObjective(sum(sum(facilities, f -> y.get(f).mul(f.cost)),
                    sum(customers.stream(), c -> sum(facilities, f -> x.get(f, c).mul(transportCost.get(f, c))))));

            prob.writeProb("facilitylocationcollection.lp", "l");

            prob.optimize();
            if (prob.attributes().getSolStatus() != XPRSenumerations.SolStatus.OPTIMAL)
                throw new RuntimeException("failed to optimize with status " + prob.attributes().getSolStatus());
            double[] sol = prob.getSolution();
            for (Facility f : facilities) {
                if (y.get(f).getValue(sol) > 0.5) {
                    System.out.println("Facility " + f.name + " is open, serves");
                    for (Customer c : customers) {
                        if (x.get(f, c).getValue(sol) > 0.0)
                            System.out.println("  " + c.name + ": " + x.get(f, c).getValue(sol));
                    }
                }
            }
        }
    }
}

© 2001-2024 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.