当前位置: 首页>>代码示例>>Golang>>正文


Golang sort.SearchFloat64s函数代码示例

本文整理汇总了Golang中sort.SearchFloat64s函数的典型用法代码示例。如果您正苦于以下问题:Golang SearchFloat64s函数的具体用法?Golang SearchFloat64s怎么用?Golang SearchFloat64s使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了SearchFloat64s函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: KMSeedPP

// KMSeedPP generates seed centers for KMeans using the KMeans++ initializer.
//
// KMSeedPP is the ++ part.  It picks the first seed randomly from points,
// then picks successive seeds randomly with probability proportional to the
// squared distance to the nearest seed.
//
// Randomness comes from math/rand default generator and is not seeded here.
//
// Returned seeds are copies of the selected points.
func KMSeedPP(points []Point, k int) []Point {
	seeds := make([]Point, k)           // return value
	p := points[rand.Intn(len(points))] // select first seed randomly
	d2 := make([]float64, len(points))  // minimum sqd to any seed
	for i, p2 := range points {         // initialize d2
		d2[i] = p.Sqd(p2)
	}
	dSum := make([]float64, len(points)) // cumulative d2 distances
	for sx := 0; ; {
		seeds[sx] = append(Point{}, p...) // duplicate selected point
		sx++
		if sx == k {
			return seeds
		}
		// update d2
		if sx > 1 { // (first seed comes with d2 already done)
			for i, p2 := range points {
				if d := p.Sqd(p2); d < d2[i] {
					d2[i] = d
				}
			}
		}
		// compute dSum
		sum := 0.
		for i, d := range d2 {
			sum += d
			dSum[i] = sum
		}
		// select next seed from up with probability proportional to d2
		p = points[sort.SearchFloat64s(dSum, rand.Float64()*sum)]
	}
}
开发者ID:soniakeys,项目名称:cluster,代码行数:41,代码来源:kmeans.go

示例2: ChoiceOne

func (p PossibilityWeightRander) ChoiceOne(r *KmgRand) (Index int) {
	if len(p.SearchList) == 0 {
		panic("[PossibilityWeightRander.ChoiceOne] len(p.SearchList)==0")
	}
	floatR := r.Float64Between(0, p.Total)
	return sort.SearchFloat64s(p.SearchList, floatR) - 1
}
开发者ID:iizotop,项目名称:kmg,代码行数:7,代码来源:PossibilityWeight.go

示例3: next

func (g valueGrid) next(x float64, next bool) float64 {
	i := sort.SearchFloat64s(g.values, x)
	n := len(g.values)
	if i == n {
		return g.values[n-1]
	}
	if g.values[i] == x {
		if next {
			i++
		} else {
			i--
		}
	} else {
		if !next {
			i--
		}
	}
	if i < 0 {
		i = 0
	}
	if i == n {
		i = n - 1
	}
	return g.values[i]
}
开发者ID:gordonklaus,项目名称:audiogui,代码行数:25,代码来源:grid.go

示例4: selectRatio

func selectRatio(complexity func(ratio) float64) ratio {
	sum := 0.0
	sums := make([]float64, len(ratios))
	for i, r := range ratios {
		sum += complexity(r)
		sums[i] = sum
	}
	return ratios[sort.SearchFloat64s(sums, sum*rand.Float64())]
}
开发者ID:gordonklaus,项目名称:songs,代码行数:9,代码来源:melody.go

示例5: Select

