patterncsharpMinor
Maximize profit on a trade route
Viewed 0 times
routetrademaximizeprofit
Problem
Fixed trade route with goods and prices and fixed cargo capacity.
How can I optimize profit?
No cost to trade or transport - just fixed capacity, route, and prices. All products use the same capacity, which is kind of artificial, but this is just a single pass starting with an empty vessel. It does a semi brute force on all possible trades
The example would be 3 port and 3 products, like apples, pears, and oranges trading at various prices.
Test:
Code:
```
public static Int32[,] PortsProducts(Int32[,] portsProducts)
{
int rowCount = portsProducts.GetLength(0);
int colCount = portsProducts.GetLength(1);
Int32[,] buySell = new Int32[rowCount, colCount];
Int32[,] buySellMax = new Int32[rowCount, colCount];
Int32[] highPrice = new Int32[colCount];
Int32[] lowPrice = new Int32[colCount];
List rowDisplay = new List();
Debug.WriteLine("portsProducts");
for (int i = 0; i portsProducts[i, j])
lowPrice[j] = portsProducts[i, j];
}
}
}
// build up a matrix of all possible buy sell hold for next rowCount - 1
// 0 hold, 1 is sell, 2 buy
List allBuySellOptions = new List ();
Int32[,] buySellOptions = new Int32[rowCount, colCount];
//Int32[,] buySellOptionsOld = new Int32[rowCount - 1, colCount];
for (int i = 0; i 0)
{ // sell
// sell first to clear capacity
profit += curProduct[j] * portsProducts[i, j];
curCapacity += curProduct[j];
buySell[i, j] = -curProduct[j];
curProduct[j] = 0;
}
else if(buySellOpt[i, j] == 1)
{
buyCount++;
}
}
if (buyCount > 0 && c
How can I optimize profit?
No cost to trade or transport - just fixed capacity, route, and prices. All products use the same capacity, which is kind of artificial, but this is just a single pass starting with an empty vessel. It does a semi brute force on all possible trades
The example would be 3 port and 3 products, like apples, pears, and oranges trading at various prices.
Test:
Int32[,] portsProducts = new Int32[,] { { 1, 1, 1 }, { 2, 1, 1 }, { 4, 2, 3 } };
Int32[,] answer = PortsProducts(portsProducts);Code:
```
public static Int32[,] PortsProducts(Int32[,] portsProducts)
{
int rowCount = portsProducts.GetLength(0);
int colCount = portsProducts.GetLength(1);
Int32[,] buySell = new Int32[rowCount, colCount];
Int32[,] buySellMax = new Int32[rowCount, colCount];
Int32[] highPrice = new Int32[colCount];
Int32[] lowPrice = new Int32[colCount];
List rowDisplay = new List();
Debug.WriteLine("portsProducts");
for (int i = 0; i portsProducts[i, j])
lowPrice[j] = portsProducts[i, j];
}
}
}
// build up a matrix of all possible buy sell hold for next rowCount - 1
// 0 hold, 1 is sell, 2 buy
List allBuySellOptions = new List ();
Int32[,] buySellOptions = new Int32[rowCount, colCount];
//Int32[,] buySellOptionsOld = new Int32[rowCount - 1, colCount];
for (int i = 0; i 0)
{ // sell
// sell first to clear capacity
profit += curProduct[j] * portsProducts[i, j];
curCapacity += curProduct[j];
buySell[i, j] = -curProduct[j];
curProduct[j] = 0;
}
else if(buySellOpt[i, j] == 1)
{
buyCount++;
}
}
if (buyCount > 0 && c
Solution
Code organization
Mutually exclusive conditions
Use more helper variables
In the
It would be easier to write and to read if you put those in local helper variables.
PortsProducts is one gigantic method. I suggest to decompose it to many small methods that each have a single clear responsibility, with a descriptive method name.Mutually exclusive conditions
portsProducts[i, j] will never be higher than the highest prices and lower than the lowest at the same time, so the 2nd if statement here should be an else if:if (highPrice[j] portsProducts[i, j])
lowPrice[j] = portsProducts[i, j];Use more helper variables
In the
buySellBump2 method there are many references to buySellOptions[i, j] and portsProducts[i, j].It would be easier to write and to read if you put those in local helper variables.
Code Snippets
if (highPrice[j] < portsProducts[i, j])
highPrice[j] = portsProducts[i, j];
if (lowPrice[j] > portsProducts[i, j])
lowPrice[j] = portsProducts[i, j];Context
StackExchange Code Review Q#150832, answer score: 5
Revisions (0)
No revisions yet.