Created
April 28, 2018 00:16
-
-
Save Vladis466/f900c07a62eea2cb4042bcddfdadeb9f to your computer and use it in GitHub Desktop.
Capacity of 1000000 gives a max value of approx 14.2593 BTC.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
namespace MiningOptimizationTesting | |
{ | |
/// <summary> | |
/// Test implementation of an algorithm to maximize potential returns for mining a block | |
/// Based on the knapsack 0-1 DP solution. | |
/// </summary> | |
public class BlockTransactionOptimizer | |
{ | |
static string[] fileLines; | |
static double[] transactionFees; | |
static double[] transactionSizes; | |
static readonly int CAPACITY = 1000000; | |
static readonly double REWARD = 12.5; | |
static readonly int QUANTITY = 12; | |
public static void Main(string[] args) | |
{ | |
string filepath = args[1]; | |
Console.WriteLine($"Loading file from path: {filepath}"); | |
if (File.Exists(filepath)) | |
{ | |
fileLines = File.ReadAllLines(filepath); | |
} | |
else | |
{ | |
Console.WriteLine("file not found"); | |
throw new FileNotFoundException($"{nameof(filepath)}"); | |
} | |
transactionFees = new double[QUANTITY]; | |
transactionSizes = new double[QUANTITY]; | |
for (int i = 1; i <= QUANTITY; i++) | |
{ | |
Console.WriteLine("\t" + fileLines[i]); | |
var tempItems = fileLines[i].Split(); | |
double.TryParse(tempItems[1], out transactionSizes[i-1]); | |
double.TryParse(tempItems[2], out transactionFees[i-1]); | |
} | |
double optimizedBlockFee = OptimizeBlockFees(QUANTITY, CAPACITY, transactionFees, transactionSizes); | |
var result =Math.Round(optimizedBlockFee, 4) + REWARD; | |
Console.WriteLine($"Capacity of {CAPACITY} gives a max value of {result} BTC"); | |
Console.ReadLine(); | |
} | |
static double OptimizeBlockFees(int n, double capacity, double[] v, double[] w) | |
{ | |
double[] m = new double[(int)capacity + 1]; | |
for (int i = 0; i < n; i++) | |
for (int j = (int)capacity; j >= 0; j--) | |
m[j] = j < w[i] ? m[j] : Math.Max(m[j], m[j -(int)w[i]] + v[i]); | |
return m[(int)capacity]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment