<commit_before>// Copyright 2015 <NAME>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package goga

import (
	"math"

	"github.com/cpmech/gosl/rnd"
	"github.com/cpmech/gosl/utl"
)

// Individual implements one individual in a population
type Individual struct {

	// essential data
	Ovas   []float64 // objective values
	Oors   []float64 // out-of-range values: sum of positive distances from constraints
	Floats []float64 // [optional] floats
	Ints   []int     // [optional] integers

	// auxiliary
	Id int // identifier; e.g. for debugging

	// comparison, diversity and non-dominance sorting data
	WinOver   []*Individual // [ninds] individuals dominated by this individual
	Nwins     int           // number of wins => current len(WinOver)
	Nlosses   int           // number of individuals dominating this individual
	FrontId   int           // Pareto front id
	DistCrowd float64       // crowd distance
	DistNeigh float64       // minimum distance to any neighbouring individual
}

// NewIndividual allocates a new individual
func NewIndividual(nova, noor int, floats []float64, ints []int) (o *Individual) {
	o = new(Individual)
	o.Ovas = make([]float64, nova)
	o.Oors = make([]float64, noor)
	if len(floats) > 0 {
		o.Floats = make([]float64, len(floats))
		copy(o.Floats, floats)
	}
	if len(ints) > 0 {
		o.Ints = make([]int, len(ints))
		copy(o.Ints, ints)
	}
	return
}

// GetCopy returns a copy of this individual
func (o Individual) GetCopy() (x *Individual) {
	x = new(Individual)
	x.Ovas = make([]float64, len(o.Ovas))
	x.Oors = make([]float64, len(o.Oors))
	copy(x.Ovas, o.Ovas)
	copy(x.Oors, o.Oors)
	if len(o.Floats) > 0 {
		x.Floats = make([]float64, len(o.Floats))
		copy(x.Floats, o.Floats)
	}
	if len(o.Ints) > 0 {
		x.Ints = make([]int, len(o.Ints))
		copy(x.Ints, o.Ints)
	}
	x.Id = o.Id
	return
}

// CopyInto copies this individual's data into another individual
func (o Individual) CopyInto(x *Individual) {
	copy(x.Ovas, o.Ovas)
	copy(x.Oors, o.Oors)
	if len(o.Floats) > 0 {
		copy(x.Floats, o.Floats)
	}
	if len(o.Ints) > 0 {
		copy(x.Ints, o.Ints)
	}
	x.Id = o.Id
	return
}

// Feasible returns whether this individual is feasible or not
func (o Individual) Feasible() bool {
	for _, oor := range o.Oors {
		if oor > 0 {
			return false
		}
	}
	return true
}

// Fight implements the competition between A and B
func (A *Individual) Fight(B *Individual) (A_wins bool) {
	A_dom, B_dom := IndCompare(A, B)
	if A_dom {
		return true
	}
	if B_dom {
		return false
	}
	if A.FrontId == B.FrontId {
		if A.DistCrowd > B.DistCrowd {
			return true
		}
		if B.DistCrowd > A.DistCrowd {
			return false
		}
	}
	//if false {
	if true {
		//io.Pforan("A.dist=%v  B.dist=%v\n", A.DistNeigh, B.DistNeigh)
		if A.DistNeigh > B.DistNeigh {
			return true
		}
		if B.DistNeigh > A.DistNeigh {
			return true
		}
	}
	if rnd.FlipCoin(0.5) {
		return true
	}
	return false
}

// global functions ////////////////////////////////////////////////////////////////////////////////

