예제 #1
0
파일: kmeans.go 프로젝트: soniakeys/cluster
// 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)]
	}
}
예제 #2
0
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
}
예제 #3
0
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]
}
예제 #4
0
파일: melody.go 프로젝트: gordonklaus/songs
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())]
}
예제 #5
0
파일: remotes.go 프로젝트: jfrazelle/docker
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
}
예제 #6
0
파일: mathfuncs.go 프로젝트: philhofer/vec
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))
}
예제 #7
0
// 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
	}
}
예제 #8
0
파일: hll.go 프로젝트: postfix/hll
// 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)
	}
}
예제 #9
0
파일: dense.go 프로젝트: leoliuzcl/skizze
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
}
예제 #10
0
파일: melody.go 프로젝트: gordonklaus/songs
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]
}
예제 #11
0
// 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
}
예제 #12
0
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
		}
	}
}
예제 #13
0
// 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)
		return
	}

	w.Write(bytes)
}
예제 #14
0
func main() {
	// Use all processors
	runtime.GOMAXPROCS(runtime.NumCPU())
	// Initialize seed
	rand.Seed(time.Now().UnixNano())

	// Initialize population
	pop := make([][6]float64, 0, PopSize)
	var newPop [][6]float64
	var wins [PopSize]int
	var fitness [PopSize + 1]float64
	var generation int
	var tempGenome [6]float64
	// If there's an argument for it, read the population
	if len(os.Args) == 2 {
		file, err := os.Open(os.Args[1])
		if err != nil {
			if os.IsNotExist(err) {
				log.Println(err)
			}
		} else {
			decoder := json.NewDecoder(file)
			if err := decoder.Decode(&pop); err != nil {
				log.Println(err)
				log.Println("Writing new file")
			}
		}
	}
	// Otherwise, generate one randomly. This also fills up empty space
	// in undersized populations that have been loaded
	for i := len(pop); i < PopSize; i++ {
		for j := 0; j < 6; j++ {
			tempGenome[j] = 2*rand.Float64() - 1
		}
		pop = append(pop, tempGenome)
	}
	var genomeOrder []int
	// Function/closures for each game
	isDone := func(game c4.State) bool {
		return game.IsDone()
	}
	displayNoBoard := func(game c4.State) {}
	showError := func(err error) {
		fmt.Println(err)
	}
	winnerChan := make(chan c4.Piece, 1)
	var winner c4.Piece
	notifyWinner := func(winner c4.Piece) {
		if winner == c4.Red {
			fmt.Println("c4.Red wins!")
		} else if winner == c4.Black {
			fmt.Println("c4.Black wins!")
		} else {
			fmt.Println("It's a draw.")
		}
		winnerChan <- winner
	}
	// Fitness temps
	var acc float64
	var tempFitness float64
	var bestFitness float64
	var bestGenome [6]float64
	// Temps
	var g1, g2 int
	var f1, f2 c4.EvalFactors
	var randNum float64

	for {
		// Save the generation
		// Determine fitness
		for battle := 0; battle < BattleCount; battle++ {
			// Initialize a permutation of competitors
			genomeOrder = rand.Perm(PopSize)
			for g1 = 0; g1 < PopSize; g1++ {
				g2 = genomeOrder[g1]
				f1 = c4.EvalFactors{pop[g1][0], pop[g1][1], pop[g1][2],
					pop[g1][3], pop[g1][4], pop[g1][5]}
				f2 = c4.EvalFactors{pop[g2][0], pop[g2][1], pop[g2][2],
					pop[g2][3], pop[g2][4], pop[g2][5]}
				fmt.Printf(
					"\nGeneration %v, round %v/%v, genome %v/%v:\n\t"+
						"%v (%v/%v)\n\tvs\n\t%v (%v/%v)\n",
					generation, battle+1, BattleCount, g1+1, PopSize,
					f1, wins[g1], battle*2,
					f2, wins[g2], battle*2)
				// Run a game with the competitors
				c4.RunGame(
					c4.AlphaBetaAI{
						c4.Red,
						8,
						func(game c4.State, p c4.Piece) float64 {
							return f1.Eval(game, p)
						},
						isDone,
					},
					c4.AlphaBetaAI{
						c4.Black,
						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
		}
	}
}
예제 #15
0
// 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)
}
예제 #16
0
func pickWinner(rouletteWheel []float64) (ret int) {
	largest := rouletteWheel[len(rouletteWheel)-1]
	winner := rand.Float64() * largest
	winnerIndex := sort.SearchFloat64s(rouletteWheel, winner)
	return winnerIndex
}
예제 #17
0
파일: node.go 프로젝트: arider/goBN
// Densitys hold the sorted probailities and the original indices
// the sample function will return the original index of the "bucket" sampled.
func (d Density) sample(r *rand.Rand) int {
	index := sort.SearchFloat64s(d.prefixSum, r.Float64())
	//	fmt.Println("CHECK", len(d.sorted), index)
	return d.index[index]
}