func (mwr *remotesWeightedRandom) Select(excludes ...string) (api.Peer, error) {
	mwr.mu.Lock()
	defer mwr.mu.Unlock()

	// NOTE(stevvooe): We then use a weighted random selection algorithm
	// (http://stackoverflow.com/questions/4463561/weighted-random-selection-from-array)
	// to choose the master to connect to.
	//
	// It is possible that this is insufficient. The following may inform a
	// better solution:

	// https://github.com/LK4D4/sample
	//
	// The first link applies exponential distribution weight choice reservoir
	// sampling. This may be relevant if we view the master selection as a
	// distributed reservoir sampling problem.

	// bias to zero-weighted remotes have same probability. otherwise, we
	// always select first entry when all are zero.
	const bias = 0.001

	// clear out workspace
	mwr.cdf = mwr.cdf[:0]
	mwr.peers = mwr.peers[:0]

	cum := 0.0
	// calculate CDF over weights
Loop:
	for peer, weight := range mwr.remotes {
		for _, exclude := range excludes {
			if peer.NodeID == exclude || peer.Addr == exclude {
				// if this peer is excluded, ignore it by continuing the loop to label Loop
				continue Loop
			}
		}
		if weight < 0 {
			// treat these as zero, to keep there selection unlikely.
			weight = 0
		}

		cum += float64(weight) + bias
		mwr.cdf = append(mwr.cdf, cum)
		mwr.peers = append(mwr.peers, peer)
	}

	if len(mwr.peers) == 0 {
		return api.Peer{}, errRemotesUnavailable
	}

	r := mwr.cdf[len(mwr.cdf)-1] * rand.Float64()
	i := sort.SearchFloat64s(mwr.cdf, r)

	return mwr.peers[i], nil
}
开发者ID:jfrazelle,项目名称:docker,代码行数:54,代码来源:remotes.go

示例6: Spearman

func Spearman(xs []float64, ys []float64) float64 {
	if len(xs) != len(ys) {
		return 0.0
	}
	meanrank := float64(len(xs)) / 2.0
	sortedxs := xs
	sortedys := ys
	sort.Float64s(sortedxs)
	sort.Float64s(sortedys)
	var num, denomx, denomy float64
	for i, x := range xs {
		xrank := float64(sort.SearchFloat64s(sortedxs, x))
		yrank := float64(sort.SearchFloat64s(sortedys, ys[i]))
		sx := xrank - meanrank
		sy := yrank - meanrank
		num += sx * sy
		denomx += sx * sx
		denomy += sy * sy
	}
	return num / (math.Sqrt(denomx) * math.Sqrt(denomy))
}
开发者ID:philhofer,项目名称:vec,代码行数:21,代码来源:mathfuncs.go

示例7: pickDegree

// pickDegree returns the smallest index i such that cdf[i] > r
// (r a random number from the random generator)
// cdf must be sorted in ascending order.
func pickDegree(random *rand.Rand, cdf []float64) int {
	r := random.Float64()
	d := sort.SearchFloat64s(cdf, r)
	if cdf[d] > r {
		return d
	}

	if d < len(cdf)-1 {
		return d + 1
	} else {
		return len(cdf) - 1
	}
}
开发者ID:pkdevboxy,项目名称:gofountain,代码行数:16,代码来源:util.go

示例8: estimateBias

// Get bias estimation calculated from the empirical results found in appendix.
// If estimate is not in the raw estimates, calculates a weighted mean to determine the bias.
func (h *Hll) estimateBias(e float64) float64 {
	biasData := biasMap[h.p]
	rawEstimate := estimateMap[h.p]
	index := sort.SearchFloat64s(rawEstimate, e)
	if index == len(rawEstimate) {
		return biasData[index-1]
	} else if index == 0 {
		return biasData[0]
	} else {
		weight1 := rawEstimate[index] - e
		weight2 := e - rawEstimate[index-1]
		return (biasData[index]*weight1 + biasData[index-1]*weight2) / (weight1 + weight2)
	}
}
开发者ID:postfix,项目名称:hll,代码行数:16,代码来源:hll.go

示例9: estimateBias

func (h *HLLPP) estimateBias(e float64) float64 {
	estimates := rawEstimateData[h.p-4]
	biases := biasData[h.p-4]

	index := sort.SearchFloat64s(estimates, e)

	if index == 0 {
		return biases[0]
	} else if index == len(estimates) {
		return biases[len(biases)-1]
	}

	e1, e2 := estimates[index-1], estimates[index]
	b1, b2 := biases[index-1], biases[index]

	r := (e - e1) / (e2 - e1)
	return b1*(1-r) + b2*r
}
开发者ID:leoliuzcl,项目名称:skizze,代码行数:18,代码来源:dense.go

示例10: nextAfter