// IndCompare compares individual 'A' with another one 'B'. Deterministic method
func IndCompare(A, B *Individual) (A_dominates, B_dominates bool) {
	var A_nviolations, B_nviolations int
	for i := 0; i < len(A.Oors); i++ {
		if A.Oors[i] > 0 {
			A_nviolations++
		}
		if B.Oors[i] > 0 {
			B_nviolations++
		}
	}<commit_msg>code clean up<commit_after>// Copyright 2015 <NAME>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package goga

import (
	"math"

	"github.com/cpmech/gosl/rnd"
	"github.com/cpmech/gosl/utl"
)

// Individual implements one individual in a population
type Individual struct {

	// essential data
	Ovas   []float64 // objective values
	Oors   []float64 // out-of-range values: sum of positive distances from constraints
	Floats []float64 // [optional] floats
	Ints   []int     // [optional] integers

	// auxiliary
	Id int // identifier; e.g. for debugging

	// comparison, diversity and non-dominance sorting data
	WinOver   []*Individual // [ninds] individuals dominated by this individual
	Nwins     int           // number of wins => current len(WinOver)
	Nlosses   int           // number of individuals dominating this individual
	FrontId   int           // Pareto front id
	DistCrowd float64       // crowd distance
	DistNeigh float64       // minimum distance to any neighbouring individual
}

// NewIndividual allocates a new individual
func NewIndividual(nova, noor int, floats []float64, ints []int) (o *Individual) {
	o = new(Individual)
	o.Ovas = make([]float64, nova)
	o.Oors = make([]float64, noor)
	if len(floats) > 0 {
		o.Floats = make([]float64, len(floats))
		copy(o.Floats, floats)
	}
	if len(ints) > 0 {
		o.Ints = make([]int, len(ints))
		copy(o.Ints, ints)
	}
	return
}

// GetCopy returns a copy of this individual
func (o Individual) GetCopy() (x *Individual) {
	x = new(Individual)
	x.Ovas = make([]float64, len(o.Ovas))
	x.Oors = make([]float64, len(o.Oors))
	copy(x.Ovas, o.Ovas)
	copy(x.Oors, o.Oors)
	if len(o.Floats) > 0 {
		x.Floats = make([]float64, len(o.Floats))
		copy(x.Floats, o.Floats)
	}
	if len(o.Ints) > 0 {
		x.Ints = make([]int, len(o.Ints))
		copy(x.Ints, o.Ints)
	}
	x.Id = o.Id
	return
}

// CopyInto copies this individual's data into another individual
func (o Individual) CopyInto(x *Individual) {
	copy(x.Ovas, o.Ovas)
	copy(x.Oors, o.Oors)
	if len(o.Floats) > 0 {
		copy(x.Floats, o.Floats)
	}
	if len(o.Ints) > 0 {
		copy(x.Ints, o.Ints)
	}
	x.Id = o.Id
	return
}

// Feasible returns whether this individual is feasible or not
func (o Individual) Feasible() bool {
	for _, oor := range o.Oors {
		if oor > 0 {
			return false
		}
	}
	return true
}

// Fight implements the competition between A and B
func (A *Individual) Fight(B *Individual) (A_wins bool) {
	A_dom, B_dom := IndCompare(A, B)
	if A_dom {
		return true
	}
	if B_dom {
		return false
	}
	if A.FrontId == B.FrontId {
		if A.DistCrowd > B.DistCrowd {
			return true
		}
		if B.DistCrowd > A.DistCrowd {
			return false
		}
	}
	if A.DistNeigh > B.DistNeigh {
		return true
	}
	if B.DistNeigh > A.DistNeigh {
		return true
	}
	if rnd.FlipCoin(0.5) {
		return true
	}
	return false
}

// global functions ////////////////////////////////////////////////////////////////////////////////

// IndCompare compares individual 'A' with another one 'B'. Deterministic method
func IndCompare(A, B *Individual) (A_dominates, B_dominates bool) {
	var A_nviolations, B_nviolations int
	for i := 0; i < len(A.Oors); i++ {
		if A.Oors[i] > 0 {
			A_nviolations++
		}
		if B.Oors[i] > 0 {
			B_nviolations++
		}
	}
{'fraction_non_alphanumeric': 0.15061322165719412, 'fraction_numerical': 0.011965300628178284, 'mean_word_length': 2.7779661016949153, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1592354', 'n_tokens_mistral': 2721, 'n_tokens_neox': 2496, 'n_words': 712}