rubylabs 0.7.2 → 0.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/spherelab.rb +97 -37
- data/lib/tsplab.rb +10 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.7.
|
1
|
+
0.7.3
|
data/lib/spherelab.rb
CHANGED
@@ -43,7 +43,7 @@ module SphereLab
|
|
43
43
|
@@delay = 0.1
|
44
44
|
|
45
45
|
NBodyView = Struct.new(:bodies, :origin, :scale, :options)
|
46
|
-
TurtleView = Struct.new(:turtle, :
|
46
|
+
TurtleView = Struct.new(:turtle, :options)
|
47
47
|
MelonView = Struct.new(:bodies, :scale, :ground, :startloc, :options)
|
48
48
|
|
49
49
|
=begin rdoc
|
@@ -287,13 +287,14 @@ module SphereLab
|
|
287
287
|
|
288
288
|
class Turtle
|
289
289
|
|
290
|
-
attr_accessor :position, :velocity, :graphic
|
290
|
+
attr_accessor :position, :velocity, :reference, :graphic
|
291
291
|
|
292
292
|
@@turtleOptions = {
|
293
293
|
:x => 20,
|
294
294
|
:y => 100,
|
295
295
|
:heading => 0,
|
296
296
|
:speed => 10,
|
297
|
+
:track => :off,
|
297
298
|
}
|
298
299
|
|
299
300
|
@@north = Vector.new(0, 10, 0)
|
@@ -306,6 +307,8 @@ module SphereLab
|
|
306
307
|
@position = Vector.new(options[:x], options[:y], 0.0)
|
307
308
|
@velocity = Vector.new(-1.0 * options[:speed] * cos(alpha), options[:speed] * sin(alpha), 0.0)
|
308
309
|
@graphic = nil
|
310
|
+
@reference = nil
|
311
|
+
@tracking = options[:track]
|
309
312
|
end
|
310
313
|
|
311
314
|
def inspect
|
@@ -331,8 +334,8 @@ module SphereLab
|
|
331
334
|
y = @velocity.x * sin(theta) + @velocity.y * cos(theta)
|
332
335
|
@velocity.x = x
|
333
336
|
@velocity.y = y
|
334
|
-
if graphic
|
335
|
-
Canvas.rotate(graphic,alpha)
|
337
|
+
if @graphic
|
338
|
+
Canvas.rotate(@graphic,alpha)
|
336
339
|
Canvas.sync
|
337
340
|
end
|
338
341
|
return alpha
|
@@ -342,13 +345,40 @@ module SphereLab
|
|
342
345
|
prevx = @position.x
|
343
346
|
prevy = @position.y
|
344
347
|
@position.add( @velocity * dt )
|
345
|
-
if graphic
|
346
|
-
Canvas.move(graphic, @position.x-prevx, prevy-@position.y,
|
348
|
+
if @graphic
|
349
|
+
Canvas.move(@graphic, @position.x-prevx, prevy-@position.y, @tracking)
|
347
350
|
Canvas.sync
|
348
351
|
end
|
349
352
|
return dt
|
350
|
-
end
|
351
|
-
|
353
|
+
end
|
354
|
+
|
355
|
+
def orient
|
356
|
+
if @reference.nil?
|
357
|
+
puts "no reference point"
|
358
|
+
return nil
|
359
|
+
end
|
360
|
+
mx, my = @reference
|
361
|
+
tx, ty = @position.x, @position.y
|
362
|
+
mid = Vector.new(tx-mx, ty-my, 0.0)
|
363
|
+
theta = Canvas.degrees(mid.angle(@velocity))
|
364
|
+
alpha = 2 * (90.0 - theta)
|
365
|
+
turn(alpha)
|
366
|
+
Canvas.sync if @graphic
|
367
|
+
return alpha
|
368
|
+
end
|
369
|
+
|
370
|
+
def track(val)
|
371
|
+
case val
|
372
|
+
when :off : @tracking = :off
|
373
|
+
when :on : @tracking = :track
|
374
|
+
else
|
375
|
+
puts "tracking is either :on or :off"
|
376
|
+
return nil
|
377
|
+
end
|
378
|
+
return val
|
379
|
+
end
|
380
|
+
|
381
|
+
end # class Turtle
|
352
382
|
|
353
383
|
=begin rdoc
|
354
384
|
Set up an experiment with a robot/turtle. Initialize the canvas, make the turtle,
|
@@ -368,11 +398,27 @@ module SphereLab
|
|
368
398
|
Canvas.init(edge, edge, "SphereLab")
|
369
399
|
turtle = Turtle.new( :x => edge/10, :y => edge/2, :heading => 0, :speed => 10 )
|
370
400
|
turtle.graphic = Canvas.polygon(poly, :outline => 'black', :fill => '#00ff88')
|
371
|
-
|
372
|
-
|
401
|
+
if options[:flag]
|
402
|
+
turtle.set_flag( *options[:flag] )
|
403
|
+
end
|
404
|
+
if options[:track]
|
405
|
+
turtle.track( options[:track] )
|
406
|
+
end
|
407
|
+
class <<turtle
|
408
|
+
def plant_flag
|
409
|
+
set_flag( @position.x, @position.y )
|
410
|
+
Canvas.sync
|
411
|
+
end
|
412
|
+
end
|
413
|
+
@@drawing = TurtleView.new( turtle, options )
|
373
414
|
Canvas.sync
|
374
415
|
return true
|
375
416
|
end
|
417
|
+
|
418
|
+
def set_flag(fx, fy)
|
419
|
+
Canvas.circle( fx, fy, 3, :fill => 'darkblue' )
|
420
|
+
@reference = [ fx, fy ]
|
421
|
+
end
|
376
422
|
|
377
423
|
def robot
|
378
424
|
if @@drawing.nil?
|
@@ -382,27 +428,13 @@ module SphereLab
|
|
382
428
|
return @@drawing.turtle
|
383
429
|
end
|
384
430
|
end
|
385
|
-
|
386
|
-
# TODO -- aim toward point on circle at original radius, not current radius...
|
387
|
-
|
388
|
-
def orient_robot
|
389
|
-
r = robot
|
390
|
-
mx, my = @@drawing.center
|
391
|
-
tx, ty = r.position.x, r.position.y
|
392
|
-
mid = Vector.new(tx-my, ty-my, 0.0)
|
393
|
-
theta = Canvas.degrees(mid.angle(r.velocity))
|
394
|
-
alpha = 2 * (90.0 - theta)
|
395
|
-
r.turn(alpha)
|
396
|
-
Canvas.sync
|
397
|
-
return alpha
|
398
|
-
end
|
399
|
-
|
431
|
+
|
400
432
|
# :begin :make_circle
|
401
433
|
def make_circle(nseg, time, angle)
|
402
434
|
view_robot
|
403
|
-
|
404
|
-
nseg.times {
|
405
|
-
|
435
|
+
robot.turn( angle/2 )
|
436
|
+
nseg.times { robot.advance(time); robot.turn(angle) }
|
437
|
+
robot.turn( -angle/2 )
|
406
438
|
return true
|
407
439
|
end
|
408
440
|
# :end :make_circle
|
@@ -428,8 +460,9 @@ module SphereLab
|
|
428
460
|
res = Vector.new(-r * cos)
|
429
461
|
end
|
430
462
|
|
431
|
-
def random_bodies(n, big)
|
463
|
+
def random_bodies(n, big, moving)
|
432
464
|
big = 1 if big.nil?
|
465
|
+
moving = true if moving.nil?
|
433
466
|
res = []
|
434
467
|
mm = 1e12 # average mass
|
435
468
|
mr = 150 # average distance from origin
|
@@ -449,6 +482,9 @@ module SphereLab
|
|
449
482
|
b.size = 5
|
450
483
|
res << b
|
451
484
|
end
|
485
|
+
if !moving
|
486
|
+
res.each { |b| b.velocity.x = b.velocity.y = b.velocity.z = 0.0 }
|
487
|
+
end
|
452
488
|
return res
|
453
489
|
end
|
454
490
|
|
@@ -464,7 +500,7 @@ module SphereLab
|
|
464
500
|
raise "usage: make_system(id)" unless args.length > 0
|
465
501
|
if args[0] == :random
|
466
502
|
raise "usage: make_system(:random, n, m)" unless args.length >= 2 && args[1].class == Fixnum
|
467
|
-
return random_bodies(args[1], args[2])
|
503
|
+
return random_bodies(args[1], args[2], args[3])
|
468
504
|
end
|
469
505
|
filename = args[0]
|
470
506
|
if filename.class == Symbol
|
@@ -489,7 +525,7 @@ module SphereLab
|
|
489
525
|
end
|
490
526
|
return bodies
|
491
527
|
end
|
492
|
-
|
528
|
+
|
493
529
|
=begin rdoc
|
494
530
|
Initialize the drawing canvas by drawing a circle for each body in list b.
|
495
531
|
=end
|
@@ -516,7 +552,30 @@ module SphereLab
|
|
516
552
|
Canvas.sync
|
517
553
|
return true
|
518
554
|
end
|
519
|
-
|
555
|
+
|
556
|
+
=begin rdoc
|
557
|
+
Demonstrate adding force vectors by moving only one body
|
558
|
+
=end
|
559
|
+
|
560
|
+
def update_one(bodies, i, time)
|
561
|
+
b = bodies[i]
|
562
|
+
if b.graphic.nil?
|
563
|
+
puts "display the system with view_system"
|
564
|
+
return nil
|
565
|
+
end
|
566
|
+
for j in 0...bodies.length
|
567
|
+
next if i == j
|
568
|
+
Body.interaction( b, bodies[j] )
|
569
|
+
end
|
570
|
+
b.move(time)
|
571
|
+
newx, newy = scale(b.position, @@drawing.origin, @@drawing.scale)
|
572
|
+
Canvas.move(b.graphic, newx-b.prevx, newy-b.prevy, @@drawing.options[:pendown])
|
573
|
+
b.prevx = newx
|
574
|
+
b.prevy = newy
|
575
|
+
b.clear_force
|
576
|
+
Canvas.sync
|
577
|
+
end
|
578
|
+
|
520
579
|
=begin rdoc
|
521
580
|
Call SphereLab::step(bodies, time) to calculate the pairwise interactions between all
|
522
581
|
Body objects in the array +bodies+ and then compute their new positions after +time+ seconds.
|
@@ -625,6 +684,12 @@ module SphereLab
|
|
625
684
|
mymax = earth.coords[1]
|
626
685
|
melon = Canvas.circle(mxmin, mymax, 5, :fill => blist[0].color)
|
627
686
|
blist[0].graphic = melon
|
687
|
+
class <<blist[0]
|
688
|
+
def height
|
689
|
+
return 0 if prevy.nil?
|
690
|
+
return position.y - prevy
|
691
|
+
end
|
692
|
+
end
|
628
693
|
scale = (mymax-mymin) / hmax.to_f
|
629
694
|
@@drawing = MelonView.new(blist, scale, blist[0].position.y, melon.coords, options)
|
630
695
|
Canvas.sync
|
@@ -656,11 +721,6 @@ module SphereLab
|
|
656
721
|
melon.position.y += height
|
657
722
|
end
|
658
723
|
melon.velocity.y = 0.0
|
659
|
-
class <<melon
|
660
|
-
def height
|
661
|
-
return position.y - prevy
|
662
|
-
end
|
663
|
-
end
|
664
724
|
return height
|
665
725
|
end
|
666
726
|
|
data/lib/tsplab.rb
CHANGED
@@ -5,6 +5,9 @@
|
|
5
5
|
=end
|
6
6
|
|
7
7
|
=begin
|
8
|
+
todo unit tests
|
9
|
+
todo set up options, defaults as in mars, spheres (@@options...)
|
10
|
+
todo :trace as one of the options, array of things to print
|
8
11
|
TODO test predigree counts
|
9
12
|
TODO add "phylogeny" links -- keep refs to parent(s)
|
10
13
|
TODO test: selection only, no mutations
|
@@ -528,6 +531,10 @@ inherited from a parent, and incremented whenever a mutation or crossover is app
|
|
528
531
|
end
|
529
532
|
|
530
533
|
def view_tour(t, userOptions = {} )
|
534
|
+
if @@drawing.nil?
|
535
|
+
puts "call view_map to initialize the canvas"
|
536
|
+
return nil
|
537
|
+
end
|
531
538
|
map = @@drawing.cities
|
532
539
|
@@drawing.links.each { |x| x.delete }
|
533
540
|
@@drawing.links.clear
|
@@ -538,7 +545,7 @@ inherited from a parent, and incremented whenever a mutation or crossover is app
|
|
538
545
|
x0, y0 = x1, y1
|
539
546
|
end
|
540
547
|
@@drawing.nodes.each { |x| x.raise }
|
541
|
-
|
548
|
+
Canvas.sync
|
542
549
|
end
|
543
550
|
|
544
551
|
# todo -- get drawing params from options
|
@@ -589,6 +596,8 @@ inherited from a parent, and incremented whenever a mutation or crossover is app
|
|
589
596
|
@@tourOptions = {
|
590
597
|
|
591
598
|
}
|
599
|
+
|
600
|
+
@@drawing = nil
|
592
601
|
|
593
602
|
end # module TSPLab
|
594
603
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubylabs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- conery
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-
|
12
|
+
date: 2010-03-01 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|