func (m *melody) nextAfter(duration float64, prev *note, rats []ratio) (*note, ratio) {
	if prev.time.max < m.time-m.coherencyTime {
		fmt.Printf("melody: %.2f < %.2f\n", prev.time.max, m.time-m.coherencyTime)
	}

	time := interval{m.time, m.time + duration}

	cSum, ampSum := m.historyComplexity(time)
	sum := 0.0
	sums := make([]float64, len(rats))
	for i, r := range rats {
		p := math.Log2(prev.abs * r.float() / m.center)
		sum += math.Exp2(-p*p/2) * math.Exp2(-m.complexity(time, prev, cSum, ampSum, r))
		sums[i] = sum
	}
	i := sort.SearchFloat64s(sums, sum*rand.Float64())
	return m.appendHistory(time, prev, rats[i]), rats[i]
}
开发者ID:gordonklaus,项目名称:songs,代码行数:18,代码来源:melody.go

示例11: Train

// Start with initial residual r = y, and β1 = β2 = · · · = βp = 0.
// Find the predictor Zj (j = 1, . . . , p) most correlated with r
// Update βj ← βj + δj
// Set r ← r − δjZj
// Repeat
//
// Pretty much the same as least squares boosting
func (f *ForwardStage) Train(y []float64) error {
	// first we need to standardize the matrix and scale y
	// and set up variables
	f.x.Standardize() // make sure x_j_bar = 0
	n, p := f.x.rows, f.x.cols

	// set all betas to 0
	f.betas = rep(0.0, p)

	// center y
	r := subtractMean(y) // make sure y_bar = 0
	x := mat64.NewDense(n, p, rep(0.0, n*p))
	f.firstRun = true

	// how do we know when to stop?
	for f.isCorrelation(r) {

		// find the most correlated variable
		cors := make([]float64, 0, f.x.cols)
		for i := 0; i < f.x.cols; i++ {
			cors[i] = cor(f.x.data.Col(nil, i), y)
		}
		maxCor := max(cors)
		maxIdx := sort.SearchFloat64s(cors, maxCor)

		// update beta_j
		// beta_j = beta_j + delta_j
		// where delta_j = epsilon * sign(y, x_j)
		x.SetCol(maxIdx, f.x.data.Col(nil, maxIdx))
		//ols := NewOLS(&DataFrame{x, n, p, nil})
		//ols.Train(r)

		// update beta
		delta := f.epsilon * sign(sum(prod(x.Col(nil, maxIdx), r)))
		f.betas[maxIdx] += delta

		// set r = r - delta_j * x_j
		r = diff(r, multSlice(x.Col(nil, maxIdx), delta))

	}
	return nil
}
开发者ID:timkaye11,项目名称:glasso,代码行数:49,代码来源:forward_stagewise.go

示例12: Observe

func (h *histogram) Observe(v float64) {
	// TODO(beorn7): For small numbers of buckets (<30), a linear search is
	// slightly faster than the binary search. If we really care, we could
	// switch from one search strategy to the other depending on the number
	// of buckets.
	//
	// Microbenchmarks (BenchmarkHistogramNoLabels):
	// 11 buckets: 38.3 ns/op linear - binary 48.7 ns/op
	// 100 buckets: 78.1 ns/op linear - binary 54.9 ns/op
	// 300 buckets: 154 ns/op linear - binary 61.6 ns/op
	i := sort.SearchFloat64s(h.upperBounds, v)
	if i < len(h.counts) {
		atomic.AddUint64(&h.counts[i], 1)
	}
	atomic.AddUint64(&h.count, 1)
	for {
		oldBits := atomic.LoadUint64(&h.sumBits)
		newBits := math.Float64bits(math.Float64frombits(oldBits) + v)
		if atomic.CompareAndSwapUint64(&h.sumBits, oldBits, newBits) {
			break
		}
	}
}
开发者ID:COLDTURNIP,项目名称:kubernetes,代码行数:23,代码来源:histogram.go

示例13: CreateFenceHandler

