linear algebra - C# SolverFoundation find maximum combination of lengths -
i have list of brackets need find combination of them 4 brackets, best fits length.
these brackets , example need find out combination of these closest without going on 120 inches.
<?xml version="1.0" encoding="utf-8" ?> <brackets> <bracket> <partnumber>f0402583</partnumber> <length>42.09</length> </bracket> <bracket> <partnumber>f0402604</partnumber> <length>32.36</length> </bracket> <bracket> <partnumber>f0403826</partnumber> <length>46.77</length> </bracket> <bracket> <partnumber>f0402566</partnumber> <length>44.17</length> </bracket> <bracket> <partnumber>f0402289</partnumber> <length>20.55</length> </bracket> <bracket> <partnumber>f0402612</partnumber> <length>18.46</length> </bracket> <bracket> <partnumber>f0402606</partnumber> <length>30.28</length> </bracket> <bracket> <partnumber>f0403828</partnumber> <length>22.76</length> </bracket> </brackets>
i've tried figure out solverfoundation library i'm not math major , have little experience it...using excel solver simplex lp solving method solution 1 30.28" bracket, 1 42.09" bracket, , 1 46.77" bracket comes out 119.14"
here naive solution. have limited understanding of mathematics (most advanced combinatorics , such!), understand challenge. solution runs through possibilities filters valid solution (those total lengths < 120), sorts them length, , outputs first result.
i'm sure if solution require demonstrating understanding of mathematics concept, not way go looking solution - allow check solution.
the runtime of o(n^c).
public static void main(string[] args) { var lengths = new list<decimal>(new decimal[] { 42.09m, 32.36m, 46.77m, 44.17m, 20.55m, 18.46m, 30.28m, 22.76m }); var lst = new list<solution>(); (int = 0; < lengths.count; i++) { lst.add(new solution(new decimal[] { lengths[i] })); (int j = 0; j < lengths.count; j++) { lst.add(new solution(new decimal[] { lengths[i], lengths[j] })); (int k = 0; k < lengths.count; k++) { lst.add(new solution(new decimal[] { lengths[i], lengths[j], lengths[k] })); (int l = 0; l < lengths.count; l++) { lst.add(new solution(new decimal[] { lengths[i], lengths[j], lengths[k], lengths[l] })); } } } } var validsolution = (from sln in lst sln.value <= 120.00m && sln.isvalid select sln).orderbydescending(sln => sln.value) .first(); console.writeline(validsolution); }
and solution class:
public class solution : icomparable<solution> { public readonly int maxlengths = 4; public readonly decimal value; public readonly decimal maxvalue = 120.00m; public readonly bool isvalid; public readonly decimal[] lengths; public solution(decimal[] lengths) { this.lengths = lengths; if (lengths.length > 4) throw new argumentexception("too many lengths."); foreach (var dec in lengths) { if (dec <= 0.00m) throw new argumentexception(); value += dec; } isvalid = value < 120.00m; } public int compareto(solution other) { if (this.value > other.value) return 1; else if (this.value == other.value) return 0; else return -1; } public override string tostring() { var value = string.format("[solution] lengths:"); foreach (var d in lengths) { value += d + ", "; } value += this.value; return value.tostring(); } }
Comments
Post a Comment