示例#1
0
func Test_groups01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("groups01")

	Init(0)

	ng := 3            // number of groups
	nints := 12        // number of integers
	size := nints / ng // groups size
	ints := utl.IntRange(nints)
	groups := utl.IntsAlloc(ng, size)
	hists := make([]*IntHistogram, ng)
	for i := 0; i < ng; i++ {
		hists[i] = &IntHistogram{Stations: utl.IntRange(nints + 1)}
	}
	IntGetGroups(groups, ints)
	io.Pfcyan("groups = %v\n", groups)
	for i := 0; i < NSAMPLES; i++ {
		IntGetGroups(groups, ints)
		for j := 0; j < ng; j++ {
			check_repeated(groups[j])
			hists[j].Count(groups[j], false)
		}
	}
	for i := 0; i < ng; i++ {
		io.Pf("\n")
		io.Pf(TextHist(hists[i].GenLabels("%d"), hists[i].Counts, 60))
	}
}
示例#2
0
func BuildIndicatorMatrix(nv int, pth []int) (x [][]int) {
	x = utl.IntsAlloc(nv, nv)
	for k := 1; k < len(pth); k++ {
		i, j := pth[k-1], pth[k]
		x[i][j] = 1
	}
	return
}
示例#3
0
文件: optimiser.go 项目: cpmech/goga
// Initialises continues initialisation by generating individuals
//  Optional:  obj  XOR  fcn, nf, ng, nh
func (o *Optimiser) Init(gen Generator_t, obj ObjFunc_t, fcn MinProb_t, nf, ng, nh int) {

	// generic or minimisation problem
	if obj != nil {
		o.ObjFunc = obj
	} else {
		if fcn == nil {
			chk.Panic("either ObjFunc or MinProb must be provided")
		}
		o.Nf, o.Ng, o.Nh, o.MinProb = nf, ng, nh, fcn
		o.ObjFunc = func(sol *Solution, cpu int) {
			o.MinProb(o.F[cpu], o.G[cpu], o.H[cpu], sol.Flt, sol.Int, cpu)
			for i, f := range o.F[cpu] {
				sol.Ova[i] = f
			}
			for i, g := range o.G[cpu] {
				sol.Oor[i] = utl.GtePenalty(g, 0.0, 1) // g[i] ≥ 0
			}
			for i, h := range o.H[cpu] {
				h = math.Abs(h)
				sol.Ova[0] += h
				sol.Oor[o.Ng+i] = utl.GtePenalty(o.EpsH, h, 1) // ϵ ≥ |h[i]|
			}
		}
		o.F = la.MatAlloc(o.Ncpu, o.Nf)
		o.G = la.MatAlloc(o.Ncpu, o.Ng)
		o.H = la.MatAlloc(o.Ncpu, o.Nh)
		o.Nova = o.Nf
		o.Noor = o.Ng + o.Nh
	}

	// calc derived parameters
	o.Generator = gen
	o.CalcDerived()

	// allocate solutions
	o.Solutions = NewSolutions(o.Nsol, &o.Parameters)
	o.Groups = make([]*Group, o.Ncpu)
	for cpu := 0; cpu < o.Ncpu; cpu++ {
		o.Groups[cpu] = new(Group)
		o.Groups[cpu].Init(cpu, o.Ncpu, o.Solutions, &o.Parameters)
	}

	// metrics
	o.Metrics = new(Metrics)
	o.Metrics.Init(o.Nsol, &o.Parameters)

	// auxiliary
	o.tmp = NewSolution(0, 0, &o.Parameters)
	o.cpupairs = utl.IntsAlloc(o.Ncpu/2, 2)
	o.iova0 = -1
	o.ova0 = make([]float64, o.Tf)

	// generate trial solutions
	o.generate_solutions(0)
}
示例#4
0
// CombineEidIps combines eids and ips(ids)
func CombineEidIps(eids, ips []int) (eids_ips [][]int) {
	eids_ips = utl.IntsAlloc(len(eids)*len(ips), 2)
	k := 0
	for _, eid := range eids {
		for _, ip := range ips {
			eids_ips[k][0] = eid
			eids_ips[k][1] = ip
			k++
		}
	}
	return
}
示例#5
0
// Init initialises Munkres' structure
func (o *Munkres) Init(nrow, ncol int) {
	chk.IntAssertLessThan(0, nrow) // nrow > 1
	chk.IntAssertLessThan(0, ncol) // ncol > 1
	o.nrow, o.ncol = nrow, ncol
	o.C = utl.DblsAlloc(o.nrow, o.ncol)
	o.M = make([][]Mask_t, o.nrow)
	for i := 0; i < o.nrow; i++ {
		o.M[i] = make([]Mask_t, o.ncol)
	}
	o.Links = make([]int, o.nrow)
	npath := 2*o.nrow + 1 // TODO: check this
	o.path = utl.IntsAlloc(npath, 2)
	o.row_covered = make([]bool, o.nrow)
	o.col_covered = make([]bool, o.ncol)
}
示例#6
0
文件: group.go 项目: cpmech/goga
// Init initialises group
func (o *Group) Init(cpu, ncpu int, solutions []*Solution, prms *Parameters) {
	nsol := len(solutions)
	start, endp1 := (cpu*nsol)/ncpu, ((cpu+1)*nsol)/ncpu
	o.Ncur = endp1 - start
	o.All = make([]*Solution, o.Ncur*2)
	o.Indices = make([]int, o.Ncur)
	o.Pairs = utl.IntsAlloc(o.Ncur/2, 2)
	for i := 0; i < o.Ncur; i++ {
		o.All[i] = solutions[start+i]
		o.All[o.Ncur+i] = NewSolution(-i, nsol, prms)
		o.Indices[i] = i
	}
	o.Metrics = new(Metrics)
	o.Metrics.Init(len(o.All), prms)
}
示例#7
0
文件: graph.go 项目: yunpeng1/gosl
// Init initialises graph
//  Input:
//    edges    -- [nedges][2] edges (connectivity)
//    weightsE -- [nedges] weights of edges. can be <nil>
//    verts    -- [nverts][ndim] vertices. can be <nil>
//    weightsV -- [nverts] weights of vertices. can be <nil>
func (o *Graph) Init(edges [][]int, weightsE []float64, verts [][]float64, weightsV []float64) {
	o.Edges, o.WeightsE = edges, weightsE
	o.Verts, o.WeightsV = verts, weightsV
	o.Shares = make(map[int][]int)
	o.Key2edge = make(map[int]int)
	for k, edge := range o.Edges {
		i, j := edge[0], edge[1]
		utl.IntIntsMapAppend(&o.Shares, i, k)
		utl.IntIntsMapAppend(&o.Shares, j, k)
		o.Key2edge[o.HashEdgeKey(i, j)] = k
	}
	if o.Verts != nil {
		chk.IntAssert(len(o.Verts), len(o.Shares))
	}
	nv := len(o.Shares)
	o.Dist = utl.DblsAlloc(nv, nv)
	o.Next = utl.IntsAlloc(nv, nv)
}
示例#8
0
文件: bspline.go 项目: yunpeng1/gosl
// Elements returns the indices of nonzero spans
func (o *Bspline) Elements() (spans [][]int) {
	nspans := 0
	for i := 0; i < o.m-1; i++ {
		l := o.T[i+1] - o.T[i]
		if math.Abs(l) > STOL {
			nspans += 1
		}
	}
	spans = utl.IntsAlloc(nspans, 2)
	ispan := 0
	for i := 0; i < o.m-1; i++ {
		l := o.T[i+1] - o.T[i]
		if math.Abs(l) > STOL {
			spans[ispan][0] = i
			spans[ispan][1] = i + 1
			ispan += 1
		}
	}
	return
}
示例#9
0
文件: delaunay.go 项目: yunpeng1/gosl
// Delaunay computes 2D Delaunay triangulation using Triangle
//  Input:
//    X = { x0, x1, x2, ... Npoints }
//    Y = { y0, y1, y2, ... Npoints }
//  Ouptut:
//    V = { { x0, y0 }, { x0, y0 }, { x0, y0 } ... Nvertices }
//    C = { { id0, id1, id2 }, { id0, id1, id2 } ... Ncellls }
func Delaunay(X, Y []float64, verbose bool) (V [][]float64, C [][]int, err error) {

	// input
	chk.IntAssert(len(X), len(Y))
	n := len(X)
	verb := 0
	if verbose {
		verb = 1
	}

	// perform triangulation
	var T C.triangulateio
	defer func() { C.trifree(&T) }()
	res := C.delaunay2d(
		&T,
		(C.long)(n),
		(*C.double)(unsafe.Pointer(&X[0])),
		(*C.double)(unsafe.Pointer(&Y[0])),
		(C.long)(verb),
	)
	if res != 0 {
		chk.Err("Delaunay2d failed: Triangle returned %d code\n", res)
	}

	// output
	nverts := int(T.numberofpoints)
	ncells := int(T.numberoftriangles)
	V = utl.DblsAlloc(nverts, 2)
	C = utl.IntsAlloc(ncells, 3)
	for i := 0; i < nverts; i++ {
		V[i][0] = float64(C.getpoint((C.long)(i), 0, &T))
		V[i][1] = float64(C.getpoint((C.long)(i), 1, &T))
	}
	for i := 0; i < ncells; i++ {
		C[i][0] = int(C.getcorner((C.long)(i), 0, &T))
		C[i][1] = int(C.getcorner((C.long)(i), 1, &T))
		C[i][2] = int(C.getcorner((C.long)(i), 2, &T))
	}
	return
}
示例#10
0
func Test_graph04(tst *testing.T) {

	//verbose()
	chk.PrintTitle("graph04")

	//           [10]
	//      0 ––––––––→ 3      numbers in parentheses
	//      |    (1)    ↑      indicate edge ids
	//   [5]|(0)        |
	//      |        (3)|[1]
	//      ↓    (2)    |      numbers in brackets
	//      1 ––––––––→ 2      indicate weights
	//           [3]

	var G Graph
	G.Init(
		// edge:  0       1       2       3
		[][]int{{0, 1}, {0, 3}, {1, 2}, {2, 3}},
		[]float64{5, 10, 3, 1}, // weights
		nil, nil,
	)

	err := G.ShortestPaths("FW")
	if err != nil {
		tst.Errorf("ShortestPaths failed:\n%v", err)
		return
	}

	source, target := 0, 3
	pth := G.Path(source, target)
	io.Pforan("pth = %v\n", pth)

	nv := len(G.Dist)
	x := utl.IntsAlloc(nv, nv)
	for k := 1; k < len(pth); k++ {
		i, j := pth[k-1], pth[k]
		io.Pforan("i=%d j=%v\n", i, j)
		x[i][j] = 1
	}
	io.Pf("%s", PrintIndicatorMatrix(x))
	errPath, errLoop := CheckIndicatorMatrix(source, target, x, chk.Verbose)
	io.Pforan("errPath = %v\n", errPath)
	io.Pforan("errLoop = %v\n", errLoop)
	if errPath != 0 {
		tst.Errorf("path is incorrect\n")
	}
	if errLoop != 0 {
		tst.Errorf("path has loops\n")
	}

	x[0][0] = 1
	x[2][1] = 1
	x[3][3] = 1
	xmat := make([]int, nv*nv)
	for i := 0; i < nv; i++ {
		for j := 0; j < nv; j++ {
			xmat[i*nv+j] = x[i][j]
		}
	}
	io.Pf("\n\n%s", PrintIndicatorMatrix(x))
	errPath, errLoop = CheckIndicatorMatrix(source, target, x, chk.Verbose)
	io.Pforan("errPath = %v\n", errPath)
	io.Pforan("errLoop = %v\n", errLoop)
	errPathRM, errLoopRM := CheckIndicatorMatrixRowMaj(source, target, nv, xmat)
	io.Pfcyan("errPath = %v\n", errPathRM)
	io.Pfcyan("errLoop = %v\n", errLoopRM)
	if errPathRM != errPath {
		tst.Errorf("row major function failed")
	}
	if errLoopRM != errLoop {
		tst.Errorf("row major function failed")
	}
}
示例#11
0
// LatinIHS implements the improved distributed hypercube sampling algorithm.
// Note: code developed by John Burkardt (GNU LGPL license) --  see source code
// for further information.
//  Input:
//   dim -- spatial dimension
//   n   -- number of points to be generated
//   d   -- duplication factor ≥ 1 (~ 5 is reasonable)
//  Output:
//   x   -- [dim][n] points
func LatinIHS(dim, n, d int) (x [][]int) {

	//  Discussion:
	//
	//    N Points in a DIM_NUM dimensional Latin hypercube are to be selected.
	//
	//    Each of the DIM_NUM coordinate dimensions is discretized to the values
	//    1 through N.  The points are to be chosen in such a way that
	//    no two points have any coordinate value in common.  This is
	//    a standard Latin hypercube requirement, and there are many
	//    solutions.
	//
	//    This algorithm differs in that it tries to pick a solution
	//    which has the property that the points are "spread out"
	//    as evenly as possible.  It does this by determining an optimal
	//    even spacing, and using the duplication factor D to allow it
	//    to choose the best of the various options available to it.
	//
	//  Licensing:
	//
	//    This code is distributed under the GNU LGPL license.
	//
	//  Modified:
	//
	//    10 April 2003
	//
	//  Author:
	//
	//    John Burkardt
	//
	//  Reference:
	//
	//    Brian Beachkofski, Ramana Grandhi,
	//    Improved Distributed Hypercube Sampling,
	//    American Institute of Aeronautics and Astronautics Paper 2002-1274.

	// auxiliary variables
	var i, j, k, count, point_index, best int
	var min_all, min_can, dist float64

	// constant
	r8_huge := 1.0E+30

	// slices
	avail := make([]int, dim*n)
	list := make([]int, d*n)
	point := make([]int, dim*d*n)
	x = utl.IntsAlloc(dim, n)

	opt := float64(n) / math.Pow(float64(n), float64(1.0/float64(dim)))

	// pick the first point
	for i = 0; i < dim; i++ {
		x[i][n-1] = Int(1, n)
	}

	// initialize avail and set an entry in a random row of each column of avail to n
	for j = 0; j < n; j++ {
		for i = 0; i < dim; i++ {
			avail[i+j*dim] = j + 1
		}
	}
	for i = 0; i < dim; i++ {
		avail[i+(x[i][n-1]-1)*dim] = n
	}

	// main loop: assign a value to x[1:m,count] for count = n-1 down to 2
	for count = n - 1; 2 <= count; count-- {

		// generate valid points.
		for i = 0; i < dim; i++ {
			for k = 0; k < d; k++ {
				for j = 0; j < count; j++ {
					list[j+k*count] = avail[i+j*dim]
				}
			}

			for k = count*d - 1; 0 <= k; k-- {
				point_index = Int(0, k)
				point[i+k*dim] = list[point_index]
				list[point_index] = list[k]
			}
		}

		// for each candidate, determine the distance to all the
		// points that have already been selected, and save the minimum value
		min_all = r8_huge
		best = 0
		for k = 0; k < d*count; k++ {
			min_can = r8_huge

			for j = count; j < n; j++ {

				dist = 0.0
				for i = 0; i < dim; i++ {
					dist = dist + math.Pow(float64(point[i+k*dim])-float64(x[i][j]), 2.0)
				}
				dist = math.Sqrt(dist)

				if dist < min_can {
					min_can = dist
				}
			}

			if math.Abs(min_can-opt) < min_all {
				min_all = math.Abs(min_can - opt)
				best = k
			}

		}
		for i = 0; i < dim; i++ {
			x[i][count-1] = point[i+best*dim]
		}

		// having chosen x[:,count], update avail
		for i = 0; i < dim; i++ {
			for j = 0; j < n; j++ {
				if avail[i+j*dim] == x[i][count-1] {
					avail[i+j*dim] = avail[i+(count-1)*dim]
				}
			}
		}
	}

	// for the last point, there's only one choice
	for i = 0; i < dim; i++ {
		x[i][0] = avail[i+0*dim]
	}
	return
}