// CreateFenceHandler POST /fences
func CreateFenceHandler(w http.ResponseWriter, r *http.Request) {
	user, err := auth.ValidateSession(r)

	if err != nil {
		http.Error(w, "Invalid Session token. "+err.Error(), http.StatusUnauthorized)
		return
	}

	decoder := json.NewDecoder(r.Body)
	var requestFence fenceResponse
	err = decoder.Decode(&requestFence)

	if err != nil {
		InternalServerError(err, w)
		return
	}

	indexRadius := sort.SearchInts(models.UpgradeTypesRadius[:], requestFence.Radius)
	if !(indexRadius < len(models.UpgradeTypesRadius) && models.UpgradeTypesRadius[indexRadius] == requestFence.Radius) {
		http.Error(w, "Invalid Radius", http.StatusExpectationFailed)
		return
	}

	indexRent := sort.SearchFloat64s(models.UpgradeTypesRent[:], float64(requestFence.RentMultiplier))
	if !(indexRent < len(models.UpgradeTypesRent) && models.UpgradeTypesRent[indexRent] == requestFence.RentMultiplier) {
		http.Error(w, "Invalid RentMultiplier", http.StatusExpectationFailed)
		return
	}

	if requestFence.TTL <= 0 || requestFence.TTL > models.FenceMaxTTL {
		http.Error(w, "Invalid TTL", http.StatusExpectationFailed)
		return
	}

	var f models.Fence

	f.Lat = requestFence.Lat
	f.Lon = requestFence.Lon
	f.Name = requestFence.Name
	f.Radius = requestFence.Radius
	f.RentMultiplier = requestFence.RentMultiplier
	f.TTL = requestFence.TTL
	f.DiesAt = time.Now().Add(time.Duration(f.TTL) * time.Second)
	f.UserID = user.ID

	overlap, err := checkFenceOverlap(&f)

	if err != nil {
		InternalServerError(err, w)
		return
	}

	if overlap {
		http.Error(w, "Fence does overlap.", http.StatusBadRequest)
		return
	}

	price, err := scores.GetGeoFencePrice(f.Lat, f.Lon, f.TTL, f.RentMultiplier, indexRadius)
	if price > user.Balance {
		http.Error(w, "You do not have enough money for this thing.", http.StatusPaymentRequired)
		return
	}

	f.Cost = price

	user.LastKnownGeoHash = geomodel.GeoCell(requestFence.Lat, requestFence.Lon, models.LastKnownGeoHashResolution)
	user.Balance = user.Balance - price
	user.ExpensesGeoFenceAllTime = user.ExpensesGeoFenceAllTime + price

	err = user.Save()
	if err != nil {
		log.Errorf("Error while saving user: %v", err)
	}

	redis.AddBalanceRecord(redis.GetBalanceRecordName(user.ID, redis.BalanceNameExpenseGeoFence), price)

	f.User = *user

	err = f.Save()

	if err != nil {
		InternalServerError(err, w)
		return
	}

	err = search.IndexGeoFence(f)

	if err != nil {
		InternalServerError(err, w)
		return
	}

	jobs.QueueNotifyUsersSyncRequest(f.Lat, f.Lon)
	jobs.QueueFenceExpireRequest(&f)

	bytes, err := json.Marshal(&f)

	if err != nil {
		InternalServerError(err, w)
//.........这里部分代码省略.........
开发者ID:alternaDev,项目名称:georenting-server,代码行数:101,代码来源:fences.go

示例14: main


//.........这里部分代码省略.........
						8,
						func(game c4.State, p c4.Piece) float64 {
							return f2.Eval(game, p)
						},
						isDone,
					},
					displayNoBoard,
					showError,
					notifyWinner)
				// Update win counts
				if winner = <-winnerChan; winner == c4.Red {
					wins[g1]++
				} else if winner == c4.Black {
					wins[g2]++
				}
			}
		}

		// Calculate win/game ratios
		acc = 0
		bestFitness = math.Inf(-1)
		for i, _ := range wins {
			tempFitness = float64(wins[i]) / float64(BattleCount)
			// The actual numbers we use will be consist of weighted ranges
			// picked randomly, which we can speed up using a binary search
			fitness[i] = acc
			acc += tempFitness
			// Keep the best genome of the generation
			if tempFitness > bestFitness {
				bestFitness = tempFitness
				bestGenome = pop[i]
			}
		}
		// Add a top to the last range
		fitness[PopSize] = acc

		newPop = make([][6]float64, 0, PopSize)
		for i := 0; i < PopSize; i++ {
			// SELECTION
			// Find two random genomes
			randNum = rand.Float64() * acc
			// The binary search always goes up from randNum, except at 0,
			// so we need to compensate for that
			if randNum != 0 {
				g1 = sort.SearchFloat64s(fitness[0:len(fitness)], randNum)
				g1--
			} else {
				g1 = 0
			}
			randNum = rand.Float64() * acc
			if randNum != 0 {
				g2 = sort.SearchFloat64s(fitness[0:len(fitness)], randNum)
				g2--
			} else {
				g2 = 0
			}

			// CROSSOVER AND MUTATION
			for j := 0; j < 6; j++ {
				// CROSSOVER
				// We're just going to pick random genes.
				// I don't think gene locality is a thing here anyway
				if rand.Intn(2) == 0 {
					tempGenome[j] = pop[g1][j]
				} else {
					tempGenome[j] = pop[g2][j]
				}

				// MUTATION
				tempGenome[j] += rand.NormFloat64() * mutationStdDev
			}

			newPop = append(newPop, tempGenome)
		}
		pop = newPop[0:PopSize]

		// Write the latest generation to a file
		if len(os.Args) == 2 {
			if file, err := os.Create(os.Args[1]); err == nil {
				enc := json.NewEncoder(file)
				enc.Encode(&pop)
				enc.Encode(&generation)
				enc.Encode(&bestGenome)
				enc.Encode(&bestFitness)
			}
		}

		// Show the best fitness
		fmt.Println("Generation:  ", generation)
		fmt.Println("Best genome: ", bestGenome)
		fmt.Println("Fitness:     ", bestFitness)
		fmt.Println()
		generation++

		// Clear the variables
		for i := 0; i < PopSize; i++ {
			wins[i] = 0
		}
	}
}
开发者ID:TJSomething,项目名称:cs773c-connect4-minimax,代码行数:101,代码来源:main.go

