This commit is contained in:
nik
2026-05-20 16:20:45 -07:00
commit 007710b5a8
91 changed files with 450401 additions and 0 deletions

View File

@@ -0,0 +1,128 @@
import java.util.*;
/**
* The Allocation class represents an unmodifiable relief solution.
* It provides methods to retrieve the total cost and total helped population
* of the solution. The ordering of the regions in the solution determines
* the population that can be helped.
*/
public class Allocation {
private List<Region> regions;
/**
* Creates a new Allocation object representing the given regions.
* @param regions the regions in the solution
*/
private Allocation(List<Region> regions) {
this.regions = new ArrayList<>(regions);
}
/**
* Creates a new Allocation object with no regions in it.
*/
public Allocation() {
this(new ArrayList<>());
}
/**
* Returns a copy of this allocation's regions.
*/
public List<Region> getRegions() {
return new ArrayList<>(regions);
}
/**
* Returns a new Allocation with the contents of this allocation
* and the passed in region added to it.
* @param r Region to be added to the end of the new Allocation.
* @return a new Allocation with r added to it.
*/
public Allocation withRegion(Region r) {
if (regions.contains(r)) {
throw new IllegalArgumentException("Allocation already contains region " + r);
}
List<Region> newRegions = new ArrayList<>(regions);
newRegions.add(r);
return new Allocation(newRegions);
}
/**
* Returns a new Allocation with the contents of this allocation
* and the passed in region removed from it.
* @param r Region to be removed from the new Allocation.
* @return a new Allocation with r removed from it.
*/
public Allocation withoutRegion(Region r) {
if (!regions.contains(r)) {
throw new IllegalArgumentException("Allocation doesn't contain region " + r);
}
List<Region> newRegions = new ArrayList<>(regions);
newRegions.remove(r);
return new Allocation(newRegions);
}
/**
* Returns the number of regions in this Allocation.
*/
public int size() {
return regions.size();
}
/**
* Calculates and returns the total population that can be helped
* by this Allocation.
* @return the total population that can be helped by this Allocation.
*/
public int totalPeople() {
int total = 0;
for (Region r : regions) {
total += r.getPopulation();
}
return total;
}
/**
* Calculates and returns the combined cost of this Allocation.
* @return the combined cost of this Allocation.
*/
public double totalCost() {
double total = 0;
for (int i = 0; i < regions.size(); i++) {
total += regions.get(i).getCost(i);
}
return total;
}
/**
* Returns a String representation of an Allocation object in the format:
* "[Region, ..., Region]" where each Region is in its string representation.
* @return the String representation of an Allocation object
*/
public String toString() {
return regions.toString();
}
/**
* Compares the specified object with this allocation for equality. Returns true if the
* specified object is also an Allocation and the two Allocations have the same
* collection of regions.
* @param other object to be compared for equality with this allocation
* @return true if the specified object is equal to this allocation
*/
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (!(other instanceof Allocation)) {
return false;
}
Allocation otherAlloc = (Allocation)other;
return this.regions.equals(otherAlloc.getRegions());
}
public int hashCode() {
return regions.hashCode();
}
}

View File

@@ -0,0 +1,73 @@
import java.util.*;
public class Client {
private static Random rand = new Random();
public static void main(String[] args) throws Exception {
// List<Region> scenario = createRandomScenario(10, 10, 100, 1000, 100000);
List<Region> scenario = createSimpleScenario();
System.out.println(scenario);
double budget = 2000;
Set<Allocation> allocations = generateOptions(budget, scenario);
printAllocations(allocations);
}
public static Set<Allocation> generateOptions(double budget, List<Region> sites) {
Set<Allocation> allocations = new HashSet<>();
return generateOptions(budget, sites, allocations);
}
private static Set<Allocation> generateOptions(double budget, List<Region> sites,
Set<Allocation> allocations) {
if (sites.isEmpty()) {
return allocations;
}
Region temp = sites.remove();
if (budget >= temp.baseCost) {
budget -= temp.baseCost;
}
}
///////////////////////////////////////////////////////////////////////////
// PROVIDED HELPER METHODS - **DO NOT MODIFY ANYTHING BELOW THIS LINE!** //
///////////////////////////////////////////////////////////////////////////
public static void printAllocations(Set<Allocation> allocations) {
System.out.println("All Allocations:");
for (Allocation a : allocations) {
System.out.println(" " + a);
}
}
public static List<Region> createRandomScenario(int numLocs, int minPop, int maxPop,
double minCostPer, double maxCostPer) {
List<Region> result = new ArrayList<>();
for (int i = 0; i < numLocs; i++) {
int pop = rand.nextInt(minPop, maxPop + 1);
double cost = rand.nextDouble(minCostPer, maxCostPer) * pop;
result.add(new Region("Region #" + i, pop, round2(cost)));
}
return result;
}
public static List<Region> createSimpleScenario() {
List<Region> result = new ArrayList<>();
result.add(new Region("Region #1", 50, 500));
result.add(new Region("Region #2", 100, 700));
result.add(new Region("Region #3", 60, 1000));
result.add(new Region("Region #4", 20, 1000));
result.add(new Region("Region #5", 200, 900));
return result;
}
private static double round2(double num) {
return Math.round(num * 100) / 100.0;
}
}

View File

@@ -0,0 +1,84 @@
/**
* The Region class represents a geographical location with a name, population, and cost.
* It provides methods to retrieve the population and cost of the location,
* as well as a method to generate a string representation of the object.
*/
public class Region {
private String name;
private int population;
private double baseCost;
/**
* Creates a new Region object with the given name, population, and cost.
* @param name the name of the location
* @param pop the population of the location
* @param baseCost the base cost of the location
*/
public Region(String name, int pop, double baseCost) {
this.name = name;
this.population = pop;
this.baseCost = baseCost;
}
/**
* Returns the population of the location
* @return the population of the location
*/
public int getPopulation() { return this.population; }
/**
* Returns the cost of the location
* @param index a number indicating when this region is provided relief. A larger value for
* index indicates that this region is helped later.
* @return the cost of providing relief to this region. Regions that are
* helped later (i.e. with a larger index value) have a higher cost.
*/
public double getCost(int index) {
return (1 + 0.1 * index) * this.baseCost;
}
/**
* Returns a String representation of a Region object in the format:
* "<name>: pop. <population>, cost: $<cost>"
* @return the String representation of a Region object
*/
public String toString() {
return name + ": pop. " + population + ", base cost: $" + baseCost;
}
/**
* Compares the specified object with this location for equality. Returns true if the
* specified object is also a location and the two locations have the
* same name, population, and cost.
* @param other object to be compared for equality with this location
* @return true if the specified object is equal to this location
*/
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (!(other instanceof Region)) {
return false;
}
Region otherLoc = (Region)other;
return this.name.equals(otherLoc.name) &&
this.population == otherLoc.population &&
this.baseCost == otherLoc.baseCost;
}
/**
* Returns the hash code value for this location
* @return the hash code value for this location
*/
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + Integer.hashCode(population);
result = 31 * result + Double.hashCode(baseCost);
return result;
}
}