Skip to content

Commit

Permalink
Working on supply chain
Browse files Browse the repository at this point in the history
  • Loading branch information
brianreedpowers committed Oct 11, 2023
1 parent aff4669 commit f9ec32a
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public class GenericMarket {
String name;
String location;
double[] demandLoin;
double[] demandPackaged;

Expand All @@ -12,6 +13,15 @@ public GenericMarket(String name,
this.demandLoin = demandLoin;
this.demandPackaged = demandPackaged;
}
public GenericMarket(String name,
String location,
double[] demandLoin,
double[] demandPackaged){
this.name = name;
this.location = location;
this.demandLoin = demandLoin;
this.demandPackaged = demandPackaged;
}

public double[] getDemandLoin() {
return demandLoin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public class GenericPort {
String name;
String location;
double[] landings;


Expand All @@ -10,6 +11,13 @@ public GenericPort(String name, double[] landings){
this.landings = landings;
}

public GenericPort(String name, String location, double[] landings){
this.name = name;
this.location = location;
this.landings = landings;
}


public double[] getLandings() {
return landings;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

public class GenericProcessor {
String name;
String location;
double[] maxOutput;

//cannery transformation ability
double[] transformationAbility;

double[] processingCost;

public GenericProcessor(double[] maxOutput,
Expand All @@ -17,6 +17,17 @@ public GenericProcessor(double[] maxOutput,
this.processingCost = processingCost;
}

public GenericProcessor(String name,
String location,
double[] maxOutput,
double[] transformationAbility,
double[] processingCost){
this.name = name;
this.location = location;
this.maxOutput = maxOutput;
this.transformationAbility = transformationAbility;
this.processingCost = processingCost;
}
public GenericProcessor(String name,
double[] maxOutput,
double[] transformationAbility,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package uk.ac.ox.oxfish.model.market.supplychain;

import uk.ac.ox.oxfish.biology.Species;

public class ImportTarriff {
private Species species;
private int origin;
private int destination;
private double rate_raw, rate_loin, rate_packaged;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@
import com.google.ortools.Loader;
import com.google.ortools.linearsolver.*;

import uk.ac.ox.oxfish.biology.Species;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class SupplyChain {
List<String> speciesNames = new ArrayList<>();
List<String> locationNames = new ArrayList<>();

MPSolver solver;
double infinity = java.lang.Double.POSITIVE_INFINITY;

Expand Down Expand Up @@ -368,6 +382,147 @@ public double[] getPortPrices(int index){
}*/


public List<GenericPort> readPortsFromCSV(String path){
List<GenericPort> ports = new ArrayList<>();
Path pathToFile = Paths.get(path);

try(BufferedReader br = Files.newBufferedReader(pathToFile, StandardCharsets.US_ASCII)){
String line = br.readLine(); //First line is a header
line = br.readLine();
while(line != null){
String[] attributes = line.split(",");
GenericPort port = createPort(attributes);
ports.add(port);
line=br.readLine();
}

} catch (IOException ioe) {
ioe.printStackTrace();
}
return ports;
}

public List<GenericProcessor> readFacilitiesFromCSV(String path){
List<GenericProcessor> facilities = new ArrayList<>();
Path pathToFile = Paths.get(path);
try(BufferedReader br = Files.newBufferedReader(pathToFile, StandardCharsets.US_ASCII)){
String line = br.readLine(); //First line is a header
line = br.readLine();
while(line != null){
String[] attributes = line.split(",");
GenericProcessor facility = createFacility(attributes);
facilities.add(facility);
line=br.readLine();
}

} catch (IOException ioe) {
ioe.printStackTrace();
}
return facilities;

}

public List<GenericMarket> readMarketsFromCSV(String path){
List<GenericMarket> markets = new ArrayList<>();
Path pathToFile = Paths.get(path);
try(BufferedReader br = Files.newBufferedReader(pathToFile, StandardCharsets.US_ASCII)){
String line = br.readLine(); //First line is a header
line = br.readLine();
while(line != null){
String[] attributes = line.split(",");
GenericMarket market = createMarket(attributes);
markets.add(market);
line=br.readLine();
}

} catch (IOException ioe) {
ioe.printStackTrace();
}
return markets;
}

public List<TransportCost> readTransportCostsFromCSV(String path){
List<TransportCost> costs = new ArrayList<>();
Path pathToFile = Paths.get(path);
try(BufferedReader br = Files.newBufferedReader(pathToFile, StandardCharsets.US_ASCII)){
String line = br.readLine(); //First line is a header
line = br.readLine();
while(line != null){
String[] attributes = line.split(",");
TransportCost cost = createTransportCost(attributes);
costs.add(cost);
line=br.readLine();
}

} catch (IOException ioe) {
ioe.printStackTrace();
}
return costs;
}




private static int getIndex(List<String> list, String token){
if(list.indexOf(token) > -1){
return (list.indexOf(token));
} else {
list.add(token);
return (list.indexOf(token));
}
}

private GenericPort createPort(String[] data){
String name = data[0];
String location = data[1];
int locationIndex = getIndex(locationNames,location);
double[] landings = new double[(data.length-2)/2];
//System.out.println(data.length);
for(int i=0; i<(data.length-2)/2; i++){
int speciesIndex = getIndex(speciesNames, data[i*2+2]);
// System.out.println(speciesIndex);
landings[speciesIndex] = Double.parseDouble(data[i*2+3]);
}
return new GenericPort(name, location, landings);
}

private GenericProcessor createFacility(String[] data){
String name = data[0];
String location = data[1];
int locationIndex = getIndex(locationNames, location);
double coldStorage = Double.parseDouble(data[2]);
double CTA = Double.parseDouble(data[3]);
double maxLoining = Double.parseDouble(data[4]);
double maxCanning = Double.parseDouble(data[5]);
return new GenericProcessor(name, location, new double[]{maxLoining,maxCanning},new double[]{CTA}, new double[]{41.52, 87.80-41.52} );
}

private TransportCost createTransportCost(String[] data){
String origin = data[0];
String destination = data[1];
double cost = Double.parseDouble(data[2]);
return new TransportCost(getIndex(locationNames, origin),
getIndex(locationNames, destination),
cost);
}


private GenericMarket createMarket(String[] data){
String name = data[0];
String location = data[1];
int locationIndex = getIndex(locationNames, location);
double canDemand = Double.parseDouble(data[2]);
//then
//loin demand per species
double[] loinDemand = new double[(data.length-3)/2];
//System.out.println(data.length);
for(int i=0; i<(data.length-3)/2; i++){
int speciesIndex = getIndex(speciesNames, data[i*2+3]);
// System.out.println(speciesIndex);
loinDemand[speciesIndex] = Double.parseDouble(data[i*2+4]);
}
return new GenericMarket(name, location,loinDemand, new double[]{canDemand} );
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package uk.ac.ox.oxfish.model.market.supplychain;

public class TransportCost {
int origin;
int destination;
double cost;

public TransportCost(int origin, int destination, double cost){
this.origin=origin;
this.destination=destination;
this.cost=cost;
}

public int getOrigin(){
return origin;
}
public int getDestination(){
return destination;
}

public double getCost() {
return cost;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package uk.ac.ox.oxfish.model.market;

import org.junit.jupiter.api.Test;
import uk.ac.ox.oxfish.model.market.supplychain.GenericMarket;
import uk.ac.ox.oxfish.model.market.supplychain.GenericPort;
import uk.ac.ox.oxfish.model.market.supplychain.GenericProcessor;
import uk.ac.ox.oxfish.model.market.supplychain.SupplyChain;
import uk.ac.ox.oxfish.model.market.supplychain.*;

import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

public class SupplyChainTest {



@Test
public void testSimpleSupplyChain(){
//Create Ports
Expand Down Expand Up @@ -61,6 +62,16 @@ public void testSimpleSupplyChain(){
1.5,
2.5,
1.1);

List<GenericPort> testPorts = testSupplyChain.readPortsFromCSV(Paths.get("inputs", "epo_inputs", "tests", "supply_chain", "ports.csv").toAbsolutePath().toString());
List<GenericProcessor> testFacilities = testSupplyChain.readFacilitiesFromCSV(Paths.get("inputs", "epo_inputs", "tests", "supply_chain", "facilities.csv").toAbsolutePath().toString());
List<GenericMarket> testMarkets = testSupplyChain.readMarketsFromCSV(Paths.get("inputs", "epo_inputs", "tests", "supply_chain", "demands.csv").toAbsolutePath().toString());
List<ImportTarriff> importTarriffs;
List<TransportCost> transportCosts = testSupplyChain.readTransportCostsFromCSV(Paths.get("inputs", "epo_inputs", "tests", "supply_chain", "transportation_costs.csv").toAbsolutePath().toString());


//breakpoint

testSupplyChain.initializeLP();
testSupplyChain.establishConstraints(0);
testSupplyChain.setObjective(0);
Expand Down

0 comments on commit f9ec32a

Please sign in to comment.