示例15: EstimateFenceCostHandler

// EstimateFenceCostHandler POST /fences/estimateCost
func EstimateFenceCostHandler(w http.ResponseWriter, r *http.Request) {
	user, err := auth.ValidateSession(r)

	if err != nil {
		http.Error(w, "Invalid Session token. "+err.Error(), http.StatusUnauthorized)
		return
	}

	decoder := json.NewDecoder(r.Body)
	var f fenceResponse
	err = decoder.Decode(&f)

	indexRadius := sort.SearchInts(models.UpgradeTypesRadius[:], f.Radius)
	if !(indexRadius < len(models.UpgradeTypesRadius) && models.UpgradeTypesRadius[indexRadius] == f.Radius) {
		http.Error(w, "Invalid Radius", http.StatusExpectationFailed)
		return
	}

	indexRent := sort.SearchFloat64s(models.UpgradeTypesRent[:], float64(f.RentMultiplier))
	if !(indexRent < len(models.UpgradeTypesRent) && models.UpgradeTypesRent[indexRent] == f.RentMultiplier) {
		http.Error(w, "Invalid RentMultiplier", http.StatusExpectationFailed)
		return
	}

	if f.TTL <= 0 || f.TTL > models.FenceMaxTTL {
		http.Error(w, "Invalid TTL", http.StatusExpectationFailed)
		return
	}

	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	price, err := scores.GetGeoFencePrice(f.Lat, f.Lon, f.TTL, f.RentMultiplier, indexRadius)

	if err != nil {
		InternalServerError(err, w)
		return
	}

	overlap, err := checkFenceOverlapWithFenceResponse(&f)

	if err != nil {
		InternalServerError(err, w)
		return
	}

	if overlap {
		http.Error(w, "Fence does overlap.", http.StatusBadRequest)
		return
	}

	var response = costEstimateResponse{Cost: price, CanAfford: user.Balance >= price}

	bytes, err := json.Marshal(&response)

	if err != nil {
		InternalServerError(err, w)
		return
	}

	w.Write(bytes)
}
开发者ID:alternaDev,项目名称:georenting-server,代码行数:65,代码来源:fences.go


注:本文中的sort.SearchFloat64s函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。