-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72a1c63
commit 26ba33c
Showing
13 changed files
with
415 additions
and
302 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package eaopt | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
) | ||
|
||
func ExampleDiffEvo() { | ||
// Instantiate DiffEvo | ||
var de, err = NewDefaultDiffEvo() | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
// Fix random number generation | ||
de.GA.RNG = rand.New(rand.NewSource(42)) | ||
|
||
// Run minimization | ||
var bowl = func(X []float64) (y float64) { | ||
for _, x := range X { | ||
y += x * x | ||
} | ||
return | ||
} | ||
X, y, err := de.Minimize(bowl, 2) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
// Output best encountered solution | ||
fmt.Printf("Found minimum of %.5f in %v\n", y, X) | ||
// Output: | ||
// Found minimum of 0.00000 in [6.0034503946953274e-05 0.00013615643701126828] | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package eaopt | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestNewGARNGNotNil(t *testing.T) { | ||
var conf = NewDefaultGAConfig() | ||
conf.RNG = nil | ||
var ga, err = conf.NewGA() | ||
if err != nil { | ||
t.Errorf("Expected nil, got %v", err) | ||
} | ||
if ga.RNG == nil { | ||
t.Error("RNG should not be nil") | ||
} | ||
} | ||
|
||
func TestNewGAErrors(t *testing.T) { | ||
var testCases = []struct { | ||
conf GAConfig | ||
}{ | ||
{func() GAConfig { c := NewDefaultGAConfig(); c.NPops = 0; return c }()}, | ||
{func() GAConfig { c := NewDefaultGAConfig(); c.PopSize = 0; return c }()}, | ||
{func() GAConfig { c := NewDefaultGAConfig(); c.NGenerations = 0; return c }()}, | ||
{func() GAConfig { c := NewDefaultGAConfig(); c.HofSize = 0; return c }()}, | ||
{func() GAConfig { c := NewDefaultGAConfig(); c.Model = nil; return c }()}, | ||
{func() GAConfig { c := NewDefaultGAConfig(); c.Model = invalidModels[0]; return c }()}, | ||
{func() GAConfig { c := NewDefaultGAConfig(); c.Migrator = MigRing{0}; return c }()}, | ||
{func() GAConfig { c := NewDefaultGAConfig(); c.Migrator = MigRing{1}; c.MigFrequency = 0; return c }()}, | ||
} | ||
for i, tc := range testCases { | ||
t.Run(fmt.Sprintf("TC %d", i), func(t *testing.T) { | ||
var _, err = tc.conf.NewGA() | ||
if err == nil { | ||
t.Errorf("Expected error, got nil") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNewDefaultGAConfig(t *testing.T) { | ||
var _, err = NewDefaultGAConfig().NewGA() | ||
if err != nil { | ||
t.Errorf("Expected nil, got %v", err) | ||
} | ||
} |
Oops, something went wrong.