Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ワントップでないピラミッドの荷重計算
#!/usr/bin/env ruby
# gf-20190611.rb : ワントップでないピラミッドの荷重計算
# by takehikom
# git clone https://github.com/takehiko/gf.git
# ruby gf-20190611.rb
# gem install rubyXL
# ruby gf-20190611.rb -e
# ls -l gf1.xlsx gf1a.xlsx gf2.xlsx gf3.xlsx
require_relative "gf/lib/gf/root.rb"
# 最下段が5人,2段目が4人,3段目が3人の俵型ピラミッドの荷重計算
# ボトムアップによる構成
# https://twitter.com/ma85003/status/1137715222154698759
h = { :workbook_name => "gf1.xlsx" }
gf1 = GF::Formation.new(h)
gf1.level = 3
1.upto(3) do |i|
1.upto(6 - i) do |j|
n = gf1.compose_name(gf1.lv(i), j)
p = GF::Person.new(:name => n)
gf1.add_person(p)
if i >= 2
n2 = gf1.compose_name(gf1.lv(i - 1), j)
p.put_load(gf1.mem[n2], 0.5)
n3 = gf1.compose_name(gf1.lv(i - 1), j + 1)
p.put_load(gf1.mem[n3], 0.5)
end
end
end
puts "<<Pyramid No.1>>"
puts gf1
puts gf1.to_s_member
# 最下段が4人,2段目が3人,3段目が3人の俵型ピラミッドの荷重計算
# ボトムアップによる構成
# https://twitter.com/ma85003/status/1137715222154698759
h = { :workbook_name => "gf1a.xlsx" }
gf1a = GF::Formation.new(h)
gf1a.level = 3
level_person = [4, 3, 3]
1.upto(3) do |i|
1.upto(level_person[i - 1]) do |j|
n = gf1a.compose_name(gf1a.lv(i), j)
p = GF::Person.new(:name => n)
gf1a.add_person(p)
case i
when 2
n2 = gf1a.compose_name(gf1a.lv(i - 1), j)
p.put_load(gf1a.mem[n2], 0.5)
n3 = gf1a.compose_name(gf1a.lv(i - 1), j + 1)
p.put_load(gf1a.mem[n3], 0.5)
when 3
n2 = gf1a.compose_name(gf1a.lv(i - 1), j)
p.put_load(gf1a.mem[n2], 1.0)
end
end
end
puts "<<Pyramid No.1(a)>>"
puts gf1a
puts gf1a.to_s_member
# トップダウンによる構成
# (将来的にはgf/lib/gf/trigonal.rbへ入れる)
def place_topdown_trigonal(gf, name_a)
until name_a.empty?
name = name_a.shift
p = gf.mem[name]
i, j, k = gf.decompose_name(name)
if gf.lv(i) >= 3
i2 = i - 2 * (gf.opt[:descend] ? -1 : 1)
j2 = j + 1
[k, k + 1].each do |k2|
name2 = gf.compose_name(i2, j2, k2)
if !gf.mem.key?(name2)
p2 = GF::Person.new(:name => name2)
gf.add_person(p2)
name_a << name2
else
p2 = gf.mem[name2]
end
p.put_load(p2, gf.default_foot_rate)
end
end
if gf.lv(i) >= 2
i2 = i - 1 * (gf.opt[:descend] ? -1 : 1)
j2 = j
[k, k + 1].each do |k2|
name2 = gf.compose_name(i2, j2, k2)
if !gf.mem.key?(name2)
p2 = GF::Person.new(:name => name2)
gf.add_person(p2)
name_a << name2
else
p2 = gf.mem[name2]
end
p.put_load(p2, gf.default_hand_rate)
end
end
end
end
# 三角錐型6段ツートップの荷重計算
# トップダウンによる構成
# https://twitter.com/Julian75435/status/1136677486891130880
h = { :workbook_name => "gf2.xlsx" }
gf2 = GF::Formation.new(h)
lev = gf2.level = 6
name_top_a = [1, 2].map { |k| gf2.compose_name(gf2.lv(lev), 1, k) }
p_top_a = name_top_a.map { |n| GF::Person.new(:name => n) }
p_top_a.each { |p| gf2.add_person(p) }
name_top2_a = [1, 2, 3].map { |k| gf2.compose_name(gf2.lv(lev - 1), 1, k) }
p_top2_a = name_top2_a.map { |n| GF::Person.new(:name => n) }
p_top2_a.each { |p| gf2.add_person(p) }
p_top_a.each_with_index do |p, i|
p.put_load(p_top2_a[i], 0.5)
p.put_load(p_top2_a[i + 1], 0.5)
end
place_topdown_trigonal(gf2, name_top2_a)
puts "<<Pyramid No.2>>"
puts gf2
puts gf2.to_s_member
# 万里の長城の荷重計算
# ボトムアップによる構成
# [isbn:9784180958108], p.70
h = { :workbook_name => "gf3.xlsx" }
gf3 = GF::Formation.new(h)
gf3.level = 3
bottom_num = 11 # 最下段の人数
1.upto(3) do |i|
1.upto(bottom_num + 1 - i) do |j|
n = gf3.compose_name(gf3.lv(i), j)
p = GF::Person.new(:name => n)
gf3.add_person(p)
if i >= 2
rate = (i == 3) ? 0.5 : gf3.default_hand_rate
n2 = gf3.compose_name(gf3.lv(i - 1), j)
p.put_load(gf3.mem[n2], rate)
n3 = gf3.compose_name(gf3.lv(i - 1), j + 1)
p.put_load(gf3.mem[n3], rate)
end
end
end
puts "<<Pyramid No.3>>"
puts gf3
puts gf3.to_s_member
# Excel出力
if /e/i =~ ARGV[0]
require_relative "gf/lib/gf/excel.rb"
gf1.to_excel
gf1a.to_excel
gf2.to_excel
gf3.to_excel
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.