定制 kick-in/hungarian 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kick-in/hungarian

Composer 安装命令:

composer require kick-in/hungarian

包简介

README 文档

README

PHP implementation of various algorithms for the Linear Assignment Problem.

Build status

Documentation

The original paper by Jonker and Volgenant can be found SpringerLink or in the doc/ folder.

Usage

The hungarian library contains the fundamentals needed to solve a linear assignment problem, as well as some abstractions to make integration easier.

Basic usage

The plain matrix object is indexed by integer and allows getting and setting of values, it is always a square matrix. To fill a three by three matrix, you might do the following.

use Kickin\Hungarian\Matrix\Matrix;

$matrix = new Matrix(3);
$matrix->set(0, 0, 10);
$matrix->set(1, 0, 15);
$matrix->set(2, 0, 12);
$matrix->set(0, 1, 12);
$matrix->set(1, 1, 13);
$matrix->set(2, 1, 14);
$matrix->set(0, 2, 15);
$matrix->set(1, 2, 17);
$matrix->set(2, 2, 25);

Using the matrix above, we can use the Hungarian method.

use Kickin\Hungarian\Algo\Hungarian;

$solver = new Hungarian();
$result = $solver->solve($matrix);

Or, if you'd want to find the highest scoring assignment, you can call use solveMax().

$result = $solver->solveMax($matrix);

Under the hood, this is equivalent to solving the matrix after calling invert().

This result can then be used as a list of tuples.

foreach($result as [$row, $col]){
  echo $row . ": " . $col . "\n";
}

Alternate types of matrices

In most cases, you're not actually trying to pair integers, instead you might want to assign users to tasks. One way to do this, is by using string labels

use Kickin\Hungarian\Matrix\StringMatrix;

$matrix = new StringMatrix(["Alice", "Bob", "Carol"], ["Bathroom", "Kitchen", "Windows"]);
$matrix->set("Alice", "Bathroom", 10);
$matrix->set("Bob",   "Bathroom", 15);
$matrix->set("Carol", "Bathroom", 12);
$matrix->set("Alice", "Kitchen",  12);
$matrix->set("Bob",   "Kitchen",  13);
$matrix->set("Carol", "Kitchen",  14);
$matrix->set("Alice", "Windows",  15);
$matrix->set("Bob",   "Windows",  17);
$matrix->set("Carol", "Windows",  25);

Another option is by using objects as labels, for example using Eloquent

use Kickin\Hungarian\Matrix\LabeledMatrix;

$matrix = new LabeledMatrix(User::all(), Task::all());

MatrixBuilder

Finally, it is likely you don't have a square matrix or need to write quite some boilerplate code to create a proper matrix. To help in this, you can use the MatrixBuilder class. This will automatically ensure your matrix is square, augmenting it where needed.

use Kickin\Hungarian\Matrix\MatrixBuilder;

$builder = new MatrixBuilder();
$builder->setRowSource(["Alice", "Bob", "Carol", "Dave"]);
$builder->setColSource(["Garbage", "Sweep floor"]);
$builder->setDefaultValue(1);
$builder->setAugmentValue(10);
$builder->setMappingFunction(function($row, $col){
  return 1; // Define your own scoring for any assignment pair
});

$matrix = $builder->build();

If desired, you can easily remove unassigned rows and columns from the results

$result = $solver->solve($matrix);
$assignedOnly = $result->withoutUnassigned();

统计信息

  • 总下载量: 13.67k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 6
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-01-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固