comb

package module
Version: v0.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2019 License: MIT Imports: 7 Imported by: 0

README

comb

CircleCI

This library provides the ability to split Slice elements into multiple groups.
The approach is to solve combinatorial optimization problems by minimizing the energy function.

Usage

The interface is similar to sort.Slice. It can be used as the following example.


target := []float64{1.0, 2.0, 3.0, 3.5, 4.0, 3.8}
score := func(ind []int) float64 {
	total := 0.0
	for _, i := range ind {
		total += target[i]
	}
	return total
}
energy := func(inds [][]int) float64 {
	var min, max float64
	for _, ind := range inds {
		s := score(ind)
		if min > s {
			min = s
		}
		if max < s {
			max = s
		}
	}
	return (max - min) * (max - min)
}
groups := comb.Slice(target, 4, energy)

for _, group := range groups {
	fmt.Printf("[")
	for _, i := range group {
		fmt.Printf(" %f ", target[i])
	}
	fmt.Printf("] => %f\n", score(group))
}
//Output:
//[ 1.000000  3.800000 ] => 4.800000
//[ 2.000000  3.000000 ] => 5.000000
//[ 3.500000 ] => 3.500000
//[ 4.000000 ] => 4.000000

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Slice

func Slice(slice interface{}, groupCount int, energy func(i [][]int) float64, options ...Option) [][]int
Example
package main

import (
	"fmt"

	"github.com/mashiike/comb"
)

func main() {
	target := []float64{1.0, 2.0, 3.0, 3.5, 4.0, 3.8}
	score := func(ind []int) float64 {
		total := 0.0
		for _, i := range ind {
			total += target[i]
		}
		return total
	}
	energy := func(inds [][]int) float64 {
		var min, max float64
		for _, ind := range inds {
			s := score(ind)
			if min > s {
				min = s
			}
			if max < s {
				max = s
			}
		}
		return (max - min) * (max - min)
	}
	groups := comb.Slice(target, 4, energy, comb.Seed(1))

	for _, group := range groups {
		fmt.Printf("[")
		for _, i := range group {
			fmt.Printf(" %f ", target[i])
		}
		fmt.Printf("] => %f\n", score(group))
	}
}
Output:

[ 1.000000  3.800000 ] => 4.800000
[ 2.000000  3.000000 ] => 5.000000
[ 3.500000 ] => 3.500000
[ 4.000000 ] => 4.000000

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

type Option

type Option func(*Config)

func MaxIter

func MaxIter(n int) Option

func Seed

func Seed(n int64) Option

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL