Created
April 3, 2012 16:14
-
-
Save mpickering/2293275 to your computer and use it in GitHub Desktop.
Simplex
This file contains 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
def parseInput(a,flag) | |
def handleOne(x) | |
if x.length == 1 or x =~ /^\+[a-z]$/ | |
1 | |
elsif x =~ /^-[a-z]$/ | |
-1 | |
else | |
x.chop.to_i | |
end | |
end | |
symbols=Hash.new | |
if flag == 'o' | |
if a =~ /^[\+|-]?\d*[a-z]{1}([\+|-]\d*[a-z]{1})+$/ | |
res = a.scan(/[\+|-]?\d*[a-z]{1}/) | |
else | |
return false | |
end | |
end | |
if flag == 'c' | |
if a=~ /^[\+|-]?\d*[a-z]{1}([\+|-]\d*[a-z]{1})+=\d+$/ | |
res = a.scan(/[\+|-]?\d*[a-z]/) | |
else return false | |
end | |
symbols['='] = a.split('=').last.to_i | |
end | |
mod=Hash['o',-1,'c',1] | |
for i in res | |
last=i.split('').last | |
if symbols.has_key?(last) | |
symbols[last]+=handleOne(i)*mod[flag] | |
else | |
symbols[last]=handleOne(i)*mod[flag] | |
end | |
end | |
symbols | |
end | |
#puts 'Enter Objective' | |
#a = gets.chomp | |
#puts parseInput(a,'o') | |
#while !parseInput(a,'o') | |
# puts 'Enter Objective' | |
# a=gets.chomp | |
#end | |
obj=parseInput('3x+2y+5z','o') | |
#puts 'Enter Contraints' | |
#a = gets.chomp | |
#cons=[] | |
#while !a.empty? | |
# parse=parseInput(a,'c') | |
# puts parse ? cons << parse : 'Invalid Cons' | |
# a = gets.chomp | |
#end | |
cons=['2x+2y+5z=60','4x+y+2z=56'].map{|x| parseInput(x,'c')} | |
nCons=cons.size | |
headings = obj.keys.concat(cons.map(&:keys).flatten).uniq | |
headings.concat((0...nCons).map{|x| "S%s" % x}) | |
rows = Hash.new | |
rows[0] = Hash['P',1] | |
headings.each{|x| rows[0][x] = obj.key?(x) ? obj[x] : 0} | |
rown=0 | |
cons.each{|item| rown+=1 | |
rows[rown]=Hash['P',0] | |
headings.each{|x| rows[rown][x] = cons[rown-1].key?(x) ? cons[rown-1][x] : 0} | |
rows[rown]['S%d' % (rown-1)] = 1 | |
} | |
puts rows | |
while !headings.select{|x| x.match(/^[a-z]$/) && rows[0][x]<0}.empty? | |
#select=rows[0].min{|a,b| rows[0][b] <=> rows[0][a]} | |
select = rows[0].sort_by(&:last).first[0] | |
puts select | |
comp = (1..nCons).min_by{|x| rows[x][select] > 0 ? (rows[x]['=']*1.0)/rows[x][select] : 1.0/0.0} | |
pivot = rows[comp][select]*1.0 | |
rows[comp].each{|key,value| rows[comp][key] = value/pivot} | |
(0..nCons).find_all{|x| x != comp}.each{|r| factor = rows[r][select]*(-1) | |
headings.each{|h| rows[r][h] = rows[r][h]+(rows[comp][h]*factor)}} | |
end | |
puts rows.each(&:inspect) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment